Advertisement
a53

microbuz

a53
May 5th, 2022
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define INF (INT_MAX >> 2)
  4. #define K_MAX 10
  5. #define S_MAX (K_MAX * (K_MAX + 1) / 2)
  6. #define C_MAX 100
  7.  
  8. using namespace std;
  9.  
  10. ifstream fin("microbuz.in");
  11. ofstream fout("microbuz.out");
  12.  
  13. int T;
  14. int N;
  15. int C[K_MAX + 5];
  16. pair <int, int> D[S_MAX + 5]; ///D[i] = perechea (cost, conf) unde cost reprezinta costul minim sa obtinem distanta i
  17. ///si conf reprezinta configuratia cu care acest cost a fost obtinut
  18. bitset <C_MAX * K_MAX + 5> A[(1 << K_MAX) + 5];
  19.  
  20. pair <int, int> compute_conf(int conf) {
  21. int cost = 0;
  22. int dist = 0;
  23. for(int i = 0; i < K_MAX; i++) {
  24. if(conf & (1 << i)) {
  25. dist += i + 1;
  26. cost += C[i];
  27. }
  28. }
  29. return make_pair(cost, dist);
  30. }
  31.  
  32. int main()
  33. {
  34. fin >> T;
  35. for(int i = 0; i < K_MAX; i++) {
  36. fin >> C[i];
  37. }
  38. fin >> N;
  39.  
  40. if(T <= 2) {
  41. for(int i = 1; i <= S_MAX; i++) {
  42. D[i].first = INF;
  43. }
  44.  
  45. for(int conf = 0; conf < (1 << K_MAX); conf++) {
  46. int cost = 0, dist = 0;
  47. tie(cost, dist) = compute_conf(conf);
  48. if(D[dist].first > cost) {
  49. D[dist] = make_pair(cost, conf);
  50. }
  51. }
  52.  
  53. int ans = INF;
  54. int ans_conf[4];
  55. for(int dist_1 = 0; dist_1 <= S_MAX; dist_1++) {
  56. for(int dist_2 = 0; dist_2 <= S_MAX && dist_1 + dist_2 <= N; dist_2++) {
  57. int dist_3 = N - dist_1 - dist_2;
  58. if(dist_3 > S_MAX) {
  59. continue;
  60. }
  61. if(ans > D[dist_1].first + D[dist_2].first + D[dist_3].first) {
  62. ans = D[dist_1].first + D[dist_2].first + D[dist_3].first;
  63. ans_conf[1] = D[dist_1].second;
  64. ans_conf[2] = D[dist_2].second;
  65. ans_conf[3] = D[dist_3].second;
  66. }
  67. }
  68. }
  69.  
  70. if(T == 1) {
  71. fout << ans << "\n";
  72. }
  73. else {
  74. for(int i = 1; i <= 3; i++) {
  75. for(int j = 0; j < K_MAX; j++) {
  76. if(ans_conf[i] & (1 << j)) {
  77. fout << j + 1 << " " << C[j] << "\n";
  78. }
  79. }
  80. }
  81. }
  82. }
  83. else {
  84. for(int conf = 1; conf < (1 << K_MAX); conf++) {
  85. int cost = 0, dist = 0;
  86. tie(cost, dist) = compute_conf(conf);
  87. for(int i = 0; i < K_MAX; i++) {
  88. if(conf & (1 << i)) {
  89. A[conf] |= A[conf ^ (1 << i)];
  90. }
  91. }
  92. A[conf][cost] = 1;
  93. }
  94.  
  95. int ans = 0, ans_conf = 0;
  96. for(int conf = 1; conf < (1 << K_MAX) - 1; conf++) {
  97. int cost = 0, dist = 0;
  98. tie(cost, dist) = compute_conf(conf);
  99. int flip = ((1 << K_MAX) - 1) ^ conf;
  100. if(A[flip][cost]) {
  101. if(cost > ans) {
  102. ans = cost;
  103. ans_conf = conf;
  104. }
  105. }
  106. }
  107.  
  108. fout << ans << "\n";
  109. for(int i = 0; i < K_MAX; i++) {
  110. if(ans_conf & (1 << i)) {
  111. fout << i + 1 << " ";
  112. }
  113. }
  114. fout << "\n";
  115.  
  116. int flip = ((1 << K_MAX) - 1) ^ ans_conf;
  117. for(int conf = 1; conf <= flip; conf++) {
  118. if((conf & flip) == conf) { ///conf este submultime a lui flip
  119. int cost = 0, dist = 0;
  120. tie(cost, dist) = compute_conf(conf);
  121. if(cost == ans) {
  122. for(int i = 0; i < K_MAX; i++) {
  123. if(conf & (1 << i)) {
  124. fout << i + 1 << " ";
  125. }
  126. }
  127. return 0;
  128. }
  129. }
  130. }
  131. }
  132. return 0;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement