题意:某人要考试,有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 (二分)的更多相关文章

  1. codeforces 732D(二分)

    题目链接:http://codeforces.com/contest/732/problem/D 题意:有m门需要过的课程,n天的时间可以选择复习.考试(如果的d[i]为0则只能复习),一门课至少要复 ...

  2. CodeForces 732D Exams

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

  3. codeforces 1165F1/F2 二分好题

    Codeforces 1165F1/F2 二分好题 传送门:https://codeforces.com/contest/1165/problem/F2 题意: 有n种物品,你对于第i个物品,你需要买 ...

  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. 【37.50%】【codeforces 732D】Exams

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  7. Codeforces 732D [二分 ][贪心]

    /* 不要低头,不要放弃,不要气馁,不要慌张 题意: n天进行m科考试,每科考试需要a的复习时间,n天每天最多可以考一科.并且指定哪天考哪科. 注意考试那天不能复习. 问最少需要多少天可全部通过考试. ...

  8. CF732D. Exams[二分答案 贪心]

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

  9. CodeForces 359D (数论+二分+ST算法)

    题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=47319 题目大意:给定一个序列,要求确定一个子序列,①使得该子序 ...

随机推荐

  1. bzoj1063

    仔细观察可以发现,这个规划路径很像树链剖分 树链剖分的经典定理:任意一个点到根的所经过轻边不超过logn 而这个规划路径所走公路相当于轻边,也就是说,不便利度不会很大 那么直接dp即可,设f[x,i, ...

  2. SQL SERVER 2008筛选时报错 无法为该请求检索数据

    使用SqlServer2008的筛选功能时报错“无法为该请求检索数据. (Microsoft.SqlServer.Management.Sdk.Sfc)” 如下图: 解决方法: 打上SQL SERVE ...

  3. jQuery的datatable怎么才能给某一列添加超链接?

    aocolumeDef.这个里面去定义.return返回的字符串会代替原来cell里面的内容 e.g: datatable=$('#dt_basic').dataTable({ "bAuto ...

  4. 【MySQL】Java对SQL时间类型的操作(获得当前、昨天、前年。。时间)

    Java获得当前时间 java.util.Date date = new java.util.Date(); Timestamp time = new Timestamp(date.getTime() ...

  5. validator的验证

    通常喜欢这么写验证 <form method="post" data-ajax="false" action="/Shppping/PlaceO ...

  6. 强制将IE8设置为IE7兼容模式来解析网页

    强制将IE8设置为IE7兼容模式来解析网页 英文原文:http://msdn.microsoft.com/en-us/library/cc288325(VS.85).aspx 文件兼容性用于定义让IE ...

  7. javascript一些有用但又不常用的特性

    1.onclick="save();return false;" 取消“浏览器默认行为”.     比如一个链接     <a href="http://zhida ...

  8. sharepoint 2010 在aspx 写lambda 时错误

    在sharepoint 2010 中,写lambda时,遇到错误.在aspx里面,写lambda表达式, 运行时报错,就不明道理了.经过百般测试,终于找到方法: 错误提示: "/" ...

  9. css3制作优惠券

    <div class="demo-container demo"><style> .demo{width:410px;} .stamp *{padding: ...

  10. mysql教程-触发器

    触发器 1. mysql触发器 情景说明 情景设置,如图,当我们点击了购买,将会发生什么? 现有如下两张表 商品表 编号(id)名称(name)价格(price)库存(stock) 1F2战斗机100 ...