【codeforces 732D】【二分+贪心】

来源:互联网 发布:王奕霏编程 编辑:程序博客网 时间:2024/05/29 07:20

传送门:D. Exams

描述:


D. Exams
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 20 1 0 2 1 0 22 1
output
5
input
10 30 0 1 2 3 0 2 0 1 21 1 4
output
9
input
5 11 1 1 1 15
output
-1
Note

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.


题意:

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


思路:

贪心,对于一个固定的天数,要判断能不能在期间通过所有考试,首先应该优先满足靠后的考试,因为复习的时间不一定要连续,因此靠后的考试能够有更多的复习时间,所以就从给定的最后一天向前遍历。最后二分一下找出最少天数就好了。


复杂度:

O(nlogn)


代码:

#include <bits/stdc++.h>using  namespace  std;#define mst(ss,b) memset(ss,b,sizeof(ss));#define rep(i,k,n) for(int i=k;i<=n;i++)#define rrep(i,k,n) for(int i=k;i>=n;i--)template<class T> void read(T&num) {    char CH; bool F=false;    for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());    for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());    F && (num=-num);}int stk[70], tp;template<class T> inline void print(T p) {    if(!p) { puts("0"); return; }    while(p) stk[++ tp] = p%10, p/=10;    while(tp) putchar(stk[tp--] + '0');    putchar('\n');}const int inf=0x3f3f3f;const int N=1e5+10;int n, m;int d[N], a[N], flag[N];bool check(int x){mst(flag, 0);int cur = x - 1;rrep(i, x, 1){cur = min(cur, i-1);if(d[i] && !flag[d[i]] && a[d[i]] <= cur){cur -= a[d[i]] + 1;  flag[d[i]] = 1;}}rep(i, 1, m)if(!flag[i])return false;return true;}int  main(){read(n), read(m);rep(i, 1, n)read(d[i]);rep(i, 1, m)read(a[i]);int r = n, l = 1, ans = inf;while(l<=r){int m = (l + r) >> 1;if(check(m))r = m - 1, ans = m;else l = m + 1;}if(ans == inf)puts("-1");else print(ans);  return 0;}



0 0