首页 > 代码库 > CodeForces 732D Exams

CodeForces 732D Exams

二分。

二分答案,然后贪心验证一下即可。

#pragma comment(linker, "/STACK:1024000000,1024000000")#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<vector>#include<map>#include<set>#include<queue>#include<stack>#include<ctime>#include<iostream>using namespace std;typedef long long LL;const double pi=acos(-1.0),eps=1e-10;void File(){    freopen("D:\\in.txt","r",stdin);    freopen("D:\\out.txt","w",stdout);}template <class T>inline void read(T &x){    char c = getchar();    x = 0;    while(!isdigit(c)) c = getchar();    while(isdigit(c))    {        x = x * 10 + c - 0;        c = getchar();    }}int n,m;int a[200010],b[200010];long long t[200010];bool flag[200010];bool check(int x){    memset(flag,0,sizeof flag);    for(int i=1;i<=x;i++) a[i]=b[i];    for(int i=x;i>=1;i--)    {        if(a[i]==0) continue;        if(flag[a[i]]) a[i]=0;        else flag[a[i]]=1;    }    long long sum=0; int cnt=0;    for(int i=1;i<=x;i++)    {        if(a[i]==0) sum=sum+1;        else        {            sum=sum-t[a[i]];            cnt++;        }        if(sum<0) return 0;    }    if(cnt<m) return 0;    return 1;}int main(){    cin>>n>>m;    for(int i=1;i<=n;i++) cin>>b[i];    for(int i=1;i<=m;i++) cin>>t[i];    int L=1,R=n,ans=-1;    while(L<=R)    {        int mid=(L+R)/2;        if(check(mid)) R=mid-1,ans=mid;        else L=mid+1;    }    cout<<ans<<endl;    return 0;}

 

CodeForces 732D Exams