CF732D Exams
这题可以用二分答案来做
那么为什么可以用二分答案呢?
答案当然是满足了单调性。 假设用\(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的更多相关文章
- CF732D Exams 题解
CF732D Exams 题目描述 Vasiliy has an exam period which will continue for \(n\) days. He has to pass exam ...
- CF732D. Exams[二分答案 贪心]
D. Exams time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...
- CF732D Exams 二分 贪心
思路:二分+贪心 提交次数:10次以上 错因:刚开始以为二分(边界,$+1or-1$)写错了,调了半天,后来才发现是$ck()$写错了.开始只判了最后是否小于零,而应该中间一旦小于零就$return\ ...
- 【CF732D】Exams(线性扫描,贪心,二分)
题意:有m门需要过的课程,n天的时间可以选择复习.考试(如果的d[i]为0则只能复习),一门课至少要复习a[i]天才能通过(可以不连续的复习得到a[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 480A A. Exams(贪心)
题目链接: A. Exams time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- 批发市场收记账管理系统(iPad与手机版)水产批发市场客户欠账、还款管理水产宝介绍 第八章 财务(应收账款,应付账款,已收账款,已付账款)
1.财务 ① 财务模块主功能(收支记账,记账类别,应收账款,应付账款,支付方式管理,账期管理) ② 支付设置 a 系统内置支付方式有6种 b 新增支付方式 新增支付方式 主要上传支付方式图标. ...
- 入职小白随笔之Android四大组件——广播详解(broadcast)
Broadcast 广播机制简介 Android中的广播主要可以分为两种类型:标准广播和有序广播. 标准广播:是一种完全异步执行的广播,在广播发出之后,所有的广播接收器几乎都会在同一时刻接收到这条广播 ...
- 解决idea中mysql连接失败Could not create connection to database server. Attempted reconnect 3 times. Giving up.
原因是少一个参数,设置时区的. 解决方法: 加一个参数: serverTimezone=UTC jdbc:mysql://localhost:3306/SshProject?useUnicode=t ...
- diango中有三种response
from django.shortcuts import render, redirect, HttpResponse HttpResponse() render() redirect()
- Linux系统学习 九、日志、命令、身份鉴别、目录、文件查看、控制台终端、文件属性
一.配置静态IP地址 输入ifconfig后没有配置IP地址,接下来进行手动配置. 输入以下命令进入IP配置文件进行配置 原始内容 进入vi后,输入i进入编辑状态,编辑完成后,按esc键退出编辑状 ...
- 批量修改含空格的文件名「Linux」
1.问题:文件批量重命名和处理文件名中的空格 如果文件名中有空格,在执行以下shell脚本的时候会出错. shell 脚本 for filename in `ls` do echo $filename ...
- adb devices无法连接mumu模拟器
解决方案: 如果你的android环境能够直接访问 adb 的相关指令.只需要把mumu模拟器打开 然后打开cmd -> 输入 adb connect 127.0.0.1:7555 就能直接连上 ...
- 【pat】algorithm常用函数整理
reference is_permutation Test whether range is permutation of another Parameters first1, last1 Input ...
- shiro实战(2)--ssm
一.web.xml的配置 <?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi=& ...
- 记一次Tomcat启动报错Failed to start component [StandardEngine[Catalina].Standard
今天启动项目的时候,发现tomcat一直报错,之前都一直没有问题的啊,提示 org.apache.catalina.LifecycleException: Failed to start ...