这题可以用二分答案来做

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

答案当然是满足了单调性。 假设用\(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. ASP.NET MVC中使用MvcPager异步分页+在分页中复选框下一页上一页也保持选中

    ASP.NET MVC 分页使用的是作者杨涛的MvcPager分页控件  地址:http://www.webdiyer.com/mvcpager/demos/ajaxpaging/ 这个分页控件在里面 ...

  2. 从0使用Ruby on Rails打造企业级RESTful API项目实战之我的云音乐

    本节对我们项目实现的功能和知识点做一个简单的介绍,因为是RESTful API项目,所以对于后端来说基本上没有什么UI界面可展示,那我们就在关键的点,使用客户端(Android)实现的效果图. 课程简 ...

  3. Flutter学习笔记(26)--返回拦截WillPopScope,实现1秒内点击两次返回按钮退出程序

    如需转载,请注明出处:Flutter学习笔记(26)--返回拦截WillPopScope,实现1秒内点击两次返回按钮退出程序 在实际开发中,为了防止用户误触返回按钮导致程序退出,通常会设置为在1秒内连 ...

  4. Test Case:: 12C ASMCMD New feature (Doc ID 1589249.1)

    Test Case:: 12C ASMCMD New feature (Doc ID 1589249.1) APPLIES TO: Oracle Database - Enterprise Editi ...

  5. Spring学习的第一天

    Spring是以Ioc和Aop为内核,提供了表现层spring MVC 和持久层Spring JDBC等众多应用技术,还能整合开源世界众多著名的第三方框架和类库,成为使用最多的JavaEE企业应用开源 ...

  6. 11 K-Means 原理及案例

    11 K-Means 原理及案例 非监督学习 unsupervised learning (非监督学习) ,只有特征值,没有目标值 聚类: 主要方法 - k-means (K - 需要分成的类别数) ...

  7. qt 自定义控件窗口提升

  8. javascript中的定时器入门

    JavaScript提供定时器(timer)的功能,可以延期执行或重复执行函数或代码段. window对象提供了三个方法来实现定时器的效果,分别是setTimeout().setInternal()和 ...

  9. Wpf Dispatcher.BeginInvoke((Action)delegate{}));

    <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/w ...

  10. mesos-slave启动不起来

    刚开始时候的状态 后来装了docker后