Exams
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.
分析:二分答案,然后考试按从后往前模拟check即可;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define llinf 0x3f3f3f3f3f3f3f3fLL
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<ll,int>
#define Lson L, mid, ls[rt]
#define Rson mid+1, R, rs[rt]
#define sys system("pause")
const int maxn=1e5+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
inline ll read()
{
ll x=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,k,t,cnt[maxn],ok[maxn],op[maxn],now,ca,ans;
bool panduan(int p)
{
queue<int>q;
set<int>w;
for(int i=p;i>=;i--)
{
if(ok[i])
{
if(w.find(ok[i])==w.end()&&!cnt[ok[i]])
{
q.push(ok[i]);
w.insert(ok[i]);
}
else
{
if(!w.empty())
{
if(++cnt[q.front()]==op[q.front()])
{
now++;
w.erase(q.front());
q.pop();
}
}
}
}
else
{
if(!w.empty())
{
if(++cnt[q.front()]==op[q.front()])
{
now++;
w.erase(q.front());
q.pop();
}
}
}
}
return now==m;
}
int main()
{
int i,j;
ans=-;
scanf("%d%d",&n,&m);
rep(i,,n)scanf("%d",&ok[i]);
rep(i,,m)scanf("%d",&op[i]);
int l=,r=n;
while(l<=r)
{
int mid=(l+r)>>;
now=;
memset(cnt,,sizeof(cnt));
if(panduan(mid))
{
ans=mid,r=mid-;
}
else l=mid+;
}
printf("%d\n",ans);
//system("Pause");
return ;
}
Exams的更多相关文章
- CF732D. Exams[二分答案 贪心]
		
D. Exams time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...
 - 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 ...
 - cf492C Vanya and Exams
		
C. Vanya and Exams time limit per test 1 second memory limit per test 256 megabytes input standard i ...
 - cf479C Exams
		
C. Exams time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...
 - ural  1091. Tmutarakan Exams(容斥原理)
		
1091. Tmutarakan Exams Time limit: 1.0 secondMemory limit: 64 MB University of New Tmutarakan trains ...
 
随机推荐
- 【Python】生成器、回溯和八皇后问题
			
八皇后问题: 把N个皇后,放在N*N的棋盘上面,从第一行往下放,每个皇后占一行,同时,每个皇后不能处在同一列,对角线上,有多少种放置方法. 思路: 典型的回溯问题: 1.当要放置最后一个皇后时候,默认 ...
 - Linux shell 操作 postgresql,并设置crontab任务
			
Linux shell 操作 postgresql:删除间隔日期的数据-删除指定日期的数据-vacuumdb 清理数据库 -清理日志 -定期执行脚本 *修改pg_hba.conf 设置本地连接无密码, ...
 - navicat 连接远程mysql
			
01putty中session---远程地址(程序访问的域名) ,端口22--load加载进来 02SSH--Auth-Tunnels(隧道)-- putty端口映射SSH--Auth-Tunnels ...
 - 7 个 jQuery 最佳实践
			
前言 随着富网络应用(rich web applications)数量的增长,以及用户对快速交互响应的高期望,开发者开始使用JavaScript库来快速高效的完成一些重复性的工作.这其中最流行的Jav ...
 - 分享到QQ空间、新浪微博、腾讯微博和人人网
			
function shareys(type, url, title, img, content) { switch (type) { case "sina": url = &quo ...
 - 图片拉伸:resizableImageWithCapInsets
			
iOS 5.0 在iOS 5.0中,UIImage有一个新方法可以处理图片的拉伸问题 - (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)ca ...
 - 多线程---优先级&yield方法
			
优先级只有10级,1-10.最高10(java中用Thread.MAX_PRIORITY),最低1,中间级5. 设置优先级的方法是 线程对象.setPriority(5): yield : 暂停(不是 ...
 - if语句—交互程序二
			
参考:<笨方法学Python>—习题31 用了两个嵌套的if语句! # coding: utf-8 print u"欢迎来到玩家国度, 你需要根据提示完成闯关!" # ...
 - Computed Observable的参数
			
构造计算监控(Constructing a computed observable) 1. ko.computed( evaluator [, targetObject, options] ) eva ...
 - 1.0 Python 学习网站
			
w3cschool : http://www.runoob.com/python/python-tutorial.html cnblog Python 从入门到精通: http://www.cnbl ...