这题可以用二分答案来做

那么为什么可以用二分答案呢?

答案当然是满足了单调性。 假设用\(x\)天能够考完所有试,那么用大于$x $天必定也能够考完所有试,所以满足了单调性,我们就可以二分答案

那么如何\(check\)呢?考虑一下贪心

贪心思路:在二分的\(mid\)天之前找到每一科考试可以考的最后一天,只在这一天去考这一门科目,其它时间积攒复习时间,若在\(mid\)前这个科目可考的最后一天出现了,而此时积攒的复习时间并不足以考过这门科目,则说明用\(mid\)天不能考完这些科目,否则就让计数器\(cnt\)的值加一,表示现在已经考了\(cnt\)门,最后检验一下\(cnt\)是否等于\(m\),若不等于则说明还有科目在\(mid\)天前没有出现,增大范围,否则缩小范围,让\(ans\)等于\(mid\)。

代码如下

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; const int A = 1e5 + 11;
const int B = 1e6 + 11; inline int read() {
char c = getchar(); int x = 0, f = 1;
for( ; !isdigit(c); c = getchar()) if(c == '-') f = -1;
for( ; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + (c ^ 48);
return x * f;
} int n, m, all, cnt;
int d[A], w[A], a[A], la[A]; inline bool check(int x) {
memset(la, 0, sizeof(la));
for(int i = 1; i <= n; i++) a[i] = d[i];
for(int i = 1; i <= x; i++) if(a[i]) a[la[a[i]]] = 0, la[a[i]] = i;
int tl = 0, cnt = 0;
for(int i = 1; i <= x; i++) {
if(a[i]) { tl -= w[a[i]]; if(tl < 0) return 0; else cnt++; }
else tl++;
}
return cnt == m;
} int main() {
n = read(), m = read();
if(n < m) return puts("-1"), 0;
for(int i = 1; i <= n; i++) d[i] = read();
for(int i = 1; i <= m; i++) w[i] = read(), all += w[i];
if(all > n) return puts("-1"), 0;
int l = 0, r = n, ans = -1;
while(l <= r) {
int mid = (l + r) >> 1;
if(check(mid)) ans = mid, r = mid - 1;
else l = mid + 1;
}
return cout << ans << '\n', 0;
}

CF732D Exams的更多相关文章

  1. CF732D Exams 题解

    CF732D Exams 题目描述 Vasiliy has an exam period which will continue for \(n\) days. He has to pass exam ...

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

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

  3. CF732D Exams 二分 贪心

    思路:二分+贪心 提交次数:10次以上 错因:刚开始以为二分(边界,$+1or-1$)写错了,调了半天,后来才发现是$ck()$写错了.开始只判了最后是否小于零,而应该中间一旦小于零就$return\ ...

  4. 【CF732D】Exams(线性扫描,贪心,二分)

    题意:有m门需要过的课程,n天的时间可以选择复习.考试(如果的d[i]为0则只能复习),一门课至少要复习a[i]天才能通过(可以不连续的复习得到a[i]),问最早什么时候可以把所有课程都通过,如果不能 ...

  5. 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 ...

  6. codeforces 480A A. Exams(贪心)

    题目链接: A. Exams time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  7. 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 ...

  8. Codeforces Round #280 (Div. 2) C. Vanya and Exams 贪心

    C. Vanya and Exams Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/492/pr ...

  9. Codeforces Round #274 (Div. 1) A. Exams 贪心

    A. Exams Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/480/problem/A Des ...

随机推荐

  1. Python如何运行程序

    人生苦短,我用Python. 作为一个开发人员,如何写代码是必须要知道的,代码如何运行也是有必要了解的.通过了解代码如何运行,可指导我们写出效率更高的代码.下午看了<Python学习手册> ...

  2. iOS中UICollectionView添加头视图

    参考链接:https://www.jianshu.com/p/ef57199bf34a 找了一堆的博客,写的都少了很重要的一步. //引入头部视图 -(UICollectionReusableView ...

  3. 安装完dlib以及face_recognition,但是在python IDE中无法导入的问题

    在dlib以及face_recognition通过pip安装成功之后(如何通过pip进行安装,在博主的上一篇随笔中有提及),在pycharm中无法导入. 在综合完网上的各种说法之后,发现了原因,在下边 ...

  4. 08. Go 语言包(package)

    Go 语言包(package) Go 语言的源码复用建立在包(package)基础之上.Go 语言的入口 main() 函数所在的包(package)叫 main,main 包想要引用别的代码,必须同 ...

  5. 洛谷 SP263 Period

    洛谷 SP263 Period 题目描述 For each prefix of a given string S* with N* characters (each character has an ...

  6. js 运算的内置函数

    // 一.Math.round()作用:四舍五入返回整数.(返回参数+0.5后,向下取整) // Math.round(5.57) //返回6 // Math.round(2.4) //返回2 // ...

  7. ESP8266源码分析--打印的基本用法

    缘由: 物联网小芯片中,很多都不是特别复杂,绝大多数问题都需要靠串口打印就能定位的.所以,串口打印是一个非常靠谱的玩意.一定要好好的利用.ESP8266的打印模板设计的特别好,这里我就列出来,供大家参 ...

  8. IT兄弟连 HTML5教程 CSS3揭秘 CSS3属性2

    3  背景属性 在CSS3中提供了多个背景属性,这里只介绍两个比较常用的属性,其他属性可以从手册中获取帮助.在CSS3中,通过background-image或者background属性可以为一个容器 ...

  9. 损失函数--KL散度与交叉熵

    损失函数 在逻辑回归建立过程中,我们需要一个关于模型参数的可导函数,并且它能够以某种方式衡量模型的效果.这种函数称为损失函数(loss function). 损失函数越小,则模型的预测效果越优.所以我 ...

  10. SpringBoot2.0 整合 JWT 框架,解决Token跨域验证问题

    本文源码:GitHub·点这里 || GitEE·点这里 一.传统Session认证 1.认证过程 1.用户向服务器发送用户名和密码. 2.服务器验证后在当前对话(session)保存相关数据. 3. ...