codeforces 732D
1 second
256 megabytes
standard input
standard output
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 2
0 1 0 2 1 0 2
2 1
5
10 3
0 0 1 2 3 0 2 0 1 2
1 1 4
9
5 1
1 1 1 1 1
5
-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.
思路就是二分加贪心,还是不会做,找了大神的代码,仔细研读了一下,受益匪浅,做了一点注释,就发上方便以后自己查阅
错误地地方请指正
#include <bits/stdc++.h>
using namespace std;
const int Maxn=100100;
int A[Maxn];
int B[Maxn];
vector<int>v[Maxn];
int N,M; bool f(int i){
vector<pair<int,int> >t;//pair里存的是点的位置,和值
for(int j = 1; j <= M; j++){
int p = upper_bound(v[j].begin(),v[j].end(),i) - v[j].begin() -1;//寻找该点里的最大值,就是队列v[j]队列里面最大值的位置,用来判断前面的天数是否可以够他复习
if( p == -1 ){
return false;//如果找不到,那么这个mid的值不满足
}
t.push_back( {v[j][p],B[j]} );//放进一个新的队列
}
sort(t.begin(),t.end());//排序的目的是按照点的位置排序,目的是贪心原理
int k = -1;//k的意思是到这次考试时候,化肥这次考试前面(队列顺序)考试复习需要的总天数
for(auto i:t){//遍历队列中的所有值,
if( i.second+k >= i.first ){//如果总天数+这次需要的天数,比安排那天考试的天数(即该点顺序)大,就返回假,否则累加
return false;
}else{
k += i.second+1;
}
}
return true;
} int main(){
cin >> N >> M;
for(int i = 0; i < N; i++){
cin >> A[i];//输入该点的值
v[A[i]].push_back(i);//存放该值的位置
}
for(int i = 1; i <= M; i++){
cin >> B[i];//输入要复习多少天
}
int l = -1;
int r = N + 1;
while( l < r-1 ){//二分,直到找到一个区间(1,mid)满足条件
int mid = (l+r)>>1;
if( f(mid) ){
r = mid;
}else{
l = mid;
}
}
if( r != N+1 ){
cout << r+1 << endl;
}else{
cout << -1 << endl;
}
}
codeforces 732D的更多相关文章
- codeforces 732D(二分)
题目链接:http://codeforces.com/contest/732/problem/D 题意:有m门需要过的课程,n天的时间可以选择复习.考试(如果的d[i]为0则只能复习),一门课至少要复 ...
- Codeforces 732D [二分 ][贪心]
/* 不要低头,不要放弃,不要气馁,不要慌张 题意: n天进行m科考试,每科考试需要a的复习时间,n天每天最多可以考一科.并且指定哪天考哪科. 注意考试那天不能复习. 问最少需要多少天可全部通过考试. ...
- CodeForces 732D Exams (二分)
题意:某人要考试,有n天考m个科目,然后有m个科目要考试的时间和要复习多少天才能做,问你他最早考完所有科目是什么时间. 析:二分答案,然后在判断时,直接就是倒着判,很明显后出来的优先,也就是一个栈. ...
- CodeForces 732D Exams
D. Exams time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...
- 【37.50%】【codeforces 732D】Exams
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- Codeforces Round #377 (Div. 2)A,B,C,D【二分】
PS:这一场真的是上分场,只要手速快就行.然而在自己做的时候不用翻译软件,看题非常吃力非常慢,还有给队友讲D题如何判断的时候又犯了一个毛病,一定要心平气和,比赛也要保证,不要用翻译软件做题: Code ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
随机推荐
- c#简单的Json解析类
使用方法: 引用Newtonsoft.Json.dll文件,然后引用命名空间using Newtonsoft.Json.Linq;JsonDome中有实例,照做就行 现在贴上示例代码 using Ne ...
- js 中对象属性特性2
对象的存储描述: get 和 set 方法 <script> var obj ={ get age(){ return 22 }, set age(value){ console. ...
- REST接口规范
参考文章 这篇文章使用不同的method代表不同操作 http://www.cnblogs.com/tommyli/p/3913018.html 实际应用中(我们过去的应用) 则是直接使用url来代表 ...
- typedef与define基本使用
参考: typedef用法 http://www.cnblogs.com/ggjucheng/archive/2011/12/27/2303238.html#define用法:http://blog. ...
- 介绍.NET Core
在connect (),我们宣布.NET 核心将能完全释放,作为开放源码软件.我也答应在.NET 核心跟更多的细节.在这篇文章,我将提供.NET 核心,我们如何去释放它,它涉及到.NET 框架,如何和 ...
- Instruments --- 内存泄露
虽然iOS 5.0版本之后加入了ARC机制,由于相互引用关系比较复杂时,内存泄露还是可能存在.所以了解原理很重要. 这里讲述在没有ARC的情况下,如何使用Instruments来查找程序中的内存泄露, ...
- LightOj_1287 Where to Run
题目链接 题意: 有n个街口和m条街道, 你后边跟着警察,你需要进行大逃亡(又是大爱的抢银行啊),在每个街口你都有≥1个选择, 1)停留在原地5分钟. 2)如果这个街口可以到xi这个街口, 并且, 通 ...
- TIPSO--基于JQUERY的消息提示框插件,用起来蛮顺手
项目产品经理要求, 呵呵,关于描述,十个字以内的,直接显示,多于十个字的,用消息框提示: 相关模板及JS如下: $(function() { $('.tip').tipso({ useTitle: f ...
- windows远程桌面连接配置
我的电脑 -> 属性 -> 远程 把两个checkbox勾上 运行(win + r) -> 输入secpol.msc回车 -> 找到本地策略 -> 安全选项 ->账 ...
- [wikioi]线段覆盖 2
http://wikioi.com/problem/3027/ # 有个小错误调了半天,最终发现sort(line, line+N)错了,后面那个是exclusive的,所以要line+N+1.# 按 ...