Vasiliy has an exam period which will continue for n days. He has to pass exams onm subjects. Subjects are numbered from 1 tom.
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 numberi. Vasiliy can switch subjects while preparing for exams, it is not necessary to prepare continuously duringai days for the exam numberi. 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.
InputThe first line contains two integers n andm (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), wheredi is the number of subject, the exam of which can be passed on the day numberi. If di equals 0, it is not allowed to pass any exams on the day numberi.
The third line contains m positive integersa1,?a2,?...,?am (1?≤?ai?≤?105), where ai is the number of days that are needed to prepare before passing the exam on the subjecti.
OutputPrint one integer — the minimum number of days in which Vasiliy can pass all exams. If it is impossible, print-1.
ExamplesInput7 20 1 0 2 1 0 22 1Output5Input10 30 0 1 2 3 0 2 0 1 21 1 4Output9Input5 11 1 1 1 15Output-1NoteIn 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.
題目大意:
一共有N天(按照時間順序給出),其中有M科目需要通過。
每個科目需要復習的天數是ai.
對應給出di為時間表,di==0的,可以選擇復習任意一科,di!=0的,可以選擇考di這一科,也可以選擇復習任意一科。
問你能否通過所有考試,如果可以,輸出最小天數。
思路:
1、對于天數來講,時間越長,越有可能通過所有考試,那么這里就包含了一個單調性,我們可以二分天數,然后進行貪心處理。
2、對于當前枚舉的天數mid.
我們要進行貪心判斷:
①首先在從第一天到第mid天之內,必須要有di包含所有的科目,如果有一些科目沒有出現過,那么這些科目是不能進行考試的,所以這種情況是不行的。
②貪心的去想,我們肯定考試越晚越好,所以我們取每一科最后出現的位子作為考這一科的時間。
③那么其他時間都是可以進行任意復習的時間,我們過程維護可以進行復習的天數還有多少天have.當遇到一個考試時間點,那么對應have-=需要復習這科的時間,如果have出現了負數的情況,那么對應增大時間,否則全程沒遇到,就可以相應的減少時間了。
Ac代碼:
#include<stdio.h>#include<string.h>#include<queue>using namespace std;int a[100050];int ned[100050];int last[100050];int n,m;int Slove(int mid){ int have=0; memset(last,-1,sizeof(last)); for(int i=1;i<=mid;i++) { if(a[i]!=0) { last[a[i]]=i; } } for(int i=1;i<=m;i++)if(last[i]==-1)return 0; for(int i=1;i<=mid;i++) { if(a[i]==0)have++; else { if(last[a[i]]==i) { if(have>=ned[a[i]]) { have-=ned[a[i]]; } else return 0; } else have++; } } return 1;}int main(){ while(~scanf("%d%d",&n,&m)) { for(int i=1;i<=n;i++)scanf("%d",&a[i]); for(int i=1;i<=m;i++)scanf("%d",&ned[i]); int ans=-1; int l=1; int r=n; while(r-l>=0) { int mid=(l+r)/2; if(Slove(mid)==1) { ans=mid; r=mid-1; } else l=mid+1; } printf("%d/n",ans); }}
新聞熱點
疑難解答