fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <algorithm>
  6. #define FI(type) vector<type>::iterator
  7. #define isBetween(A,B,C) (((A <= B) && (B <= C)))
  8.  
  9. using namespace std;
  10.  
  11. struct frend
  12. {
  13. int food, num, start, end;
  14. frend(int beg, int last, int f, int n)
  15. :food(f), num(n), start(beg), end(last) {}
  16. frend(){}
  17. };
  18.  
  19. struct Dorm
  20. {
  21. int foodNeed, numDays, *food_per_day, Pop, p, i, **action;
  22. FI(frend) it;
  23. public:
  24. bool operator() (frend, frend);
  25.  
  26. Dorm (int n, int f)
  27. :foodNeed(f), numDays(n), food_per_day(new int[n]), Pop(0)
  28. {
  29. action = new (nothrow)int*[n];
  30. for (int c = 0; c < numDays; ++c)
  31. {
  32. action[c] = new (nothrow)int[n+1];
  33. memset(action[c], 0, sizeof action[c]);
  34. scanf("%d", &food_per_day[c]);
  35. food_per_day[c] -= foodNeed;//subtract food for Vasya
  36. }
  37. }
  38. vector <frend> Berland;
  39.  
  40. void Print();
  41. void Get();
  42.  
  43. };
  44.  
  45. void Dorm::Print()
  46. {
  47. printf("%d\n", Pop);
  48. for (p = 0; p < numDays; ++p)
  49. {
  50. for (int i = 0; action[p][i] >= 0; ++i)
  51. printf("%d ", action[p][i]);
  52. puts("");
  53. }
  54. }
  55.  
  56. int main()
  57. {
  58. int n, v, m, t(1), k(1);
  59.  
  60. scanf("%d%d", &n, &v);
  61. Dorm Vasya(n,v);
  62.  
  63. for (scanf("%d", &m); m--;)
  64. {
  65. scanf("%d%d%d", &n, &v, &t);
  66. Vasya.Berland.push_back(frend(n,v,t,k++));
  67. }
  68. Vasya.Get();
  69. return 0;
  70. }
  71.  
  72.  
  73. void Dorm::Get()
  74. {
  75. sort(Berland.begin(), Berland.end(), *this);//sort in order of lowest need for food to highest
  76. for (p = 0; p < numDays; ++p)
  77. {
  78. for (it = Berland.begin(), i = 1; it != Berland.end() && (food_per_day[p] - (*it).food >= 0); ++it)
  79. {
  80. if (isBetween((*it).start, p+1, (*it).end))
  81. {
  82. food_per_day[p] -= (*it).food, ++Pop;
  83. action[p][0] += 1;
  84. action[p][i++] = (*it).num;
  85. }
  86. }
  87. action[p][i] = -1;
  88. p+1 < numDays ? food_per_day[p+1] += food_per_day[p] : 0;//left over food
  89. }
  90. Print();
  91. }
  92.  
  93. bool Dorm::operator() (frend uno, frend duo)
  94. {
  95. return (uno.food < duo.food);
  96. }
Runtime error #stdin #stdout 0s 529408KB
stdin
Standard input is empty
stdout
Standard output is empty