Codeforces Round #377 (Div. 2)D. Exams(二分/贪心)

传送门

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

7 2
0 1 0 2 1 0 2
2 1

10 3
0 0 1 2 3 0 2 0 1 2
1 1 4

5 1
1 1 1 1 1
5

Sample Output

5

9

-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.

思路

题解:

二分天数,查找可能的答案,然后贪心的判断是否能完成所有的考试。从尾遍历判断,这样才能最大保证在此情况能不能通过所有的考试。

 

#include<bits/stdc++.h>
using namespace std;
const int maxn = 100005;
int a[maxn],d[maxn],book[maxn];
int n,m;

bool OK(int x)
{
    int sum = 0,cnt = 0;
    memset(book,0,sizeof(book));
    for (int i = x;i > 0;i--)
    {
        if (d[i] && !book[d[i]])
        {
            book[d[i]] = 1;
            cnt++;
            sum += a[d[i]];
        }
        else if (sum > 0)   sum--;
    }
    if (cnt < m)    return false;
    else if (sum > 0)   return false;
    else    return true;
}


int main()
{
    bool flag = false;
    scanf("%d%d",&n,&m);
    for (int i = 1;i <= n;i++)   scanf("%d",&d[i]);
    for (int i = 1;i <= m;i++)   scanf("%d",&a[i]);
    int left = 0,right = maxn;
    while (left < right)
    {
        int mid = left + ((right - left)>>1);
        if (OK(mid))
        {
            right = mid;
            flag = true;
        }
        else
        {
            left = mid + 1;
        }
    }
    if (flag)   printf("%d\n",right);
    else    printf("-1\n");
    return 0;
}

  

posted @ 2017-04-28 00:06  zxzhang  阅读(213)  评论(0编辑  收藏  举报