Codeforces 732D [二分 ][贪心]
2016-10-19 21:37
381 查看
/* 不要低头,不要放弃,不要气馁,不要慌张 题意: n天进行m科考试,每科考试需要a的复习时间,n天每天最多可以考一科。并且指定哪天考哪科。 注意考试那天不能复习。 问最少需要多少天可全部通过考试。 思路: 转化为判定性问题。二分天数。 然后贪心,在规定天数以内,最后一天通过即可。需要保证每一科最后一天之前剩下还没考试的科目都来得及复习。(同时注意要要加上考试时间) 最后还要判定是不是所有的科目都通过了。 */ #include<bits/stdc++.h> using namespace std; long long a[100050],b[100050],sum; int n,m; bool pf(int la){ set<int>mm; long long tt=sum; for(int i=la;i>=1;i--){ if(a[i]){ if(mm.find(a[i])==mm.end()){ int gg=mm.size(); if(tt+m-gg<=i){ mm.insert(a[i]); tt-=b[a[i]]; } else return 0; } } } if(!tt)return 1; else return 0; } int main() { scanf("%d%d",&n,&m); for(int i=1;i<=n;i++)scanf("%lld",a+i); for(int i=1;i<=m;i++)scanf("%lld",b+i); for(int i=1;i<=m;i++)sum+=b[i]; int l=1,r=n; while(l<=r){ int mid=(l+r)>>1; if(pf(mid))r=mid-1; else l=mid+1; } if(l>n)puts("-1"); else printf("%d\n",l); }
相关文章推荐
- CodeForces - 732D 二分+贪心
- CodeForces-732D-Exams(二分 模拟 贪心)
- 【codeforces 732D】【二分+贪心】
- Codeforces_732D_(二分贪心)
- CodeForces-729C-Road to Cinema(二分查找 贪心 排序)
- codeforces 732D Exams 二分搜索 贪心
- Codeforces 732D 二分
- CodeForces - 363D Renting Bikes 二分+贪心
- CodeForces 734C Anton and Making Potions 【二分】【贪心】
- CodeForces - 551C (二分查找答案+贪心)
- CodeForces - 363D Renting Bikes (二分+贪心)
- CodeForces 589F -- F. Gourmet and Banquet (二分 + 贪心)
- Codeforces - 831D. Office Keys - dp、二分+贪心
- 【CodeForces 729C】【二分+贪心】Road to Cinema 题解
- CodeForces - 551C 二分+贪心
- CodeForces 377B---Preparing for the Contest(二分+贪心)
- 【二分答案 && 贪心】codeforces-847E Packmen
- Codeforces 760B Frodo and pillows【贪心+二分】
- Codeforces 609D Gadgets for dollars and pounds【二分+贪心】
- codeforces_679B. Bear and Tower of Cubes(贪心+二分+dfs)