D. Exams(思维+二分+贪心)

https://codeforces.com/contest/732/problem/D


思路:

开始以为是什么dp问题,发现复杂度是不够的。这个情况下答案应该是有单调性可以二分的。考虑check。

设枚举x天完成。

我们将要在x天内的(所有要完成考试的时间)更新成最后一次(deadline)

碰到空闲天就复习。碰到该考试的deadline你就只能去考了。如果不够时间就调整二分时间。如果够这门考试的时间就可以拿来复习其他科目。

最后判一下该轮是不是所有考试都考过了。(wa22)

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e5+1000;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
LL d[maxn],a[maxn];
bool check(LL x,LL m){
    map<LL,LL>map1;
    for(LL i=1;i<=x;i++){
        if(d[i]!=0){
            map1[d[i]]=i;///更新为该考试的deadline
        }
    }
    LL freeday=0;
    for(LL i=1;i<=x;i++){
        if(d[i]==0) freeday++;
        else{
            if(i==map1[d[i]]){
               freeday-=a[d[i]];
               if(freeday<0) return false;
               map1[d[i]]=-1;///考完了以后就拿来复习其他的
            }
            else freeday++;
        }
    }
    for(LL i=1;i<=m;i++){
        if(map1[i]!=-1){
            return false;
        }
    }
    if(freeday>=0) return true;
    else return false;
}
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  LL n,m;cin>>n>>m;
  for(LL i=1;i<=n;i++) cin>>d[i];
  for(LL i=1;i<=m;i++) cin>>a[i];
  LL l=1;LL r=n+1;
  while(l<r){
    LL mid=(l+r)>>1;
    if(check(mid,m) ) r=mid;
    else l=mid+1;
    ///debug(mid)
  }
  if(l==n+1){
    cout<<"-1"<<"\n";
  }
  else{
    cout<<l<<"\n";
  }
return 0;
}

猜你喜欢

转载自blog.csdn.net/zstuyyyyccccbbbb/article/details/114969507