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. ios 控件代码transform学习笔记

    1.图片设置(平移,缩放,旋转) 创建一个transform属性 //按钮点击时,只能执行一次向上旋转 //派 M_PI_4 45度旋转 . CGAffineTransform transforms= ...

  2. java中的String

    String作为java最常用的类,被广大程序员们蹂躏已久,让我们又爱又恨,应该如何正确的使用它呢,老姜带你一起来揭开它神秘的面纱. 一.String类定义时被冠上了final的标识符,决定了它终生不 ...

  3. 转载:《TypeScript 中文入门教程》 5、命名空间和模块

    版权 文章转载自:https://github.com/zhongsp 建议您直接跳转到上面的网址查看最新版本. 关于术语的一点说明: 请务必注意一点,TypeScript 1.5里术语名已经发生了变 ...

  4. MyEclipse10查看Struts2源码及Javadoc文档

    1:查看Struts2源码 (1):Referenced Libraries >struts2-core-2.1.6.jar>右击>properties. (2):Java Sour ...

  5. UDS(ISO14229-2006) 汉译(No.6 应用层服务)

    6.1总览 应用层服务通常被当作诊断服务.应用层服务用于在基于客户端-服务器的系统(Client-Server base System)中执行一些功能,例如针对车载服务器(ECU)的检测.检查.监控和 ...

  6. 判别或预测方法汇总(判别分析、神经网络、支持向量机SVM等)

    %% [Input]:s_train(输入样本数据,行数为样本数,列为维数):s_group(训练样本类别):s_sample(待判别数据)%% [Output]:Cla(预测类别) function ...

  7. Apache的初中级面试题

    --- 原文:[关于Apache的25个初中级面试题](http://www.oschina.net/translate/apache-interview-questions) Apache 求职面试 ...

  8. [python]数据整理,将取得的众多的沪深龙虎榜数据整一整

    将昨日取得的众多的沪深龙虎榜数据整一整 提取文件夹内所有抓取下来的沪深龙虎榜数据,整理出沪深两市(含中小创)涨幅榜股票及前5大买入卖出资金净值,保存到csv文件 再手动使用数据透视表进行统计 原始数据 ...

  9. Sharepoint学习笔记—习题系列--70-576习题解析 -(Q116-Q120)

    Question  116 You are helping a corporate IT department create a SharePoint 2010 information archite ...

  10. 进新公司用cornerstone-checkout后遇到的奇葩bug,及解决方法

    从cornerstone中checkout下新的工程,运行报错. 1.开始错误原因是找不到相对应的某个.m文件的路径 解决方案:将缺少的.m文件重新从项目文件夹中导入 2.后来显示 造成的原因是在下面 ...