CodeForces 732D Exams (二分)
题意:某人要考试,有n天考m个科目,然后有m个科目要考试的时间和要复习多少天才能做,问你他最早考完所有科目是什么时间。
析:二分答案,然后在判断时,直接就是倒着判,很明显后出来的优先,也就是一个栈。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#define debug() puts("++++");
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
const int dr[] = {-1, 1, 0, 0};
const int dc[] = {0, 0, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
int a[maxn], b[maxn];
int ok[maxn], cnt[maxn]; bool judge(int mid){
memset(ok, -1, sizeof ok);
int idx = 0, num = 0;
for(int i = mid; i > 0; --i){
if(a[i] && ok[a[i]] == -1) cnt[++idx] = a[i], ++ok[a[i]], ++num;
else{
while(idx && !b[cnt[idx]]) --idx;
if(!idx) continue;
++ok[cnt[idx]];
if(ok[cnt[idx]] == b[cnt[idx]]) --idx;
}
} return num == m && !idx;
} int solve(){
int l = m, r = n+1;
while(l < r){
int mid = (l + r) >> 1;
if(judge(mid)) r = mid;
else l = mid + 1;
} return l == n+1 ? -1 : l;
} int main(){
while(scanf("%d %d", &n, &m) == 2){
for(int i = 1; i <= n; ++i) scanf("%d", a+i);
a[n+1] = 0;
b[0] = -1;
for(int i = 1; i <= m; ++i) scanf("%d", b+i);
printf("%d\n", solve());
}
return 0;
}
CodeForces 732D Exams (二分)的更多相关文章
- codeforces 732D(二分)
题目链接:http://codeforces.com/contest/732/problem/D 题意:有m门需要过的课程,n天的时间可以选择复习.考试(如果的d[i]为0则只能复习),一门课至少要复 ...
- CodeForces 732D Exams
D. Exams time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...
- codeforces 1165F1/F2 二分好题
Codeforces 1165F1/F2 二分好题 传送门:https://codeforces.com/contest/1165/problem/F2 题意: 有n种物品,你对于第i个物品,你需要买 ...
- 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 ...
- 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 ...
- 【37.50%】【codeforces 732D】Exams
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- Codeforces 732D [二分 ][贪心]
/* 不要低头,不要放弃,不要气馁,不要慌张 题意: n天进行m科考试,每科考试需要a的复习时间,n天每天最多可以考一科.并且指定哪天考哪科. 注意考试那天不能复习. 问最少需要多少天可全部通过考试. ...
- CF732D. Exams[二分答案 贪心]
D. Exams time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...
- CodeForces 359D (数论+二分+ST算法)
题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=47319 题目大意:给定一个序列,要求确定一个子序列,①使得该子序 ...
随机推荐
- abstract的method是否可同时是static,是否可同时是native,是否可同时是synchronized?
abstract的method不可以是static的,因为抽象的方法是要被子类实现的,而static与子类扯不上关系! native方法表示该方法要用另外一种依赖平台的编程语言实现的,不存在着被子类实 ...
- WinScp上传和下载
不多说,贴代码,看不懂得可以留言.需要引入WinSCP public class WebWinScp { //远程上传路径 private SessionOptions sessionOptions ...
- Smack IQ包的扩展
前几天一直很烦躁,怎么扩展smack的IQ包堵了我好久,今天静下心来看了下smack的源码,把这个问题解决了.下面给出步骤: 如果我们要扩展一个如下所示的IQ包: <iq id="00 ...
- nginx反向代理的简单配置
有两台机器A和B. A上边是nginx,B上边是tomcat. 现在要通过A的反向代理功能,通过A的nginx访问到B的tomcat. 首先tomcat已经配置好,并且正确启动,可访问. ...
- 推荐 15 个 Angular.js 应用扩展指令(参考应用)
几天前我们看到Angular 1.4.0发布了-一个以社团为驱动的发布版本涵盖了400多个GitHub的提交,增加了对特性的提升,比如动画,以及可用性. 官方新闻发布稿 覆盖了绝大部分,这同样值得放于 ...
- ASP.NET MVC 教程汇总
自学MVC看这里——全网最全ASP.NET MVC 教程汇总 MVC架构已深得人心,微软也不甘落后,推出了Asp.net MVC.小编特意整理博客园乃至整个网络最具价值的MVC技术原创文章,为想要 ...
- HDU 5783 Divide the Sequence
Divide the Sequence Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- [Everyday Mathematics]20150127
设 $f,g:[a,b]\to [0,\infty)$ 连续, 单调递增, 并且 $$\bex \int_a^x \sqrt{f(t)}\rd t\leq \int_a^x \sqrt{g(t)}\r ...
- 将VLC库封装为duilib的万能视频播放控件
转载请说明出处,谢谢~~ 昨天封装好了基于webkit的浏览器控件,修复了duilib的浏览器功能的不足,而我的仿酷狗播放器项目中不光需要浏览器,同时也需要视频播放功能,也就是完成MV的功能.所以我打 ...
- 精妙SQL语句 基础
精妙SQL语句SQL语句先前写的时候,很容易把一些特殊的用法忘记,我特此整理了一下SQL语句操作,方便自己写SQL时方便一点,想贴上来,一起看看,同时希望大家能共同多多提意见,也给我留一些更好的佳句, ...