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\) .ios
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.spa
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 \(a_{i}\) — 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 \(a_{i}\) days for the exam number \(i\) . He can mix the order of preparation for exams in any way.rest
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.code
The first line contains two integers $ n$ and \(m\) ( \(1<=n,m<=10^{5}\) ) — the number of days in the exam period and the number of subjects.ci
The second line contains nn integers \(d_{1},d_{2},...,d_{n}\) ( \(0<=d_{i}<=m\) ), where \(d_{i}\) is the number of subject, the exam of which can be passed on the day number \(i\) . If \(d_{i}\) equals 0, it is not allowed to pass any exams on the day number \(i\) .get
The third line contains \(m\) positive integers \(a_{1},a_{2},...,a_{m}\) ( \(1<=a_{i}<=10^{5}\) ), where \(a_{i}\) is the number of days that are needed to prepare before passing the exam on the subject \(i\) .string
Print one integer — the minimum number of days in which Vasiliy can pass all exams. If it is impossible, print -1.it
一我的有m門科目須要考試,每一門科目須要a[i](1<=i<=m)的複習時間(複習時間能夠不用連續),而且有一份n天的考試安排表,其中d[i]表示第i天能考第i門科目,(1<=i<=n)假如d[i]爲0,就表明這一天沒有任何科目的考試。試求這我的最少在第幾天順利經過全部考試?(注:這我的一天要麼只能考試,要麼就只能複習)io
輸入 #1
7 2
0 1 0 2 1 0 2
2 1
輸出 #1
5
輸入 #2
10 3
0 0 1 2 3 0 2 0 1 2
1 1 4
輸出 #2
9
輸入 #3
5 1
1 1 1 1 1
5
輸出 #3
-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.
二分答案 +貪心
每一天你能夠完成任務或者積攢能量
每個任務的完成都須要消耗必定的能量
每一天都只能完成一個特定的任務或者不能完成任務
求最先何時完成所有的任務
從題意中能夠看出若是可以在第i天完成所有的任務
那麼必定可以在第i+1天完成所有的任務
因此用二分答案就很顯然了吧
二分完成須要的天數
不過這裏怎麼判斷某個天數到底行不行呢?
由於只須要判斷行不行而不須要判斷優不優
因此只須要知道能不能完成就能夠了
無論完成的過程優不優
那就考慮最差的可以完成的狀況
就是在規定時間內
每個任務最後一次出現的時候可以把它完成掉就是能夠的
因此在不是最後一次出現的狀況下就囤積能量
在最後一次出現的地方就完成
若是完成不了那就是不行
到最後再檢查一下一共m個任務是否是每個任務都完成了
#include<iostream> #include<cstdio> #include<cstring> #define int long long using namespace std; int read() { int sum = 0,fg = 1; char c = getchar(); while(c < '0' || c > '9') { if(c == '-')fg = -1; c = getchar(); } while(c >= '0' && c <= '9') { sum = sum * 10 + c - '0'; c = getchar(); } return sum * fg; } const int Max = 100005; int d[Max]; int a[Max]; int t[Max]; int n,m; bool check(int x) { memset(t,0,sizeof(t)); int tot = 0; int acioi = 0; for(register int i = 1;i <= x;++ i) t[d[i]] ++; for(register int i = 1;i <= x;i ++) { if(d[i] != 0) { t[d[i]] -= 1; if(t[d[i]] == 0) { acioi ++; tot -= a[d[i]]; if(tot < 0) return false; } else tot ++; } else tot ++; } for(register int i = 1;i <= m;++ i) if(t[i] != 0) return false; if(acioi == m) return true; else return false; } signed main() { // freopen("generals.in","r",stdin); // freopen("generals.out","w",stdout); n = read(),m = read(); for(register int i = 1;i <= n;++ i) d[i] = read(); for(register int i = 1;i <= m;++ i) a[i] = read(); int l = 1,r = n + 1; while(l < r) { int mid = (l + r) >> 1; if(check(mid))r = mid; else l = mid + 1; } if(l == n + 1) { cout << -1 << endl; return 0; } cout << l << endl; return 0; }