cd732D Exams half

Topic: http://codeforces.com/problemset/problem/732/D

Meaning of the questions: give you n, m, n number, the number of m, n days, m field test, given n the first few days to test every day exam (0 if it is not that day examination), given every the number of days required to review the exam. You can not arrange to review the examination subjects per day (can only review a branch), or arrange to go to the exam. Determined minimum finished all days of the test, if the test field in incomplete m n -1 is output days.

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
int n,m,a[maxn]={0},b[maxn]={0};
bool check(int mid)
{
    int vis[maxn]={0},cost=0,cnt=0;
    for(int i=mid;i>0;i--)
    {
        if(a[i]!=0)
        {
            if(!vis[a[i]])
            {
                cost+=b[a[i]];
                vis[a[i]]=1;
                cnt++;
                if(cost+1>i)return false;
            }
            else if(cost!=0)cost--;
        }
        else if(cost!=0)cost--;
    }
    if(cnt!=m)return false;
    return true;
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    for(int i=1;i<=m;i++)scanf("%d",&b[i]);
    int l=1,r=n,ans=-1;
    while(l<=r)
    {
        int mid=l+r>>1;
        if(check(mid))
        {
            ans=mid;
            r=mid-1;
        }
        else l=mid+1;
    }
    printf("%d\n",ans);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/myrtle/p/11780731.html