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 ...
随机推荐
- java 面向对象——进度1
面向对象:1,面向对象和面向过程思想. 面向对象强调的是对象实例. 面向过程强调的是动作. 对象将动作进行封装. 在问题领域中,我们先去找的都是涉及的对象, 然后 ...
- hbuider 中点击就显示出一个单选的列表 ,然后后台跨域向里面动态添加数据,注意里面的格式是json object
jsp页面: <li class="mui-table-view-cell" onclick="showActionSheet()"> <di ...
- 【python】【转】if else 和 elif
else和elif语句也可以叫做子句,因为它们不能独立使用,两者都是出现在if.for.while语句内部的.else子句可以增加一种选择:而elif子句则是需要检查更多条件时会被使用,与if和els ...
- zzuli oj 1167逆转数(指针专题)
Description 任意给你一个整数,这个数可能很大(最长不超过100位),你能求出它的逆转数吗? 逆转数定义如下: 1.一个末尾没有0的整数,它的逆转数就是各位数字逆序输出: 2.一个负数 ...
- 在ubuntu中获得root权限
在终端中输入:(1)sudo passwd rootEnter new UNIX password: (在这输入你的密码)Retype new UNIX password: (确定你输入的密码)pas ...
- C语言面向对象的简便方法
都知道C语言是面向过程的,但是现在软件规模越来越大,通过面向对象的方式可以简化开发.业余时间想了个简单的方法,在C中使用一部分面向对象的基本功能.由于C语言自身的限制,并不完善,只能将就用,聊胜于无, ...
- vs2010 使用SignalR 提高B2C商城用户体验(三)
vs2010 使用SignalR 提高B2C商城用户体验(三) 上一章节,我们的web即时通讯已经可以实现跨域了,但针对我们的需求,还希望,一些客户端程序可以和我们的web用户,在线聊天,所以到Sig ...
- mysql创建自定义函数与存储过程
mysql创建自定义函数与存储过程 一 创建自定义函数 在使用mysql的过程中,mysql自带的函数可能不能完成我们的业务需求,这时就需要自定义函数,例如笔者在开发过程中遇到下面这个问题 mysql ...
- apache asp.net
http://www.apache.org/dist/httpd/ http://server.it168.com/server/2005-10-11/20051011027201.shtml htt ...
- VC下载文件 + 显示进度条
在codeproject里找了许久,发现这样一个VC下载文件并显示进度条的源码,于是添加了些中文注释: 1.下载线程函数: UINT DownloadFile(LPVOID pParam) { CWn ...