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

题意:每天可以准备任意一个科目或者通过一个科目,最少在第几天完成,全部科目;

思路:二分答案,o(n)check(我的zz mlog(m));

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define esp 0.00000000001
const int N=1e5+,M=1e6+,inf=1e9;
int d[N];
int a[N];
int n,m;
vector<int>v[N];
int deep[N];
int flag[N];
int check(int x)
{
for(int i=;i<=m;i++)
{
if(v[i].size()==)
return ;
int pos=upper_bound(v[i].begin(),v[i].end(),x)-v[i].begin();
if(pos==)
return ;
deep[i]=v[i][pos-];
}
for(int i=;i<=m;i++)
flag[deep[i]]=;
int st=;
for(int i=;i<=x;i++)
{
if(flag[i])
{
st-=a[d[i]];
if(st<)
return ;
}
else
st++;
}
return ;
}
int main() {
scanf("%d%d",&n,&m);
for(int i = ; i <= n; ++i) scanf("%d",&d[i]),v[d[i]].push_back(i);
for(int i = ; i <= m; ++i) scanf("%d",&a[i]);
int st=;
int en=n;
if(check(n)==)
return puts("-1");
for(int i=;i<=m;i++)
flag[deep[i]]=;
int ans=-;
while(st<=en)
{
int mid=(st+en)>>;
if(check(mid)) en=mid-,ans=mid;
else st=mid+;
for(int i=;i<=m;i++)
flag[deep[i]]=;
}
printf("%d\n",ans);
return ;
}

Codeforces Round #377 (Div. 2) D. Exams 二分的更多相关文章

  1. Codeforces Round #377 (Div. 2) D. Exams(二分答案)

    D. Exams Problem Description: Vasiliy has an exam period which will continue for n days. He has to p ...

  2. Codeforces Round #377 (Div. 2) D. Exams

    Codeforces Round #377 (Div. 2) D. Exams    题意:给你n个考试科目编号1~n以及他们所需要的复习时间ai;(复习时间不一定要连续的,可以分开,只要复习够ai天 ...

  3. Codeforces Round #377 (Div. 2) D. Exams 贪心 + 简单模拟

    http://codeforces.com/contest/732/problem/D 这题我发现很多人用二分答案,但是是不用的. 我们统计一个数值all表示要准备考试的所有日子和.+m(这些时间用来 ...

  4. Codeforces Round #377 (Div. 2)D(二分)

    题目链接:http://codeforces.com/contest/732/problem/D 题意: 在m天中要考k个课程, 数组a中有m个元素,表示第a[i]表示第i天可以进行哪门考试,若a[i ...

  5. Codeforces Round #377 (Div. 2)A,B,C,D【二分】

    PS:这一场真的是上分场,只要手速快就行.然而在自己做的时候不用翻译软件,看题非常吃力非常慢,还有给队友讲D题如何判断的时候又犯了一个毛病,一定要心平气和,比赛也要保证,不要用翻译软件做题: Code ...

  6. Codeforces Round #377 (Div. 2) A B C D 水/贪心/贪心/二分

    A. Buy a Shovel time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  7. Codeforces Round #274 (Div. 1) A. Exams 贪心

    A. Exams Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/480/problem/A Des ...

  8. Codeforces Round #543 (Div. 2) F dp + 二分 + 字符串哈希

    https://codeforces.com/contest/1121/problem/F 题意 给你一个有n(<=5000)个字符的串,有两种压缩字符的方法: 1. 压缩单一字符,代价为a 2 ...

  9. Codeforces Round #377 (Div. 2) E. Sockets

    http://codeforces.com/contest/732/problem/E 题目说得很清楚,每个电脑去插一个插座,然后要刚好的,电脑的power和sockets的值相同才行. 如果不同,还 ...

随机推荐

  1. innodb的锁到底占用多少内存

    举个简单的例子: CREATE TABLE `sample` ( `i` ) unsigned NOT NULL auto_increment, `j` ) default NULL, PRIMARY ...

  2. Hibernate,JPA注解@ManyToMany_JoinTable

    可以通过@ManyToMany注解可定义的多对多关联.同时,也需要通过注解@JoinTable描述关联表和关联条件.如果是双向关联,其中一段必须定义为owner,另一端必须定义为inverse(在对关 ...

  3. 13、mysql/触发器

    1. mysql mysql基础 1)mysql存储结构: 数据库 -> 表 -> 数据   sql语句 2)管理数据库: 增加: create database 数据库 default ...

  4. Integer Inquiry -TJU1112

    作为最简单的高精度加法,要注意的是如下几点, 第一,因为是数位达到上百位的大数,所以只能用字符串数组来存贮. 第二,为了方便之后的相加操作,应该把字符串数组逆序转化为一个整型数组. 第三,在控制进位的 ...

  5. extjs 中动态给gridpanel 复选框赋值

    最近在搞extjs时需要动态根据数据给gridpanel的复选框赋值 网上看了很多 ,多不行,最后找到一个好使的方法 如下: RBACformPanel.getSelectionModel().sel ...

  6. hdu4940 Destroy Transportation system(2014多校联合第七场)

    题意很容易转化到这样的问题:在一个强连通的有向图D中是否存在这样的集合划分S + T = D,从S到T集合的边权大于从T到S集合的边权. 即D(i, j)  > B(j, i) + D(j, i ...

  7. Number Sequence 分类: HDU 2015-06-19 20:54 10人阅读 评论(0) 收藏

    Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...

  8. Spring测试工具返回Application

    package pmisf.webservice.util; import javax.servlet.ServletContextEvent; import javax.servlet.Servle ...

  9. Date日期比对

    Calendar calendar1 = Calendar.getInstance(); Calendar calendar2 = Calendar.getInstance(); calendar1. ...

  10. PHP中的XML解析的5种方法

    [前言]不管是桌面软件开发,还是WEB应用,XML无处不在!然而在平时的工作中,仅仅是使用一些已经封装好的类对XML对于处理,包括生成,解析等.假期有空,于是将PHP中的几种XML解析方法总结如下: ...