记录编号 200516 评测结果 AAAAAAAAAAAAAAAAAAAA
题目名称 [NOIP 2012]借教室 最终得分 100
用户昵称 Gravatarthomount 是否通过 通过
代码语言 C++ 运行时间 1.796 s
提交时间 2015-10-28 21:30:58 内存使用 32.89 MiB
显示代码纯文本
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. using namespace std;
  5. int read() {
  6. char ch = getchar();
  7. while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
  8. int ans = 0, flag = 1;
  9. if (ch == '-') flag = -1, ch = getchar();
  10. while (ch >= '0' && ch <= '9') ans = ans * 10 + ch - '0', ch = getchar();
  11. return ans * flag;
  12. }
  13. const int N = 1000010;
  14. struct point {
  15. int x, w, s;
  16. point(int s = 0, int w = 0, int x = 0): x(x), w(w), s(s) {}
  17. } p[2*N];
  18. bool operator < (point a, point b) {
  19. return (a.w < b.w);
  20. }
  21. int d[N], f[N], a[N];
  22. int main() {
  23. freopen("classrooms.in", "r", stdin);
  24. freopen("classrooms.out", "w", stdout);
  25. int n = read(), m = read();
  26. for (int i = 1; i <= n; i++) a[i] = read();
  27. for (int i = 1; i <= m; i++) {
  28. int D = read(), L = read(), R = read();
  29. d[i] = D;
  30. p[2*i-1] = point(D, L, i);
  31. p[2*i] = point(-D, R+1, i);
  32. }
  33. sort(p+1, p+2*m+1);
  34. int st = 1, bad = m+1;
  35. memset(f, 0, sizeof(f));
  36. long long S = 0;
  37. for (int i = 1; i <= n; i++) {
  38. while (st <= 2*m && p[st].w == i) {
  39. if (p[st].x <= bad) {
  40. S += p[st].s;
  41. if (p[st].s < 0) f[p[st].x] = 0; else f[p[st].x] = 1;
  42. }
  43. st++;
  44. }
  45. if (S > a[i]) {
  46. while (bad > 1 && S > a[i]) {
  47. if (f[bad-1]) {
  48. S -= d[bad-1];
  49. f[bad-1] = 0;
  50. }
  51. bad--;
  52. }
  53. }
  54. if (bad == 1) break;
  55. }
  56. if (bad == m+1) printf("0\n"); else printf("-1\n%d\n", bad);
  57. return 0;
  58. }