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

贪心。

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h> using namespace std;
const int maxn=1e5;
int n,m;
int a[maxn+5];
int d[maxn+5];
int vis[maxn+5];
int check(int x)
{
int num=0;
int need=0;
memset(vis,0,sizeof(vis));
for(int i=x;i>=1;i--)
{
if(!vis[a[i]]&&num!=m&&a[i]!=0)
{
need+=d[a[i]];
num++;
vis[a[i]]=1;
continue;
}
if(need)
{
need--;
}
}
if(need<=0&&num==m) return 1;
return 0;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=1;i<=m;i++)
scanf("%d",&d[i]);
int l=1,r=n;
int ans=-1;
while(l<=r)
{
int mid=(l+r)>>1;
if(check(mid))
{
ans=mid;
r=mid-1;
}
else
l=mid+1;
} printf("%d\n",ans);
return 0;
}

CodeForces 732D Exams的更多相关文章

  1. CodeForces 732D Exams (二分)

    题意:某人要考试,有n天考m个科目,然后有m个科目要考试的时间和要复习多少天才能做,问你他最早考完所有科目是什么时间. 析:二分答案,然后在判断时,直接就是倒着判,很明显后出来的优先,也就是一个栈. ...

  2. 【37.50%】【codeforces 732D】Exams

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  3. codeforces 732D(二分)

    题目链接:http://codeforces.com/contest/732/problem/D 题意:有m门需要过的课程,n天的时间可以选择复习.考试(如果的d[i]为0则只能复习),一门课至少要复 ...

  4. codeforces 479C Exams 解题报告

    题目链接:http://codeforces.com/problemset/problem/479/C 题目意思:简单来说,就是有个人需要通过 n 门考试,每场考试他可以选择ai, bi 这其中一个时 ...

  5. codeforces 732D

    D. Exams time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...

  6. CodeForces 479C Exams 贪心

    题目: C. Exams time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  7. Codeforces 732D [二分 ][贪心]

    /* 不要低头,不要放弃,不要气馁,不要慌张 题意: n天进行m科考试,每科考试需要a的复习时间,n天每天最多可以考一科.并且指定哪天考哪科. 注意考试那天不能复习. 问最少需要多少天可全部通过考试. ...

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

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

  9. Codeforces Round #280 (Div. 2) C. Vanya and Exams 贪心

    C. Vanya and Exams Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/492/pr ...

随机推荐

  1. 浅谈I2C总线

    I2C总线概述 I2C(Inter-Integrated Circuit)总线是一种由PHILIPS公司在80年代开发的两线式串行总线,用于连接微控制器及其外围设备.I2C总线最主要的优点是其简单性和 ...

  2. Selenium - Switch & Select Api

    一.多表单切换  driver.switch_to.frame() iframe  :直接将一个html 页面嵌入另一个html 页面中 switch_to.frame() 默认可以直接取表单的id ...

  3. 每日英语:Do Successful People Need Sleep?

    George Washington and his Revolutionary War victories. Robert Frost and the composition of 'Stopping ...

  4. jQuery 效果 - slideToggle() 方法

    实例 通过使用滑动效果,在显示和隐藏状态之间切换 <p> 元素: $(".btn1").click(function(){ $("p").slide ...

  5. BAT-把当前用户以管理员权限运行

    相关资料: http://jingyan.baidu.com/article/72ee561a5dc24fe16138df95.html 网友求助:联想Y400,Win8系统 怎样获得管理员身份 要求 ...

  6. Unix系统编程():分散输入和集中输出(Scatter-Gather IO):readv和writev

    分散输入和集中输出(Scatter-Gather IO):readv和writev 请问这个v又代表什么? readv和writev系统调用分别实现了分散输入和集中输出的功能. #include< ...

  7. Python高级编程之生成器(Generator)与coroutine(二):coroutine介绍

    原创作品,转载请注明出处:点我 上一篇文章Python高级编程之生成器(Generator)与coroutine(一):Generator中,我们介绍了什么是Generator,以及写了几个使用Gen ...

  8. Entity Framework(五):使用配置伙伴创建数据库

    在上一篇文章中讲了如何使用fluent API来创建数据表,不知道你有没有注意到一个问题.上面的OnModelCreating方法中,我们只配置了一个类Product,也许代码不是很多,但也不算很少, ...

  9. php插入代码数据库

    插入代码:<?php $con = mysql_connect("localhost","peter","abc123"); if ( ...

  10. 在项目中使用JMail发送邮件

    1.添加JMail组件:在vs的解决方案资源管理器中,右键添加引用,选择浏览,选择jmail.dll文件,在Bin文件夹中就可以看到添加的JMail组件. 2.注册JMail组件:开始-->运行 ...