博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
codeforces - 732D Exams 【二分 + 贪心】
阅读量:6191 次
发布时间:2019-06-21

本文共 3845 字,大约阅读时间需要 12 分钟。

 

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 20 1 0 2 1 0 22 1
output
5
input
10 30 0 1 2 3 0 2 0 1 21 1 4
output
9
input
5 11 1 1 1 15
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个数, 表示第i天的状态。如果ai为0,表示今天没考试。否则表示该天可以参加第ai门考试。

另行有m个数,表示有第i门考试需要准备的准备时间。

思路:

明显的二分,但是每次二分都有策略,贪心一下。

#include #include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//#include
//#define LOACL#define space " "using namespace std;typedef long long LL;typedef __int64 Int;typedef pair
paii;const int INF = 0x3f3f3f3f;const double ESP = 1e-5;const double PI = acos(-1.0);const int MOD = 1e9 + 7;const int MAXN = 1e5 + 10;int ar[MAXN], data[MAXN];bool vis[MAXN];int n, m;bool judge(int x) { int cnt = x - 1; memset(vis, false, sizeof(vis)); //贪心策略,从最后开始,这样可以保证最优解 //如果这种策略不合法,那么一定误解 for (int i = x; i >= 1; i--) { cnt = min(cnt, i - 1); if (data[i] && !vis[data[i]] && ar[data[i]] <= cnt) { vis[data[i]] = true; cnt -= ar[data[i]] + 1; } } for (int i = 1; i <= m; i++) { if (!vis[i]) return false; } return true;}int main() { while (scanf("%d%d", &n, &m) != EOF) { for (int i = 1; i <= n; i++) scanf("%d", &data[i]); for (int i = 1; i <= m; i++) scanf("%d", &ar[i]); sort(ar + 1, ar + 1 + m); int ans = -1; int lb = 0, ub = n; while (ub >= lb) { int mid = (ub + lb) >> 1; if (judge(mid)) { ans = mid; ub = mid - 1; } else lb = mid + 1; } printf("%d\n", ans); } return 0;}

 

 

 

 

转载于:https://www.cnblogs.com/cniwoq/p/6770761.html

你可能感兴趣的文章
RSS文章订阅及生成RSS格式的xml
查看>>
你自认为理解了JavaScript?
查看>>
读《程序员的SQL金典》[4]--SQL调优
查看>>
死锁产生的原因及四个必要条件
查看>>
CSS3----background:-webkit-gradient()渐变效果
查看>>
RTP协议分析
查看>>
前后端分离了,然后呢?(转)
查看>>
自定义控件:滑动开关按钮
查看>>
js修改后没反应-看看是不是取的缓存
查看>>
【iCore3 双核心板_ uC/OS-III】例程十一:任务消息队列
查看>>
C#的delegate简单练习
查看>>
【301】IDL与C#混合编程
查看>>
分治法应用之一——Strassen矩阵乘法(转)
查看>>
linux-diff命令
查看>>
必须关注的25位知名JavaScript开发者
查看>>
linq直接执行sql语句
查看>>
POJ - 1170 Shopping Offers (五维DP)
查看>>
【Linux学习】Linux的文件权限(一)
查看>>
python的内存管理机制
查看>>
一个基于 EasyUI 的前台架构(3)封装操作Tabs的JS代码
查看>>