CodeForces 732D Exams
2017-08-28 15:08
218 查看
思路:二分最早的考试时间,check按最迟考试时间贪心选取。
丑陋的代码,好像快一些?#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <utility>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <cctype>
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define IN() freopen("in.txt", "r", stdin)
#define OUT() freopen("out.txt", "w", stdout)
#define LL long long
#define maxn 100005
#define maxm 1000005
#define mod 1000000007
#define INF 1000000007
#define EPS 1e-6
#define PI 3.1415926535898
#define N 100005
using namespace std;
//-------------------------CHC------------------------------//
int d[maxn], a[maxn], endt[maxn], idx[maxn], t[maxn];
int n, m;
bool cmp(int a, int b) { return endt[a] < endt[b]; }
bool check(int mid) {
CLEAR(endt, 0); CLEAR(t, 0);
for (int i = 1; i <= mid; ++i) endt[d[i]] = i;
for (int i = 1; i <= m; ++i) if (!endt[i]) return false;
for (int i = 1; i <= m; ++i) idx[i] = i;
sort(idx + 1, idx + m + 1, cmp);
int j = 1;
for (int i = 1; i <= m; ++i) {
int cnt = a[idx[i]];
//printf("cnt = %d\n", cnt);
while (j < endt[idx[i]]) {
if (!t[j]) t[j] = 1, cnt--;
if (!cnt) break;
j++;
}
if (cnt) return false;
t[endt[idx[i]]] = 1;
}
return true;
}
int main() {
//IN(); OUT();
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i) scanf("%d", &d[i]);
for (int i = 1; i <= m; ++i) scanf("%d", &a[i]);
int l = 0, r = n + 1;
while (r - l > 1) {
int mid = (l + r) >> 1;
//printf("l = %d, mid = %d, r = %d\n", l, mid, r);
if (check(mid)) r = mid;
else l = mid;
}
if (r > n) printf("-1\n");
else printf("%d\n", r);
return 0;
}改进版
丑陋的代码,好像快一些?#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <utility>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <cctype>
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define IN() freopen("in.txt", "r", stdin)
#define OUT() freopen("out.txt", "w", stdout)
#define LL long long
#define maxn 100005
#define maxm 1000005
#define mod 1000000007
#define INF 1000000007
#define EPS 1e-6
#define PI 3.1415926535898
#define N 100005
using namespace std;
//-------------------------CHC------------------------------//
int d[maxn], a[maxn], endt[maxn], idx[maxn], t[maxn];
int n, m;
bool cmp(int a, int b) { return endt[a] < endt[b]; }
bool check(int mid) {
CLEAR(endt, 0); CLEAR(t, 0);
for (int i = 1; i <= mid; ++i) endt[d[i]] = i;
for (int i = 1; i <= m; ++i) if (!endt[i]) return false;
for (int i = 1; i <= m; ++i) idx[i] = i;
sort(idx + 1, idx + m + 1, cmp);
int j = 1;
for (int i = 1; i <= m; ++i) {
int cnt = a[idx[i]];
//printf("cnt = %d\n", cnt);
while (j < endt[idx[i]]) {
if (!t[j]) t[j] = 1, cnt--;
if (!cnt) break;
j++;
}
if (cnt) return false;
t[endt[idx[i]]] = 1;
}
return true;
}
int main() {
//IN(); OUT();
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i) scanf("%d", &d[i]);
for (int i = 1; i <= m; ++i) scanf("%d", &a[i]);
int l = 0, r = n + 1;
while (r - l > 1) {
int mid = (l + r) >> 1;
//printf("l = %d, mid = %d, r = %d\n", l, mid, r);
if (check(mid)) r = mid;
else l = mid;
}
if (r > n) printf("-1\n");
else printf("%d\n", r);
return 0;
}改进版
#include <iostream> #include <string> #include <vector> #include <stack> #include <queue> #include <deque> #include <set> #include <map> #include <algorithm> #include <functional> #include <utility> #include <cstring> #include <cstdio> #include <cstdlib> #include <ctime> #include <cmath> #include <cctype> #define CLEAR(a, b) memset(a, b, sizeof(a)) #define IN() freopen("in.txt", "r", stdin) #define OUT() freopen("out.txt", "w", stdout) #define LL long long #define maxn 100005 #define maxm 1000005 #define mod 1000000007 #define INF 1000000007 #define EPS 1e-6 #define PI 3.1415926535898 #define N 100005 using namespace std; //-------------------------CHC------------------------------// int n, m, d[maxn], t[maxn]; struct Node { int pre, endt; bool operator<(const Node &r) { return endt < r.endt; } }a[maxn]; bool check(int mid) { CLEAR(t, 0); for (int i = 1; i <= m; ++i) a[i].endt = 0; for (int i = 1; i <= mid; ++i) a[d[i]].endt = i; sort(a + 1, a + m + 1); int j = 1; for (int i = 1; i <= m; ++i) { if (a[i].endt == 0) return false; int need = a[i].pre; while (j < a[i].endt) { if (!t[j]) --need, t[j] = 1; if (!need) break; j++; } //printf("need = %d\n", need); if (need) return false; t[a[i].endt] = 1; } return true; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) scanf("%d", &d[i]); for (int i = 1; i <= m; ++i) scanf("%d", &a[i].pre); int mid, l = 0, r = n + 1; while (r - l > 1) { mid = (l + r) >> 1; //printf("l = %d, mid = %d, r = %d\n", l, mid, r); if (check(mid)) r = mid; else l = mid; } if (r > n) printf("-1\n"); else printf("%d\n", r); return 0; }
相关文章推荐
- Codeforces 732d Exams
- CodeForces 732D Exams
- CodeForces 732D Exams
- codeforces - 732D Exams 【二分 + 贪心】
- Codeforces 732D Exams【贪心+二分】
- CodeForces 732D Exams (思维+set乱搞)
- codeforces 732D Exams
- codeforces 732D Exams 二分搜索 贪心
- Codeforces 732D Exams【二分+贪心】
- codeforces 732D Exams(贪心+二分)
- CodeForces 732D Exams (二分)
- CodeForces 732D Exams
- CodeForces 732D Exams 二分+贪心
- Codeforces 919D 拓扑排序判环 + 树上 dfs + dp
- CodeForces 91B Queue 线段树 单点更新 成段查询
- Codeforces 17E Palisection Manacher ( 或 Panlindromic Tree ) + 前缀和
- Codeforces--630H--Benches(组合数)
- [Codeforces 585D] Lizard Era: Beginning (折半枚举)
- CodeForces - 622A A. Infinite Sequence
- CodeForces 93 D.Flags(dp+矩阵快速幂)