cd732D Exams 二分

题目:http://codeforces.com/problemset/problem/732/D

题意:给你n,m,n个数,m个数,n天,m场考试,给出n天每天能考第几场考试(如果是0则那天考不了试),给出每场考试需要复习的天数。可以安排每天复习没考试的科目(只能复习一科),或者安排去考试。求出最少的考完所有试的天数,如果n天之内考不完m场则输出-1。

#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;
}

猜你喜欢

转载自www.cnblogs.com/myrtle/p/11780731.html