CF732D Exams

题目描述

Vasiliy has an exam period which will continue for \(n\) days. He has to pass exams on \(m\) subjects. Subjects are numbered from 1 to \(m\) .

About every day we know exam for which one of \(m\) subjects can be passed on that day. Perhaps, some day you can't pass any exam. It is not allowed to pass more than one exam on any day.

On each day Vasiliy can either pass the exam of that day (it takes the whole day) or prepare all day for some exam or have a rest.

About each subject Vasiliy know a number \(a_{i}\) — the number of days he should prepare to pass the exam number \(i\) . Vasiliy can switch subjects while preparing for exams, it is not necessary to prepare continuously during \(a_{i}\) days for the exam number \(i\) . He can mix the order of preparation for exams in any way.

Your task is to determine the minimum number of days in which Vasiliy can pass all exams, or determine that it is impossible. Each exam should be passed exactly one time.

输入格式

The first line contains two integers $ n$ and \(m\) ( \(1<=n,m<=10^{5}\) ) — the number of days in the exam period and the number of subjects.

The second line contains nn integers \(d_{1},d_{2},...,d_{n}\)​ ( \(0<=d_{i}<=m\) ), where \(d_{i}\) is the number of subject, the exam of which can be passed on the day number \(i\) . If \(d_{i}\)​ equals 0, it is not allowed to pass any exams on the day number \(i\) .

The third line contains \(m\) positive integers \(a_{1},a_{2},...,a_{m}\)​ ( \(1<=a_{i}<=10^{5}\) ), where \(a_{i}\)​ is the number of days that are needed to prepare before passing the exam on the subject \(i\) .

输出格式

Print one integer — the minimum number of days in which Vasiliy can pass all exams. If it is impossible, print -1.

题意翻译

一个人有m门科目需要考试,每一门科目需要a[i](1<=i<=m)的复习时间(复习时间可以不用连续),并且有一份n天的考试安排表,其中d[i]表示第i天能考第i门科目,(1<=i<=n)假如d[i]为0,就代表这一天没有任何科目的考试。试求这个人最少在第几天顺利通过所有考试?(注:这个人一天要么只能考试,要么就只能复习)

输入输出样例

输入 #1

7 2

0 1 0 2 1 0 2

2 1

输出 #1

5

输入 #2

10 3

0 0 1 2 3 0 2 0 1 2

1 1 4

输出 #2

9

输入 #3

5 1

1 1 1 1 1

5

输出 #3

-1

说明/提示

In the first example Vasiliy can behave as follows. On the first and the second day he can prepare for the exam number 1 and pass it on the fifth day, prepare for the exam number 2 on the third day and pass it on the fourth day.

In the second example Vasiliy should prepare for the exam number 3 during the first four days and pass it on the fifth day. Then on the sixth day he should prepare for the exam number 2 and then pass it on the seventh day. After that he needs to prepare for the exam number 1 on the eighth day and pass it on the ninth day.

In the third example Vasiliy can't pass the only exam because he hasn't anough time to prepare for it.

【思路】

二分答案 +贪心

【题目大意】

每一天你可以完成任务或者积攒能量

每一个任务的完成都需要消耗一定的能量

每一天都只能完成一个特定的任务或者不能完成任务

求最早什么时候完成全部的任务

【题目分析】

从题意中可以看出如果能够在第i天完成全部的任务

那么一定能够在第i+1天完成全部的任务

所以用二分答案就很显然了吧

【核心思路】

二分完成需要的天数

不过这里怎么判断某个天数到底行不行呢?

因为只需要判断行不行而不需要判断优不优

所以只需要知道能不能完成就可以了

不管完成的过程优不优

那就考虑最差的能够完成的情况

就是在规定时间内

每一个任务最后一次出现的时候能够把它完成掉就是可以的

所以在不是最后一次出现的情况下就囤积能量

在最后一次出现的地方就完成

如果完成不了那就是不行

到最后再检查一下一共m个任务是不是每一个任务都完成了

【完整代码】

#include<iostream>
#include<cstdio>
#include<cstring>
#define int long long
using namespace std; int read()
{
int sum = 0,fg = 1;
char c = getchar();
while(c < '0' || c > '9')
{
if(c == '-')fg = -1;
c = getchar();
}
while(c >= '0' && c <= '9')
{
sum = sum * 10 + c - '0';
c = getchar();
}
return sum * fg;
}
const int Max = 100005;
int d[Max];
int a[Max];
int t[Max];
int n,m;
bool check(int x)
{
memset(t,0,sizeof(t));
int tot = 0;
int acioi = 0;
for(register int i = 1;i <= x;++ i)
t[d[i]] ++;
for(register int i = 1;i <= x;i ++)
{
if(d[i] != 0)
{
t[d[i]] -= 1;
if(t[d[i]] == 0)
{
acioi ++;
tot -= a[d[i]];
if(tot < 0)
return false;
}
else
tot ++;
}
else
tot ++;
}
for(register int i = 1;i <= m;++ i)
if(t[i] != 0)
return false;
if(acioi == m)
return true;
else
return false;
} signed main()
{
// freopen("generals.in","r",stdin);
// freopen("generals.out","w",stdout);
n = read(),m = read();
for(register int i = 1;i <= n;++ i)
d[i] = read();
for(register int i = 1;i <= m;++ i)
a[i] = read();
int l = 1,r = n + 1;
while(l < r)
{
int mid = (l + r) >> 1;
if(check(mid))r = mid;
else l = mid + 1;
}
if(l == n + 1)
{
cout << -1 << endl;
return 0;
}
cout << l << endl;
return 0;
}

CF732D Exams 题解的更多相关文章

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

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

  2. CF732D Exams 二分 贪心

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

  3. CF732D Exams

    这题可以用二分答案来做 那么为什么可以用二分答案呢? 答案当然是满足了单调性. 假设用\(x\)天能够考完所有试,那么用大于$x $天必定也能够考完所有试,所以满足了单调性,我们就可以二分答案 那么如 ...

  4. 题解 CF492C Vanya and Exams

    CF492C Vanya and Exams 有了Pascal题解,来一波C++题解呀qwq.. 简单的贪心题 按b[i]从小到大排序,一个一个学科写直到达到要求即可 #include<cstd ...

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

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

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

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

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

  9. CodeForces 479C Exams 贪心

    题目: C. Exams time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

随机推荐

  1. JQuery入门篇

    JQuery入门篇 jQuery选择器 “$”表示JQuery对象 根据ID查找 $(‘#var’)表示将一个id值为var的DOM节点封装成一个jQuery对象,DOM节点必须以“#”开头. 例如: ...

  2. SpringBoot构建RESTful API

    1.RESTful介绍 RESTful是一种软件架构风格! RESTful架构风格规定,数据的元操作,即CRUD(create, read, update和delete,即数据的增删查改)操作,分别对 ...

  3. python爬虫---js加密和混淆,scrapy框架的使用.

    python爬虫---js加密和混淆,scrapy框架的使用. 一丶js加密和js混淆 js加密 ​ 对js源码进行加密,从而保护js代码不被黑客窃取.(一般加密和解密的方法都在前端) http:// ...

  4. iOS - 什么!iOS13 又获取不到WiFi了

    iOS 12 适配WiFi 增加隐私权限 https://www.cnblogs.com/baitongtong/p/10179519.html ios13又新增定位权限 别的不说,理解请看上篇文章 ...

  5. 5.Javascript闭包得实现原理和作用

    闭包的实现原理和作用 1.闭包的概念:指有权访问另一个函数作用域中的变量的函数,一般情况就是在一个函数中包含另一个函数. 2.闭包的作用:访问函数内部变量.保持函数在环境中一直存在,不会被垃圾回收机制 ...

  6. ASP.NET Core中返回 json 数据首字母大小写问题

    ASP.NET Core中返回 json 数据首字母大小写问题 在asp.net core中使用ajax请求动态绑定数据时遇到该问题 后台返回数据字段首字母为定义的大写,返回的数据没有问题 但是在前台 ...

  7. Beego 学习笔记12:文件的操作

    文件的操作 1>     此事例操作的是text文件 2>     文件的操作有读取text内容,将内容写入到文件中,删除文件,创建文件 3>     新建一个控制器,名为rwfil ...

  8. mongoDB看这篇就够了

    写在前面 hello,小伙伴们,我是 pubdreamcc ,本篇文章依旧出至于我的 GitHub仓库 node学习教程 ,如果你觉得我写的还不错,欢迎给个 star ,小伙伴们的 star 是我持续 ...

  9. Java 流程控制语句 之 顺序结构

    在一个程序执行的过程中,各条语句的执行顺序对程序的结果是有直接影响的.也就是说,程序的流程对运行结果 有直接的影响.所以,我们必须清楚每条语句的执行流程.而且,很多时候我们要通过控制语句的执行顺序来实 ...

  10. Java 流程控制 之 分支结构——条件判断语句

    一.判断语句 1.判断语句1-- 单 if 语句(单分支结构) 语法格式: if(条件表达式){ 语句体; }  执行流程: 首先判断条件表达式看其结果是 true 还是 false: 如果是 tru ...