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#]Task异步操作
1.代码示例 using System; using System.Threading; using System.Threading.Tasks; namespace ConsoleApplicat ...
- chgrp命令
chgrp命令用于变更文件或目录的所属群组. 在UNIX系统家族里,文件或目录权限的掌控以拥有者及所属群组来管理.您可以使用chgrp指令去变更文件与目录的所属群组,设置方式采用群组名称或群组识别码皆 ...
- osg(OpenSceneGraph)学习笔记1:智能指针osg::ref_ptr<>
OSG的智能指针,osg::ref_ptr<> osg::Referenced类管理引用计数内存块,osg::ref_ptr需要使用以它为基类的其它类作为模板参数. osg::ref_pt ...
- 国内外最全的asp.net开源项目 (转)
最近一些项目开始用到CMS系统,最开始是研究JAVA的,无奈国内JAVA的CMS开源系统还是比较少,最多最成熟的还是PHP的,当然现在.NET的也不少了,这里做一下汇总备忘,留待学习研究. 国内系统: ...
- delphi xe5 android 服务端和手机端的源码下载
xe5 android的服务端和手机客户端的源代码下载地址 http://files.cnblogs.com/nywh2008/AndroidTest.rar
- 开源网站管理工具—Altman
0×00前言 之前用过几款webshell工具,有B/C的也有C/S的,有的只能用于php或者aspx,当然个人用得最多.觉得用得舒服的也只有菜刀了. 但是毕竟菜刀是好几年之前的产物了,而且也已经停止 ...
- 【2011 Greater New York Regional 】Problem I :The Golden Ceiling
一道比较简单但是繁琐的三维计算几何,找错误找的我好心酸,没想到就把一个变量给写错了 = =: 题目的意思是求平面切长方体的截面面积+正方体顶部所遮盖的面积: 找出所有的切点,然后二维凸包一下直接算面积 ...
- 如何解决jquery版本冲突
<!-- 引入1.6.4版的jq --> <script src="<a href="http://ajax.googleapis.com/ajax/lib ...
- OneAPM Cloud Test——系统性能监控神器
2015 年 8 月,OneAPM 推出了一款系统性能监控产品--Cloud Test,产品上线以来以「两低一高」的特点迅速成为市场增长率最快的一匹黑马.「两低一高」,即低使用成本.低学习成本以及高服 ...
- CF_91B
题目意思是这样的:给定n个整数,求第i个数右边的距离它最远的比它小的数的下标之差然后再减1. 这里既然是需要知道距离该数最远的下标,可以从右至左扫描一遍,然后按照单调递减的顺序入栈,即只把比栈顶元素小 ...