Codeforces Round #377 (Div. 2) D. Exams 贪心 + 简单模拟

时间:2023-03-08 17:27:09

http://codeforces.com/contest/732/problem/D

这题我发现很多人用二分答案,但是是不用的。

我们统计一个数值all表示要准备考试的所有日子和。+m(这些时间用来考试)

那么这个all值就是理想的最小值。然后去前all个数找,贪心地复习最小的科目,然后有的考试的话,就优先考试。

如果经过这all天,复习完了(这个是肯定得),但是只是因为时间分配不好,导致没得考试(数据导致没得考试)

那么就暴力枚举后面的[all + 1, n]。有得考试就去考试,刚好考完就直接输出i就可以了。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = 1e5 + ;
int a[maxn];
int day[maxn];
void work() {
int n, m;
cin >> n >> m;
for (int i = ; i <= n; ++i) {
cin >> a[i];
}
LL all = m;
for (int i = ; i <= m; ++i) {
cin >> day[i];
all += day[i];
}
sort(day + , day + + m);
int lenday = ;
int now = ;
if (n < all) {
cout << "-1" << endl;
return;
}
for (int i = ; i <= all; ++i) {
if (a[i] == ) {
day[lenday]--;
if (day[lenday] == ) {
now++;
lenday++;
}
} else {
if (now) now--;
else {
day[lenday]--;
if (day[lenday] == ) {
now++;
lenday++;
}
}
}
}
if (now == ) {
cout << all << endl;
return;
}
for (int i = all + ; i <= n; ++i) {
if (a[i] == ) {
continue;
} else {
now--;
if (now == ) {
cout << i << endl;
return;
}
} }
cout << "-1" << endl;
} int main() {
#ifdef local
freopen("data.txt","r",stdin);
#endif
work();
return ;
}