codeforces 732D Exams

来源:互联网 发布:怎么样做淘宝服装模特 编辑:程序博客网 时间:2024/05/16 01:31
Description
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.


Sample Input
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
Hint
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.


二分+贪心+树状数组


对结束的天进行二分,验证过程中用树状数组优化。


pre[i]表示第i科的考试时间在mid前最靠后的位置。


read(i)表示mid前至第i天空余的可以用来学习的天数(用树状数组维护)。


先对pre[i]靠前的处理,如果read(i)<a[i]+1,则mid不符合。


代码:


#include <cstdio>#include <iostream>#include <cstring>#include <string>#include <cstdlib>#include <algorithm>#include <cmath>#include <string>using namespace std;const int maxn=1e5+10;int day[maxn],a[maxn];int pre[maxn];int tre[maxn];int rr[maxn];const int cmp(const int i,const int j){return pre[i]<pre[j];}int n,m;int lb(int x){return x&-x;}void add(int v,int pos){for(int i=pos;i<=n;i+=lb(i))tre[i]+=v;}int read(int x){int sum=0;for(int i=x;i;i-=lb(i))sum+=tre[i];return sum;}int main(){ios::sync_with_stdio(false);cin>>n>>m;for(int i=1;i<=n;i++)cin>>day[i];for(int i=1;i<=m;i++)cin>>a[i];int l=1,r=n+1;while(l<r){int mid=(l+r)/2;bool ok=1;memset(pre,0,sizeof(pre));memset(tre,0,sizeof(tre));for(int i=1;i<=mid;i++)if(day[i]!=0)pre[day[i]]=i;for(int i=1;i<=mid;i++)add(1,i);for(int i=1;i<=m;i++)rr[i]=i;sort(rr+1,rr+1+m,cmp);for(int i=1;i<=m;i++){int e=rr[i];if(pre[e]==0){ok=0;break;}int len=read(pre[e]);if(a[e]+1>len){ok=0;break;}add(-a[e]-1,pre[e]);}if(ok)r=mid;else l=mid+1;}if(l==n+1)cout<<-1;else cout<<l;return 0;}


0 0
原创粉丝点击