codeforces 732/D 二分
给出考试时间和考试需要准备的时间,问最早考完所有科目的时间
二分答案 NlogN
二分抄神犇的写法 感觉挺舒服的嘻嘻嘻
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int MAXN=1e5+5; 4 int N,M,d[MAXN],w[MAXN],cnt[MAXN]; 5 void read(int &x){ 6 x=0; 7 register char c=getchar(); 8 while(c<48||c>57) 9 c=getchar(); 10 for(;c>=48&&c<=57;c=getchar()) 11 x=(x<<1)+(x<<3)+c-48; 12 } 13 bool check(int x) 14 { 15 memset(cnt,0,sizeof(cnt)); 16 int ans = 0,val = 0; 17 for (int i=x; i>=1; i--) 18 if (d[i]&&++cnt[d[i]]==1&&w[d[i]]<i-M+ans+1) 19 { 20 ans++; 21 val+=w[d[i]]; 22 } 23 return ans == M && val+M<=x; 24 } 25 int main() 26 { 27 read(N),read(M); 28 for (int i=1; i<=N; i++) 29 read(d[i]); 30 for (int i=1; i<=M; i++) 31 read(w[i]); 32 int first = 0; 33 int last = N; 34 int mid; 35 int best = -1; 36 while (first <= last) 37 { 38 mid = (first + last)/2; 39 if (check(mid)) 40 { 41 last = mid-1; 42 best = mid; 43 } 44 else 45 { 46 first = mid+1; 47 } 48 } 49 printf("%d\n",best); 50 return 0; 51 }