这题可以用二分答案来做

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

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

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

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

代码如下

  1. #include <cmath>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <iostream>
  5. #include <algorithm>
  6. using namespace std;
  7. const int A = 1e5 + 11;
  8. const int B = 1e6 + 11;
  9. inline int read() {
  10. char c = getchar(); int x = 0, f = 1;
  11. for( ; !isdigit(c); c = getchar()) if(c == '-') f = -1;
  12. for( ; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + (c ^ 48);
  13. return x * f;
  14. }
  15. int n, m, all, cnt;
  16. int d[A], w[A], a[A], la[A];
  17. inline bool check(int x) {
  18. memset(la, 0, sizeof(la));
  19. for(int i = 1; i <= n; i++) a[i] = d[i];
  20. for(int i = 1; i <= x; i++) if(a[i]) a[la[a[i]]] = 0, la[a[i]] = i;
  21. int tl = 0, cnt = 0;
  22. for(int i = 1; i <= x; i++) {
  23. if(a[i]) { tl -= w[a[i]]; if(tl < 0) return 0; else cnt++; }
  24. else tl++;
  25. }
  26. return cnt == m;
  27. }
  28. int main() {
  29. n = read(), m = read();
  30. if(n < m) return puts("-1"), 0;
  31. for(int i = 1; i <= n; i++) d[i] = read();
  32. for(int i = 1; i <= m; i++) w[i] = read(), all += w[i];
  33. if(all > n) return puts("-1"), 0;
  34. int l = 0, r = n, ans = -1;
  35. while(l <= r) {
  36. int mid = (l + r) >> 1;
  37. if(check(mid)) ans = mid, r = mid - 1;
  38. else l = mid + 1;
  39. }
  40. return cout << ans << '\n', 0;
  41. }

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. ES6-promise对象的使用

    Promise 的含义(摘自阮一峰ES6ru) Promise 是异步编程的一种解决方案,比传统的解决方案——回调函数和事件——更合理和更强大.它由社区最早提出和实现,ES6 将其写进了语言标准,统一 ...

  2. 入职小白随笔之Android四大组件——内容提供器详解(Content Provider)

    Content Provider 内容提供器简介 内容提供器(Content Provider)主要用于在不同的应用程序之间 实现数据共享的功能,它提供了一套完整的机制,允许一个程序访问另一个程序中的 ...

  3. springboot配置文件(一)

    一.YAML语法 1.基本语法 k 空格 v 表示一对键值对(必须有空格),以空格的缩进来控制层级关系,只要是左对齐的一列数据,都表示同一个层级.属性和值大小写敏感 server: port: 808 ...

  4. 12-Factor与微服务

           为了构建分布式微服务程序,能够部署到所有云服务,Heroku工程师总结所有云原生应用程序的12要素: 1.基准代码 Single codebase: The application mu ...

  5. Kafka 的No kafka server to stop报错处理

    使用kafka-server-stop.sh命令关闭kafka服务,发现无法删除,报错如下图No kafka server to stop 下面修改kafka-server-stop.sh将 PIDS ...

  6. navicat for mysql 连接 mysql 出现Client does not support authentication protocol requested by server解决方案

    一 .桌面左下角windows图标--搜索框内输入cmd,结果如图所示,点击cmd.exe,或者使用快捷键Windows键(在键盘上有个Windows标志的按键)+R输入cmd后回车. 二. 在出来的 ...

  7. HttpClient发起Http/Https请求工具类

    <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcl ...

  8. C++ Debug 模式下程序崩溃: Expression: is_block_type_valid(header->block_use)

    出现这样的错误,可能有很多种原因,而我出现崩溃的原因是由于代码中定义了vector容器, 未对它进行初始化操作导致的, 只要对它的大小进行初始化操作就行了 崩溃代码:  vector<Rect& ...

  9. vue使用--vuex快速学习与使用

    什么是vuex? Vuex核心概念 Vuex安装与使用 1.安装 2.目录结构与vuex引入 3.store中变量的定义.管理.派生(getter) 4.vuex辅助函数的使用说明 Vuex刷新数据丢 ...

  10. C++编译器优化技术:RVO、NRVO和复制省略

    现代编译器缺省会使用RVO(return value optimization,返回值优化).NRVO(named return value optimization.命名返回值优化)和复制省略(Co ...