Codeforces Round #377 (Div. 2)
来源:互联网 发布:王者趋势彩带指标源码 编辑:程序博客网 时间:2024/06/05 07:12
D. Exams
题目http://codeforces.com/problemset/problem/732/D
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
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.
Input
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.
Output
Print one integer — the minimum number of days in which Vasiliy can pass all exams. If it is impossible, print -1.
Examples
Input
7 2
0 1 0 2 1 0 2
2 1
Output
5
Input
10 3
0 0 1 2 3 0 2 0 1 2
1 1 4
Output
9
Input
5 1
1 1 1 1 1
5
Output
-1
用二分查找确定需要的天数
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#define fo1(i,n) for(int i = 1; i <= n; ++i)#define fo0(i,n) for(int i = 0; i < n; ++i)using namespace std;const int LEN = 1e5+4;int n,m;int d[LEN];int e[LEN];int flag[LEN];bool check(int ans){ memset(flag,0,sizeof(flag)); int res = 0;//用来标记需要的时间 int exam = m;//用来标记考试的数目 for(int i = ans; i >= 1; --i)//关键是从后往前找 { if(d[i]&&!flag[d[i]]) { res += e[d[i]]; flag[d[i]]= 1; --exam; } else { if(res>0) --res; } } if(res>0||exam>0) return false; else return true;}int main(void){ cin>>n>>m; fo1(i,n) scanf("%d",&d[i]); fo1(i,m) scanf("%d",&e[i]); int Left = 0,Right = n; int Mid; while(Right>Left) { Mid = Left+(Right-Left)/2; if(check(Mid)) Right = Mid; else Left = Mid+1; } if(check(Right)) cout<<Right<<endl; else cout<<-1<<endl; return 0;}
- Codeforces Round #377 (Div. 2)
- Codeforces Round #377 (Div. 2)
- Codeforces Round #377 (Div. 2)
- CF-Codeforces Round #377 (Div. 2)
- Codeforces Round #377 (Div. 2)-D. Exams
- Codeforces Round #377 (Div. 2)题解报告
- Codeforces Round #377 (Div. 2) D. Exams
- Codeforces Round #377 (Div. 2) ABCDEF 题解
- Codeforces Round #377 (Div. 2) F
- Codeforces Round #377 (Div. 2) C. Sanatorium
- Codeforces Round #377 (Div. 2)D - Exams
- Codeforces Round #377 (Div. 2) C Sanatorium
- Codeforces Round #377 (Div. 2) D Exams
- Codeforces Round #377 (Div. 2) C 模拟
- Codeforces Round #377 (Div. 2) D 二分
- Codeforces Round #377 (Div. 2) Sanatorium
- Codeforces Round #377 (Div. 2)(A+B)
- Codeforces Round #377 (Div. 2) 解题报告
- JNI总管:JNIEnv
- 强联通分量——学习笔记
- 你还在用错误的方法编写倒排索引吗?
- Spring boot redis的使用(一)
- 数据结构实验之查找五:平方之哈希表
- Codeforces Round #377 (Div. 2)
- Js实现将选择框的内容传递到另一个选择框中
- HDU-1856-More is better
- 并行模式与算法
- Andrew Ng机器学习课程笔记–week1(机器学习简介&线性回归模型)
- I
- linux tail 过滤日志文件中的关键字
- ZZULIOJ【1071】分解质因子【数学】&&【函数】
- BZOJ 1124 [POI2008] 枪战Maf 乱搞。。