Advertisement
a53

mostenire1

a53
Mar 14th, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. // Maria Pandele - 90p
  2.  
  3. #include <fstream>
  4. #include <iostream>
  5. #include <vector>
  6. #include <cassert>
  7. #include <algorithm>
  8.  
  9. const int N = 1e5;
  10. const int K = 1e2;
  11. const int VALMAX = 1e5;
  12. const int SMAX = 1e9;
  13.  
  14. struct Fiu {
  15. int ind;
  16. int s;
  17. int st; int dr;
  18. };
  19.  
  20. bool solve(int s_mezin, std::vector<int>& a, int& k) {
  21. int cnt = 0, s = 0;
  22.  
  23. for (int i = 0; i < a.size(); ++i) {
  24. s += a[i];
  25. if (s >= s_mezin) {
  26. ++cnt;
  27. if (cnt == k) {
  28. break;
  29. }
  30. s = 0;
  31. }
  32. }
  33. if (cnt == k) {
  34. return true;
  35. }
  36. return false;
  37. }
  38.  
  39. void imparte(const int& s_mezin, std::vector<int>& a, const int& k, std::vector<Fiu>& sol) {
  40. int cnt = -1, last = -1, s = 0;
  41.  
  42. for (int i = 0; i < a.size(); ++i) {
  43. s += a[i];
  44. if (s >= s_mezin) {
  45. ++cnt;
  46. sol[cnt].st = last + 2;
  47. sol[cnt].dr = i + 1;
  48. sol[cnt].s = s;
  49. if (cnt == k - 1) {
  50. sol[cnt].dr = a.size();
  51. for (int j = i + 1; j < a.size(); ++j) {
  52. s += a[j];
  53. }
  54. sol[cnt].s = s;
  55. break;
  56. }
  57. last = i;
  58. s = 0;
  59. }
  60. }
  61. sort(sol.begin(), sol.end(), [](const Fiu& A, const Fiu& B) -> bool {
  62. return A.s > B.s;
  63. });
  64. for (int i = 0; i < sol.size(); ++i) {
  65. sol[i].ind = i + 1;
  66. }
  67. sort(sol.begin(), sol.end(), [](const Fiu& A, const Fiu& B) -> bool {
  68. return A.st < B.st;
  69. });
  70. }
  71.  
  72. int main() {
  73. std::ifstream cin("mostenire.in");
  74. std::ofstream cout("mostenire.out");
  75.  
  76. int n, k;
  77. cin >> n >> k;
  78. assert(k <= n && n <= N);
  79. assert(2 <= k && k <= K);
  80.  
  81. std::vector<int> a(n);
  82. long long stotal = 0;
  83. for (int i = 0; i < n; ++i) {
  84. cin >> a[i];
  85. assert(1 <= a[i] && a[i] <= VALMAX);
  86. stotal += a[i];
  87. }
  88.  
  89. assert(stotal <= SMAX);
  90.  
  91. int mezin = 0;
  92. for (int step = (1 << 30); step; step >>= 1) {
  93. if (mezin + step <= stotal&& solve(mezin + step, a, k)) {
  94. mezin += step;
  95. }
  96. }
  97.  
  98. assert(mezin != 0);
  99. std::vector<Fiu> sol(k);
  100. imparte(mezin, a, k, sol);
  101. cout << mezin << "\n";
  102.  
  103. for (int i = 0; i < sol.size(); ++i) {
  104. cout << sol[i].ind << " " << sol[i].dr - sol[i].st + 1 << "\n";
  105. assert(sol[i].dr - sol[i].st + 1 > 0);
  106. }
  107. return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement