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.


题意:n天要通过m科考试,d[i]每天可以考哪门科目,a[i]每门科目需要的准备时间,一天可以考试也可以准备,求最少时间


可以二分最少时间,然后贪心判断每门课能不能通过

判断时当然每门科考的越晚越好

//
// main.cpp
// cf732d
//
// Created by Candy on 01/11/2016.
// Copyright © 2016 Candy. All rights reserved.
// #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N=1e5+;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int n,m,d[N];
struct subject{
int id,late,a;
bool operator <(const subject &r)const{return late<r.late;}
}s[N];
bool check(int t){//printf("mid %d\n",t);
for(int i=;i<=m;i++) s[i].late=;
for(int i=;i<=t;i++) if(d[i]) s[d[i]].late=i;
sort(s+,s++m);
int cnt=,last=;
for(int i=;i<=m;i++){//printf("%d %d %d %d\n",i,s[i].id,s[i].late,cnt);
if(s[i].late==) return false;
cnt+=s[i].late-last-;
if(cnt<s[i].a) return false;
cnt-=s[i].a;
last=s[i].late;
}
return true;
}
int main(int argc, const char * argv[]) {
n=read();m=read();
for(int i=;i<=n;i++) d[i]=read();
for(int i=;i<=m;i++) s[i].a=read(),s[i].id=i;
int l=,r=n,ans=n+;
while(l<=r){
int mid=(l+r)/;
if(check(mid)) ans=min(ans,mid),r=mid-;
else l=mid+;
}
if(ans==n+) printf("-1");
else printf("%d",ans);
return ;
}

CF732D. Exams[二分答案 贪心]的更多相关文章

  1. BZOJ_2196_[Usaco2011 Mar]Brownie Slicing_二分答案+贪心

    BZOJ_2196_[Usaco2011 Mar]Brownie Slicing_二分答案+贪心 Description Bessie烘焙了一块巧克力蛋糕.这块蛋糕是由R*C(1 <= R,C ...

  2. 洛谷3933 Chtholly Nota Seniorious 二分答案+贪心

    题目链接 题意 给你一个N*M的矩阵 (N,M <=2000)  把他分成两部分 使两部分的极差较大的一个最小  求这个最小值.然后分矩阵的要求是:每个部分内部的方块之间,可以通过上下左右相互到 ...

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

  4. 【二分答案+贪心】UVa 1335 - Beijing Guards

    Beijing was once surrounded by four rings of city walls: the Forbidden City Wall, the Imperial City ...

  5. 【二分答案+贪心】解决“最小值最大”问题(UVa 12124 - Assemble)

    Problem A - Assemble Time limit: 2 seconds Recently your team noticed that the computer you use to p ...

  6. 【洛谷】【二分答案+贪心】P1316 丢瓶盖

    [题目描述:] 陶陶是个贪玩的孩子,他在地上丢了A个瓶盖,为了简化问题,我们可以当作这A个瓶盖丢在一条直线上,现在他想从这些瓶盖里找出B个,使得距离最近的2个距离最大,他想知道,最大可以到多少呢? [ ...

  7. BZOJ5321 JXOI2017加法(二分答案+贪心+堆+树状数组)

    二分答案后得到每个位置需要被加的次数.考虑贪心.从左到右考虑每个位置,将以该位置为左端点的区间按右端点从大到小加进堆.看该位置还需要被加多少次,如果不需要加了就不管,否则取堆顶区间将其选择,BIT实现 ...

  8. Gym 100886J Sockets 二分答案 + 贪心

    Description standard input/outputStatements Valera has only one electrical socket in his flat. He al ...

  9. bzoj 4310 跳蚤 —— 后缀数组+二分答案+贪心

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4310 二分答案——在本质不同的子串中二分答案! 如果二分到的子串位置是 st,考虑何时必须分 ...

随机推荐

  1. 生成树形结构的json字符串代码(c#)供前端angular tree使用.

    框架是使用EF6.0.可以针对返回的值使用Newtonsoft.Json.dll(百度搜一下)来对返回的值序列化为json字符串,如果对以下值那就是使用JsonConvert.SerializeObj ...

  2. div的显示和隐藏以及点击图标的更改

  3. 一个struts2登录bug的解决

    点登录的时候,在url后面总会加上一个;jsessionid=xxx 使找不到页面 的404 Bug ,百思不得其解,最后终于找到解决方案,实验最终成功解决了这个bug,下面是解决方案 1,增加依赖  ...

  4. [moka同学笔记]关于api-ms-win-crt-runtimel1-1-0.dll缺失

    在搞mongodb时候,碰到了这个坑.[api-ms-win-crt-runtimel1-1-0.dll缺失],懵逼不知道怎么改,从同事电脑里拷贝了这个文件,也按照网上的办法改了,改了半天还是不行.最 ...

  5. laravel 使用artisan命令新增数据库字段

    php artisan make:migration create_comments_table <?php use Illuminate\Database\Schema\Blueprint; ...

  6. 异常处理的解决方案 OneTrueError

    应用程序安装在用户计算机上,异常处理一直是反复出现的问题.用户报障中的描述不足以重现该问题.你不得不猜测,或者只是做猴子测试,以找出其异常出现的根源. 最严重的问题是当认为你已经找出了原因并纠正它,但 ...

  7. css中的各种单位简述以及ios10下safari禁止缩放的问题

    px:绝对单位,页面按精确像素展示 em:相对单位,基准点为父节点字体的大小,如果自身定义了font-size按自身来计算(浏览器默认字体是16px),整个页面内1em不是一个固定的值. rem:相对 ...

  8. JQuery实现一个简单的鼠标跟随提示效果

    效果体验:http://hovertree.com/texiao/jsstudy/2/ 实现思路 1 鼠标移入标题(这里是<a>标签) 创建一个div,div的内容为鼠标位置的文本 将创建 ...

  9. 奇妙的CSS之布局与定位

    前言 关于布局与定位是Web前端开发里非常基础而又重要的部分.介绍相关知识的文章,很容易就可以找到.虽然,这方面的知识点不是很多,但我们如果不弄清楚,在运用时候往往会出现预料之外的布局,这些“意外”有 ...

  10. 1-1 console的用法

    console里面具体提供了哪些方法可以供我们平时调试时使用. 目前控制台方法和属性有: ["$$", "$x", "dir", " ...