CodeForces 732D Exams
1 second
256 megabytes
standard input
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.
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.
Print one integer — the minimum number of days in which Vasiliy can pass all exams. If it is impossible, print -1.
7 2
0 1 0 2 1 0 2
2 1
5
10 3
0 0 1 2 3 0 2 0 1 2
1 1 4
9
5 1
1 1 1 1 1
5
-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的更多相关文章
- CodeForces 732D Exams (二分)
题意:某人要考试,有n天考m个科目,然后有m个科目要考试的时间和要复习多少天才能做,问你他最早考完所有科目是什么时间. 析:二分答案,然后在判断时,直接就是倒着判,很明显后出来的优先,也就是一个栈. ...
- 【37.50%】【codeforces 732D】Exams
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- codeforces 732D(二分)
题目链接:http://codeforces.com/contest/732/problem/D 题意:有m门需要过的课程,n天的时间可以选择复习.考试(如果的d[i]为0则只能复习),一门课至少要复 ...
- codeforces 479C Exams 解题报告
题目链接:http://codeforces.com/problemset/problem/479/C 题目意思:简单来说,就是有个人需要通过 n 门考试,每场考试他可以选择ai, bi 这其中一个时 ...
- codeforces 732D
D. Exams time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...
- CodeForces 479C Exams 贪心
题目: C. Exams time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- Codeforces 732D [二分 ][贪心]
/* 不要低头,不要放弃,不要气馁,不要慌张 题意: n天进行m科考试,每科考试需要a的复习时间,n天每天最多可以考一科.并且指定哪天考哪科. 注意考试那天不能复习. 问最少需要多少天可全部通过考试. ...
- Codeforces Round #377 (Div. 2)A,B,C,D【二分】
PS:这一场真的是上分场,只要手速快就行.然而在自己做的时候不用翻译软件,看题非常吃力非常慢,还有给队友讲D题如何判断的时候又犯了一个毛病,一定要心平气和,比赛也要保证,不要用翻译软件做题: Code ...
- 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 ...
随机推荐
- atitit。mssql sql server 转换mysql 及 分页sql ast的搭建
atitit.mssql sql server 转换mysql 及 分页sql ast的搭建 1. 主要的的转换::函数的转换,分页的转换 1 2. 思路::mssql sql >>as ...
- CentOS 7.0 关闭firewalld防火墙指令 及更换Iptables防火墙
CentOS 7.0 关闭firewalld防火墙指令 及更换Iptables防火墙 时间:2014-10-13 19:03:48 作者:哎丫丫 来源:哎丫丫数码网 查看:11761 评论:2 ...
- 《Google软件测试之道》- Google软件测试介绍
<Google软件测试之道>- Google软件测试介绍 2015-05-21 目录 1 质量与测试 2 角色 3 组织结构 4 爬.走.跑 5 测试类型 相关链接 与Micro ...
- 0069 如何在Intellij IDEA中查看Spring MVC项目的所有请求与处理方法的映射列表
@RequestMapping注解分散于Controller中,如何在一个列表中看到所有的请求呢,比如下面这样: File-->Project Structure-->Modules--& ...
- C++虚函数解析(转载)
虚函数详解第一篇:对象内存模型浅析 C++中的虚函数的内部实现机制到底是怎样的呢? 鉴于涉及到的内容有点多,我将分三篇文章来介绍. 第一篇:对象内存模型浅析,这里我将对对象的内存模型进 ...
- Python 操作文件、文件夹、目录大全
# -*- coding: utf-8 -*- import os import shutil # 一. 路径操作:判断.获取和删除 #1. 得到当前工作目录,即当前Python脚本工作的目录路径: ...
- 纯css3实现的win8加载动画
今天给大家分享一款纯css3实现的win8加载动画.在这款实例中动画效果完全由css3实现.一起看下效果图: 在线预览 源码下载 实现的代码. html代码: <div class=&quo ...
- Unity3D 5.0版本+注册工具分享
Unity3D引擎5.0正式版本发布也有一段时间了.笔者今天下载了新版本顺便分享一下资源. 主要有两个资源,一个是5.0f4的官方客户端,另外一个是vs的调试插件.有需要的盆友就拿去.都在下面的连接地 ...
- 16c554 的头文件
//------------------------------------------------------------------------------ #ifndef AT16C554H # ...
- [oracle] update语句卡住问题
执行update语句的时候发现执行半天不成功 update main_order set order_source = '2', order_status = '2' 查询哪些对象被锁 select ...