Codeforces_732D_(二分贪心)
1 second
256 megabytes
standard input
standard output
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 ai — 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 ai 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 ≤ 105) — the number of days in the exam period and the number of subjects.
The second line contains n integers d1, d2, ..., dn (0 ≤ di ≤ m), where di is the number of subject, the exam of which can be passed on the day number i. If di equals 0, it is not allowed to pass any exams on the day number i.
The third line contains m positive integers a1, a2, ..., am (1 ≤ ai ≤ 105), where ai 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.
7 2
0 1 0 2 1 0 2
2 1
5
10 3
0 0 1 2 3 0 2 0 1 2
1 1 4
9
5 1
1 1 1 1 1
5
-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.
题意:在满足复习够天数的条件下每天可以通过指定的一科,要么选择通过一科,要么选择复习某一科,问最少几天能全部通过。(题目中不能看出每天只能复习一科,在NOTE中才能看出每天只能复习一科)
看CF上2400多人做出来,以为可以轻松做出来,结果硬是没想出怎么做,看了题解说是二分贪心。
对1—n天进行二分check,若check(mid)==1,l=mid+1;若check(mid)==0,r=mid-1。若能通过,则最终将接近最早通过的天数。check的思路是,从mid这天开始往前,让每一科在能通过的最晚的一天通过,再从头求每科复习的天数。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
#define N 100005 int day[N];
int cost[N];
int n,k;
bool check(int x)
{
int dt[N];
memset(dt,,sizeof(dt));
for(int i=x;i>;i--)
if(!dt[day[i]])
dt[day[i]]=i;
int pre=;
for(int i=;i<=x;i++)
{
if(day[i]==)
pre++;
else if(dt[day[i]]!=i)
pre++;
else
{
if(cost[day[i]]>pre)
return ;
else
pre-=cost[day[i]];
}
}
for(int i=;i<=k;i++)
if(dt[i]==)
return ;
return ;
} int main()
{
while(scanf("%d%d",&n,&k)!=EOF)
{
for(int i=; i<=n; i++)
scanf("%d",&day[i]);
for(int i=; i<=k; i++)
scanf("%d",&cost[i]);
int l=,r=n,mid,ans=-;
while(l<=r)
{
mid=(l+r)>>;
if(check(mid))
{
ans=mid;
r=mid-;
}
else
l=mid+;
}
printf("%d\n",ans);
}
return ;
}
Codeforces_732D_(二分贪心)的更多相关文章
- Codeforces Gym 100231B Intervals 线段树+二分+贪心
Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...
- 2016-2017 ACM-ICPC CHINA-Final Ice Cream Tower 二分+贪心
/** 题目:2016-2017 ACM-ICPC CHINA-Final Ice Cream Tower 链接:http://codeforces.com/gym/101194 题意:给n个木块,堆 ...
- 【bzoj2097】[Usaco2010 Dec]Exercise 奶牛健美操 二分+贪心
题目描述 Farmer John为了保持奶牛们的健康,让可怜的奶牛们不停在牧场之间 的小路上奔跑.这些奶牛的路径集合可以被表示成一个点集和一些连接 两个顶点的双向路,使得每对点之间恰好有一条简单路径. ...
- CF732D Exams 二分 贪心
思路:二分+贪心 提交次数:10次以上 错因:刚开始以为二分(边界,$+1or-1$)写错了,调了半天,后来才发现是$ck()$写错了.开始只判了最后是否小于零,而应该中间一旦小于零就$return\ ...
- $CF949D\ Curfew$ 二分/贪心
正解:二分/贪心 解题报告: 传送门$QwQ$ 首先这里是二分还是蛮显然的?考虑二分那个最大值,然后先保证一个老师是合法的再看另一个老师那里是否合法就成$QwQ$. 发现不太会搞这个合不合法的所以咕了 ...
- $bzoj2067\ szn$ 二分+贪心
正解:二分+贪心 解题报告: 传送门$QwQ$ 题目大意就说有一棵树,然后要用若干条线覆盖所有边且不能重叠.问最少要用几条线,在用线最少的前提下最长的线最短是多长. 昂首先最少用多少条线这个还是蛮$e ...
- leetcode1552题解【二分+贪心】
leetcode1552.两球之间的磁力 题目链接 算法 二分+贪心 时间复杂度O(nlogn + nlogm) 1.根据题意描述,我们需要将m个球放入到n个篮子中,根据题目中数据范围描述发现m &l ...
- Codeforces 732D [二分 ][贪心]
/* 不要低头,不要放弃,不要气馁,不要慌张 题意: n天进行m科考试,每科考试需要a的复习时间,n天每天最多可以考一科.并且指定哪天考哪科. 注意考试那天不能复习. 问最少需要多少天可全部通过考试. ...
- codeforces 613B B. Skills(枚举+二分+贪心)
题目链接: B. Skills time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
随机推荐
- HashMap源码分析3:移除
本文源码基于JDK1.8.0_45. final Node<K,V> removeNode(int hash, Object key, Object value, boolean matc ...
- 4、Java并发性和多线程-并发编程模型
以下内容转自http://ifeve.com/%E5%B9%B6%E5%8F%91%E7%BC%96%E7%A8%8B%E6%A8%A1%E5%9E%8B/: 并发系统可以采用多种并发编程模型来实现. ...
- ERROR 1045 (28000)
mysql登陆出现这个错误时,解决方法如下: 1. 修改mysql配置文件,增加 skip-grant-tables 如我的机器是linux 所以要修改/etc/mysql/my.cnf 在mysql ...
- ORACLE EM的删除与创建
手动删除ORACLE 10G EM 使用emca可以手动配置em! 配置em的过程中有一个环节要特别主要: 不论使用dbca还是使用emca -deconfig dbcontrol db -repos ...
- Effective C++ Item 44 将与參数无关的代码抽离 templates
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie 经验:Templates 生成多个 classes 和多个函数,所以不论什么 templat ...
- Codeforces Round #272 (Div. 2) D.Dreamoon and Sets 找规律
D. Dreamoon and Sets Dreamoon likes to play with sets, integers and . is defined as the largest p ...
- ASP.NET Overview
https://msdn.microsoft.com/en-us/library/4w3ex9c2.aspx ASP.NET is a unified统一的 Web development model ...
- 把ANSI格式的TXT文件批量转换成UTF-8文件类型
把ANSI格式的TXT文件批量转换成UTF-8文件类型 Posted on 2010-08-05 10:38 moss_tan_jun 阅读(3635) 评论(0) 编辑 收藏 #region 把AN ...
- perl数组的长度与元素个数
perl数组的长度与元素个数 $#数组名 ---表示数组中最后一个元素的下标,它等于元素个数减1. @数组名 ---表示数组中元素的个数. $标量=@数组名 ---将一个数组赋值给一个标量变量,标量得 ...
- CSS样式适配杂记
1.问:input框的对齐,制作类似百度搜索框的时候,发现IE下前面输入框和后面按钮总是不能对齐. 解答:给input框增加vertical-align:bottom; 2.问:IE下display: ...