# D. Exams

## Problem Description：

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.

7 2
0 1 0 2 1 0 2
2 1

## Sample Output：

5

【题目链接】D. Exams

【题目类型】二分答案

&题解：

【时间复杂度】\(O(nlogn)\)

&代码：

``````#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
#define cle(a,val) memset(a,(val),sizeof(a))
#define SI(N) scanf("%d",&(N))
#define SII(N,M) scanf("%d %d",&(N),&(M))
#define SIII(N,M,K) scanf("%d %d %d",&(N),&(M),&(K))
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define red(i,a,b) for(int i=(a);i>=(b);i--)
const ll LINF = 0x3f3f3f3f3f3f3f3f;
#define PU(x) cout<<#x<<endl;
#define PI(A) cout<<(A)<<endl;
#define DG(x) cout<<#x<<"="<<(x)<<endl;
#define DGG(x,y) cout<<#x<<"="<<(x)<<" "<<#y<<"="<<(y)<<endl;
#define DGGG(x,y,z) cout<<#x<<"="<<(x)<<" "<<#y<<"="<<(y)<<" "<<#z<<"="<<(z)<<endl;
#define PIar(a,n) rep(i,0,n-1)cout<<a[i]<<" ";PU()
#define PIarr(a,n,m) rep(aa,0,n-1){rep(bb,0,m-1)cout<<a[aa][bb]<<" ";PU()}
typedef pair<int, int> pii;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define sz(x) int(x.size())
#define all(x) x.begin(),x.end()
const double EPS = 1e-9 ;
/*  ////////////////////////   C o d i n g  S p a c e   ////////////////////////  */
const int maxn = (int)1e5 + 9 ;
int n, m, d[maxn], a[maxn];
set<int> sei;
vector<int> vei;
bool ok(int x) {
sei.clear();
vei.clear();
for (; x >= 1; x--) if (d[x] && !sei.count(d[x])) {
sei.insert(d[x]);
vei.pb(x);
}
if (sei.size() != m) { return false; }
ll sum=0;
for (int i = sz(vei) - 1; i >= 0; i--) {
if (vei[i]>sum+a[d[vei[i]]]) sum+=a[d[vei[i]]]+1;
else return false;
}
return true;
}
void Solve() {
while (cin >> n >> m) {
rep(i, 1, n) cin >> d[i];
rep(i, 1, m) cin >> a[i];
int l = 0, r = n + 1,m;
//二分这一定要是l<=r  不要忘了是<=
while(l<=r) {
m=l+r>>1;
//这保证的是一开一闭区间 也就是[ , ) 或 ( , ]
if (ok(m)) r=m-1;
else l=m+1;
}
if (l>n||l<1) l=-1;
PI(l)
//可以看一下他们3个的区别
//      DGGG(l,r,m)
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen("1.in", "r", stdin);
#endif
iostream::sync_with_stdio(false),cin.tie(0),cout.tie(0);
Solve();
return 0;
}``````

## Codeforces Round #377 (Div. 2) D. Exams(二分答案)的更多相关文章

1. Codeforces Round #377 (Div. 2) D. Exams 二分

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

2. Codeforces Round #377 (Div. 2)D(二分)

题目链接:http://codeforces.com/contest/732/problem/D 题意: 在m天中要考k个课程, 数组a中有m个元素,表示第a[i]表示第i天可以进行哪门考试,若a[i ...

3. Codeforces Round #377 (Div. 2) A B C D 水/贪心/贪心/二分

A. Buy a Shovel time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

4. Codeforces Round #274 (Div. 1) A. Exams 贪心

A. Exams Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/480/problem/A Des ...

5. Codeforces Round #377 (Div. 2)

#include <iostream> #include <stdio.h> #include <string.h> using namespace std; in ...

6. Codeforces Round #377 (Div. 2) B. Cormen — The Best Friend Of a Man（贪心）

传送门 Description Recently a dog was bought for Polycarp. The dog's name is Cormen. Now Polycarp has ...

7. Codeforces Round #377 (Div. 2) C. Sanatorium 水题

C. Sanatorium time limit per test 1 second memory limit per test 256 megabytes input standard input ...

8. Codeforces Round #378 (Div. 2) D题（data structure）解题报告

题目地址 先简单的总结一下这次CF,前两道题非常的水,可是第一题又是因为自己想的不够周到而被Hack了一次(或许也应该感谢这个hack我的人,使我没有最后在赛后测试中WA).做到C题时看到题目情况非常 ...

9. Codeforces Round #365 (Div. 2) C - Chris and Road 二分找切点

// Codeforces Round #365 (Div. 2) // C - Chris and Road 二分找切点 // 题意:给你一个凸边行,凸边行有个初始的速度往左走,人有最大速度,可以停 ...

## 随机推荐

1. C#模拟Http与Https请求框架实例

using System.Text; using System.Net; using System.IO; using System.Text.RegularExpressions; using Sy ...

2. Java线程的5个使用技巧

萝卜白菜各有所爱.像小编我就喜欢Java.学无止境,这也是我喜欢它的一个原因.日常工作中你所用到的工具,通常都有些你从来没有了解过的东西,比方说某个方法或者是一些有趣的用法.比如说线程.没错,就是线程 ...

3. SQL Server中时间段查询

/****** Script for SelectTopNRows command from SSMS ******/ select * from dbo.VehicleData20100901 wh ...

4. 启动hadoop，报错Error JAVA_HOME is not set and could not be found

报如错误:JAVA_HOME is not set and could not be found,可能是因为JAVA_HOME环境没配置正确,还有一种情况是即使各结点都正确地配置了JAVA_HOME, ...

5. finally与return

finally关键字:和try块使用,一般做资源释放操作,比如关闭流.关闭数据库连接,释放锁. return:用于返回值. finally块可保证一定执行,当逻辑处理有返回值时,会首先执行finall ...

6. 从内存中加载DLL Delphi版(转)

源:从内存中加载DLL DELPHI版 原文 : http://www.2ccc.com/article.asp?articleid=5784 MemLibrary.pas //从内存中加载DLL D ...

7. RHEL7对比RHEL6的主要变化

RHEL7和RHEL6的主要变化 RHEL7和RHEL6的主要变化   RHEL7 RHEL6 文件系统 XFS EXT4 内核版本 3.10.x-x系列 2.6.x-x系列 内核名称 Maipo S ...

8. Scrapy爬虫实例教程（二）---数据存入MySQL

书接上回 实例教程(一) 本文将详细描述使用scrapy爬去左岸读书所有文章并存入本地MySql数据库中,文中所有操作都是建立在scrapy已经配置完毕,并且系统中已经安装了Mysql数据库(有权限操 ...

9. 服务器配置用户信息、ssh免密码登录和防火墙等安全配置

一.登录服务器      1.回到根目录 cd ~      2.ssh + 用户名@服务器公网地址 ssh root@47.94.208.76      3.输入密码:注意输入法大小写 二.查看服务 ...

10. Linux内核分析— —扒开系统调用的三层皮（下）

课程主要内容有三点: 在MenuOS中通过添加代码增加自定义的系统调用命令 使用gdb跟踪调试内核 简单分析system_call代码了解系统调用在内核代码中的处理过程 实验——分析system_ca ...