题目链接:http://codeforces.com/contest/732/problem/D

题意:

在m天中要考k个课程,

数组a中有m个元素,表示第a[i]表示第i天可以进行哪门考试,若a[i]为0,则表示当天不能参加任何科目的考试,只能预习或者休息;

数组b中有k个元素,b[i]表示科目i需要复习几天才能通过;

问最快需要几天能通过所有考试,如果不能完成所有科目考试,输出-1;

思路:

用二分方法直接找满足条件的最小的数,其中数是指当前天是第几天,条件是指当前天以前是否能考完所有科目(包括当前天);

代码:

 #include <bits/stdc++.h>
#define MAXN 100000+10
using namespace std; int a[MAXN], b[MAXN], n, k; int gg(int x){ //××××××判断花费x天能否完成所有科目考试,即能否在第x天以前完成所有科目考试;
int cnt=, vis[MAXN];
memset(vis, , sizeof(vis));
for(int i=x; i>=; i--){
// cout << cnt << "//" << endl;
if(!a[i]){
cnt--;
cnt=max(cnt, );
}else if(!vis[a[i]]){
cnt+=b[a[i]-];
// cout << cnt << "***" << endl;
vis[a[i]]=;
if(cnt>i){
return ;
}
}else if(vis[a[i]]){
cnt--;
cnt=max(, cnt);
}
}
for(int i=; i<=k; i++){
if(!vis[i]){
return ;
}
}
return ;
} int main(void){
ios::sync_with_stdio(false), cin.tie(), cout.tie();
cin >> n >> k;
for(int i=; i<n; i++){
cin >> a[i];
}
for(int i=; i<k; i++){
cin >> b[i];
}
int l=, r=n;
while(l<r){ //××××××二分查找满足条件的最小数
int mid=(l+r)>>;
// cout << mid << " " << gg(mid) << endl;//****
if(gg(mid)){
r=mid;
}else{
l=mid+;
}
}
if(l>=n){
cout << "-1" << endl;
}else{
cout << l+ << endl;
}
return ;
}

Codeforces Round #377 (Div. 2)D(二分)的更多相关文章

  1. Codeforces Round #377 (Div. 2) D. Exams

    Codeforces Round #377 (Div. 2) D. Exams    题意:给你n个考试科目编号1~n以及他们所需要的复习时间ai;(复习时间不一定要连续的,可以分开,只要复习够ai天 ...

  2. Codeforces Round #404 (Div. 2) C 二分查找

    Codeforces Round #404 (Div. 2) 题意:对于 n and m (1 ≤ n, m ≤ 10^18)  找到 1) [n<= m] cout<<n; 2) ...

  3. Codeforces Round #377 (Div. 2)A,B,C,D【二分】

    PS:这一场真的是上分场,只要手速快就行.然而在自己做的时候不用翻译软件,看题非常吃力非常慢,还有给队友讲D题如何判断的时候又犯了一个毛病,一定要心平气和,比赛也要保证,不要用翻译软件做题: Code ...

  4. Codeforces Round #377 (Div. 2) D. Exams(二分答案)

    D. Exams Problem Description: Vasiliy has an exam period which will continue for n days. He has to p ...

  5. Codeforces Round #377 (Div. 2) D. Exams 二分

    D. Exams time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...

  6. Codeforces Round #377 (Div. 2) A B C D 水/贪心/贪心/二分

    A. Buy a Shovel time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  7. Codeforces Round #324 (Div. 2) C (二分)

    题目链接:http://codeforces.com/contest/734/problem/C 题意: 玩一个游戏,一开始升一级需要t秒时间,现在有a, b两种魔法,两种魔法分别有m1, m2种效果 ...

  8. Codeforces Round #377 (Div. 2) E. Sockets

    http://codeforces.com/contest/732/problem/E 题目说得很清楚,每个电脑去插一个插座,然后要刚好的,电脑的power和sockets的值相同才行. 如果不同,还 ...

  9. Codeforces Round #377 (Div. 2) D. Exams 贪心 + 简单模拟

    http://codeforces.com/contest/732/problem/D 这题我发现很多人用二分答案,但是是不用的. 我们统计一个数值all表示要准备考试的所有日子和.+m(这些时间用来 ...

随机推荐

  1. 如何修改windows远程端口

    Windows系统默认远程桌面端口是3389,修改方法: 远程登陆服务器,运行中使用"regedit"命令打开注册表编辑器,依次展开"HKEY_LOCAL_MACHINE ...

  2. FineUI第十三天---`列布局

    这是经典的列布局:                  <x:Panel runat=                     <Items>                      ...

  3. iOS开发——UI进阶篇(十七)CALayer,核心动画基本使用

    一.CALayer简介 1.CALayer在iOS中,文本输入框.一个图标等等,这些都是UIView你能看得见摸得着的东西基本上都是UIView,比如一个按钮.一个文本标签.一个其实UIView之所以 ...

  4. IE兼容性

    1.ie兼容css3 border-radius.box-shadow.text-shadow的方法 2.ie兼容性问题及解决方法 3.css Hack 一.ie兼容border-radius.box ...

  5. linux中用shell获取昨天、明天或多天前的日期

    linux中用shell获取昨天.明天或多天前的日期 时间 -- :: BlogJava-专家区 原文 http://www.blogjava.net/xzclog/archive/2015/12/0 ...

  6. Linux CAT与ECHO命令详解 <<EOF EOF

    Linux CAT与ECHO命令详解 cat命令是Linux下的一个文本输出命令,通常是用于观看某个文件的内容的: cat主要有三大功能: .一次显示整个文件. $ cat filename .从键盘 ...

  7. Intellij IDEA + Jrebel

    Jrebel java热部署神器! 如果你没用过这里一句白话说明,就是在修改了class等源文件后无需重启web容器(如Tomcat)直接生效! 找到以后 安装... 然后 激活地址 :http:// ...

  8. 【架构】How To Use HAProxy to Set Up MySQL Load Balancing

    How To Use HAProxy to Set Up MySQL Load Balancing Dec  2, 2013 MySQL, Scaling, Server Optimization U ...

  9. How do I get ASP.NET Web API to return JSON instead of XML using Chrome

    public static class WebApiConfig { public static void Register(HttpConfiguration config) { config.Ro ...

  10. [k]css盒模型

    box-sizing :  content-box || border-box || inherit 1.content-box:此值为其默认值.元素的宽度/高度(width/height)等于元素边 ...