[关闭]
@ZCDHJ 2018-08-27T16:32:20.000000Z 字数 760 阅读 892

USACO07NOV Sunscreen

未分类


Luogu

一道比较简单的贪心题

因为有两个限制条件,所以将所有奶牛按左端点降序排列,这样子一头奶牛能用的防晒霜也能满足后面所有牛的左端点限制。

这样子的话每次选防晒霜就选满足条件的最大的那个

正确性显然

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. const int MAXN = 2500 + 5;
  5. int N, M, Ans;
  6. int SPF[MAXN], Cover[MAXN];
  7. struct Cow
  8. {
  9. int l, r;
  10. } A[MAXN];
  11. inline int read()
  12. {
  13. register int x = 0;
  14. register char ch = getchar();
  15. while(!isdigit(ch)) ch = getchar();
  16. while(isdigit(ch))
  17. {
  18. x = x * 10 + ch - '0';
  19. ch = getchar();
  20. }
  21. return x;
  22. }
  23. bool comp(Cow x, Cow y)
  24. {
  25. return x.l > y.l;
  26. }
  27. int main()
  28. {
  29. N = read();
  30. M = read();
  31. for(int i = 1; i <= N; ++i)
  32. {
  33. A[i].l = read();
  34. A[i].r = read();
  35. }
  36. for(int i = 1; i <= M; ++i)
  37. {
  38. SPF[i] = read();
  39. Cover[i] = read();
  40. }
  41. std::sort(A + 1, A + 1 + N, comp);
  42. for(int i = 1; i <= N; ++i)
  43. {
  44. int t = 0;
  45. for(int j = 1; j <= M; ++j)
  46. if(Cover[j] != 0 && SPF[j] >= A[i].l && SPF[j] <= A[i].r && SPF[j] > SPF[t]) t = j;
  47. if(t != 0)
  48. {
  49. ++Ans;
  50. --Cover[t];
  51. }
  52. }
  53. printf("%d\n", Ans);
  54. return 0;
  55. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注