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. atitit. java跟php的比较..为什么大企业喜欢java 而不是php

    atitit. java跟php的比较..为什么大企业喜欢java 而不是php 1. 主要原因::php是"低技术.低价格.低素质人员使用"的三低武器 1 2. 三低武器的特点( ...

  2. 上传图片流到服务器(AFN方法) (多张图片)(图片流)

      上传图片流到服务器(AFN方法) (多张图片)(图片流) 第一步//获取图片 UIAlertController *actionSheet = [UIAlertController alertCo ...

  3. 1、改变 vs编辑器的主题

    打开 visual studio, 在菜单栏选择  工具 -> 扩展和更新 -> 输入 “color theme” 安装完成后,选择样式. 选择好样式后, vs 立即改变主题.再次打开样式 ...

  4. [linux]signal函数不起作用

    #include "apue.h" #include <sys/wait.h> static void sig_int(int); /* our signal-catc ...

  5. eclipse配置xml的自动提示

    如mybatis的mapper配置文件: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE ...

  6. 一款纯css3实现的tab选项卡

    今天给大家带来一款纯css3实现的tab选项卡.单击左侧的选项的时候,右侧内容以动画的形式展示.效果图如下: 在线预览   源码下载 实现的代码. html代码: <div class=&quo ...

  7. [WDT]内部看门狗和外部看门狗

    1. 芯片内部看门狗 内部看门狗通常为芯片内部某个特殊定时器,用户可以通过手动初始化.设置timeout.使能.失能该看门狗,然后在线程中定时去喂狗,从而达到检测应用程序跑飞.跑死的情况. 在Linu ...

  8. CSS——div居中,window.open(0

    margin:0 auto 表示什么意思?? margin后面如果只有两个参数的话,第一个表示top和bottom,第二个表示left和right因为0 auto,表示上下边界为0,左右则根据宽度自适 ...

  9. hdu6125 Free from square 分组背包+状态压缩

    /** 题目:hdu6125 Free from square 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6125 题意: 从不大于n的所有正整数中选出 ...

  10. linux 安装开启SNMP协议,最下面是yum安装

    Linux SNMP 以下的示例采用SUSE10 Linux环境,但它同样适用于其它Linux发行版. 编译和安装 首先我们需要下载Net-SNMP的源代码,选择一个版本,比如5.7.1,地址如下: ...