Codeforces Round #377 (Div. 2)

D. Exams
题目http://codeforces.com/problemset/problem/732/D
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 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


用二分查找确定需要的天数

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define fo1(i,n) for(int i = 1; i <= n; ++i)
#define fo0(i,n) for(int i = 0; i < n; ++i)
using namespace std;
const int LEN = 1e5+4;
int n,m;
int d[LEN];
int e[LEN];
int flag[LEN];
bool check(int ans)
{
   memset(flag,0,sizeof(flag));
   int res = 0;//用来标记需要的时间
   int exam = m;//用来标记考试的数目
   for(int i = ans; i >= 1; --i)//关键是从后往前找
   {
       if(d[i]&&!flag[d[i]])
       {
           res += e[d[i]];
           flag[d[i]]= 1;
           --exam;
       }
       else
       {
           if(res>0)
           --res;
       }
   }
   if(res>0||exam>0)
    return false;
   else
    return true;
}
int main(void)
{

    cin>>n>>m;
    fo1(i,n)
    scanf("%d",&d[i]);
    fo1(i,m)
    scanf("%d",&e[i]);
    int Left = 0,Right = n;
    int Mid;
    while(Right>Left)
    {
        Mid = Left+(Right-Left)/2;
        if(check(Mid))
         Right = Mid;
        else
         Left = Mid+1;
    }
    if(check(Right))
     cout<<Right<<endl;
    else
     cout<<-1<<endl;

    return 0;
}
全部评论

相关推荐

10-11 17:45
门头沟学院 Java
走吗:别怕 我以前也是这么认为 虽然一面就挂 但是颇有收获!
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
11-27 10:46
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务