CodeForces 732D Exams (思维+set乱搞)
来源:互联网 发布:js 短路求值 编辑:程序博客网 时间:2024/06/16 14:35
题目链接:http://codeforces.com/problemset/problem/732/D点击打开链接
Vasiliy has an exam period which will continue for n days. He has to pass exams on m subjects. Subjects are numbered from 1 to m.
About every day we know exam for which one of m subjects can be passed on that day. Perhaps, some day you can't pass any exam. It is not allowed to pass more than one exam on any day.
On each day Vasiliy can either pass the exam of that day (it takes the whole day) or prepare all day for some exam or have a rest.
About each subject Vasiliy know a number ai — the number of days he should prepare to pass the exam number i. Vasiliy can switch subjects while preparing for exams, it is not necessary to prepare continuously during ai days for the exam number i. He can mix the order of preparation for exams in any way.
Your task is to determine the minimum number of days in which Vasiliy can pass all exams, or determine that it is impossible. Each exam should be passed exactly one time.
The first line contains two integers n and m (1 ≤ n, m ≤ 105) — the number of days in the exam period and the number of subjects.
The second line contains n integers d1, d2, ..., dn (0 ≤ di ≤ m), where di is the number of subject, the exam of which can be passed on the day number i. If di equals 0, it is not allowed to pass any exams on the day number i.
The third line contains m positive integers a1, a2, ..., am (1 ≤ ai ≤ 105), where ai is the number of days that are needed to prepare before passing the exam on the subject i.
Print one integer — the minimum number of days in which Vasiliy can pass all exams. If it is impossible, print -1.
7 20 1 0 2 1 0 22 1
5
10 30 0 1 2 3 0 2 0 1 21 1 4
9
5 11 1 1 1 15
-1
In the first example Vasiliy can behave as follows. On the first and the second day he can prepare for the exam number 1 and pass it on the fifth day, prepare for the exam number 2 on the third day and pass it on the fourth day.
In the second example Vasiliy should prepare for the exam number 3 during the first four days and pass it on the fifth day. Then on the sixth day he should prepare for the exam number 2 and then pass it on the seventh day. After that he needs to prepare for the exam number 1 on the eighth day and pass it on the ninth day.
In the third example Vasiliy can't pass the only exam because he hasn't anough time to prepare for it.
考试 一天只能考一科 每一科都需要时间准备 问能通过所有科目的最短时间
我们只需要记录 能够完成通过所有科目 这一情况即可
我们直接看满足题意的情况而不去想中间怎么考试的过程
而满足这一情况必须具备两个
1.已经过了足够多的天数将所有科目都复习完了 (注意需要加上考试时间 考试也占一天)
2.所有的可行考试科目都出现过
这样 我们在所有考试科目都出现过之后 再记录那些有考试的天数
计算复习完所有科目需要的天数 然后二分找最近的考试天数 就能找到通过所有考试的那一天
但是这样思考有一个漏洞
如果出现的考试时间 根本没办法复习完 就不能算这个考试时间出现过
因此我们还需记录数据所给的每一天的信息 当给完每一科需要复习的天数后 再判断哪些考试时间是可行的
依照上面的条件判断二分查找答案
#include <bits/stdc++.h>using namespace std;set<long long int > s;set<long long int > day;set<long long int > ::iterator it;long long int num[111111];long long int numm[111111];int main(){ long long int n,m; long long int sum=0; scanf("%lld%lld",&n,&m); for(long long int i=1;i<=n;i++) { long long int mid; scanf("%lld",&mid); num[i]=mid; } for(long long int i=1;i<=m;i++) { long long int mid; scanf("%lld",&mid); numm[i]=mid; sum+=mid; } for(long long int i=1;i<=n;i++) { long long int mid=num[i]; if(mid==0||i<=numm[mid]) continue; s.insert(mid); if(s.size()==m) day.insert(i); } sum+=(m); it=lower_bound(day.begin(),day.end(),sum); if(it==day.end()) printf("-1"); else printf("%lld",*(it));}
- CodeForces 732D Exams (思维+set乱搞)
- codeforces 732D Exams
- Codeforces 732D-Exams
- Codeforces 732D. Exams
- CodeForces 732D Exams
- codeforces 732D Exams
- CodeForces 732D Exams
- CodeForces 732D Exams
- CodeForces 732D Exams
- Codeforces Round #377(Div. 2)D.Exams(二分+思维)
- CodeForces 732 D.Exams(二分+贪心)
- codeforces-732D-Exams(二分)
- 【37.50%】【codeforces 732D】Exams
- CodeForces 732D - Exams(二分)
- CodeForces-732D-Exams(二分 模拟 贪心)
- codeforces 732D Exams(贪心+二分)
- codeforces 732D Exams 二分搜索 贪心
- Codeforces 732D Exams【二分+贪心】
- JVM内存初学 堆、栈、方法区
- 【JZOJ5317】【清华集训2017模拟8.19】func
- webservice接口的生成与调用
- File存储
- USB驱动--设备驱动程序
- CodeForces 732D Exams (思维+set乱搞)
- union all和union区别
- HTML表格 关于行和列的问题
- doxygen+graphviz 绘制工程的结构图技巧小结
- centos php5.6环境缺少dom extension
- oracle的分析函数简单应用和与聚合函数的区别
- python之list,tuble,dict和set
- npm更新至最新版本
- linux-基础命令