Advertisement
BaoJIaoPisu

Untitled

Apr 4th, 2022
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.13 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. using ll = long long;
  6. using ld = long double;
  7. using ull = unsigned long long;
  8.  
  9. using pii = pair<int, int>;
  10. using pll = pair<ll, ll>;
  11. using pld = pair<ld, ld>;
  12.  
  13. #define fi first
  14. #define se second
  15. #define pb push_back
  16. #define pf push_front
  17. #define mp make_pair
  18. #define ins insert
  19. #define btpc __builtin_popcount
  20. #define btclz __builtin_clz
  21.  
  22. #define sz(x) (int)(x.size());
  23. #define all(x) x.begin(), x.end()
  24. #define debug(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
  25.  
  26. mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  27.  
  28. int d4x[4] = {1, 0, -1, 0}; int d4y[4] = {0, 1, 0, -1};
  29. int d8x[8] = {0, 1, 1, 1, 0, -1, -1, -1};
  30. int d8y[8] = {1, 1, 0, -1, -1, -1, 0, 1};
  31.  
  32. template<class X, class Y>
  33. bool minimize(X &x, const Y &y) {
  34. if (x > y)
  35. {
  36. x = y;
  37. return true;
  38. }
  39. return false;
  40. }
  41. template<class X, class Y>
  42. bool maximize(X &x, const Y &y) {
  43. if (x < y)
  44. {
  45. x = y;
  46. return true;
  47. }
  48. return false;
  49. }
  50.  
  51. const int MOD = 1e9 + 7; //998244353
  52.  
  53. template<class X, class Y>
  54. void add(X &x, const Y &y) {
  55. x = (x + y);
  56. if(x >= MOD) x -= MOD;
  57. }
  58.  
  59. template<class X, class Y>
  60. void sub(X &x, const Y &y) {
  61. x = (x - y);
  62. if(x < 0) x += MOD;
  63. }
  64.  
  65. /* Author : Le Ngoc Bao Anh, 11A5, LQD High School for Gifted Student*/
  66.  
  67. const ll INF = 1e9;
  68. const int N = 500 + 10;
  69.  
  70. struct SOL {
  71. int n;
  72. vector<int> state;
  73. ll Time = 0;
  74.  
  75. SOL(int _n = 0) {
  76. n = _n;
  77. state.resize(n + 2);
  78. for(int i = 1; i <= n; i++) state[i] = 0;
  79. Time = 0;
  80. }
  81.  
  82. bool operator < (const SOL & temp) const {
  83. return Time < temp.Time;
  84. }
  85. } ans[N];
  86.  
  87. int c[N];
  88. int dist[N][N];
  89. int bit[N], d[N];
  90. vector<int> res[N];
  91.  
  92. void solve() {
  93. int n, m; cin >> n >> m;
  94. for(int i = 1; i <= n; i++) cin >> c[i];
  95.  
  96. for(int i = 0; i <= n; i++) {
  97. for(int j = 0; j <= n; j++) {
  98. cin >> dist[i][j];
  99. }
  100. }
  101.  
  102. int Q = (INF / 2) / n / max(1, (m / 2));
  103. int D = 200;
  104. int rep = Q / D;
  105.  
  106. auto check = [&](SOL &a, int pos) -> bool {
  107. if(pos > 2 && a.state[pos - 2] && a.state[pos - 1]) return false;
  108. if(pos > 1 && pos < n && a.state[pos - 1] && a.state[pos + 1]) return false;
  109. if(pos + 2 <= n && a.state[pos + 1] && a.state[pos + 2]) return false;
  110. return true;
  111. };
  112.  
  113. auto cal_cost = [&](SOL &a) -> void {
  114. ll ans = 0;
  115. int cnt = 0;
  116. for(int i = 1; i <= n; i++) if(a.state[i] == 1) bit[++cnt] = i;
  117. sort(bit + 1, bit + 1 + cnt, [&](int _a, int _b) {
  118. return c[_a] > c[_b];
  119. });
  120.  
  121. for(int i = 1; i <= m; i++) d[i] = 0;
  122. for(int i = 1; i <= cnt; i++) {
  123. int cand = 1;
  124. for(int j = 2; j <= m; j++) if(d[j] < d[cand]) cand = j;
  125. d[cand] += c[bit[i]];
  126. }
  127.  
  128. int last = 0;
  129. for(int i = 1; i <= n; i++) {
  130. if(a.state[i] == 0) {
  131. ans += dist[last][i];
  132. last = i;
  133. }
  134. }
  135.  
  136. for(int i = 1; i <= m; i++) maximize(ans, d[i]);
  137. a.Time = ans;
  138. };
  139.  
  140. for(int i = 1; i <= D; i++) {
  141. ans[i] = SOL(n);
  142. int b = min(10, n / 3);
  143. for(int j = 1; j <= b; j++) {
  144. int r = rng() % n + 1;
  145. if(check(ans[i], r)) ans[i].state[r] = 1;
  146. }
  147.  
  148. int last = 0;
  149. for(int j = 1; j <= n; j++) {
  150. if(ans[i].state[j]) continue;
  151. if(!check(ans[i], j)) continue;
  152. int t = rng() % 4;
  153. if(t == 1) {
  154. ans[i].state[j] = 1;
  155. continue;
  156. }
  157.  
  158. if(dist[last][j] > c[j]) {
  159. ans[i].state[j] = 1;
  160. } else last = j;
  161. }
  162.  
  163. cal_cost(ans[i]);
  164. }
  165.  
  166. for(int _ = 1; _ <= rep; _++) {
  167. for(int r = 1; r <= D; r++) {
  168. int son = D + r;
  169. int mom = rng() % D + 1;
  170. int dad = rng() % D + 1;
  171. while(dad == mom) dad = rng() % D + 1;
  172.  
  173. ans[son] = SOL(n);
  174. int p = rng() % (n - 1) + 1;
  175. for(int i = 1; i <= p; i++) {
  176. ans[son].state[i] = ans[mom].state[i];
  177. }
  178.  
  179. for(int i = p + 1; i <= n; i++) {
  180. if(ans[dad].state[i]) {
  181. if(check(ans[son], i)) ans[son].state[i] = 1;
  182. }
  183. }
  184.  
  185. while(true) {
  186. int p = rng() % n + 1;
  187. if(ans[son].state[p] == 1) {
  188. ans[son].state[p] = 0;
  189. break;
  190. } else {
  191. if(check(ans[son], p)) {
  192. ans[son].state[p] = 1;
  193. break;
  194. }
  195. }
  196. }
  197.  
  198. cal_cost(ans[son]);
  199. }
  200.  
  201. sort(ans + 1, ans + 1 + 2 * D);
  202. }
  203.  
  204. int cnt = 0;
  205. for(int i = 1; i <= n; i++) if(ans[1].state[i] == 1) bit[++cnt] = i;
  206. sort(bit + 1, bit + 1 + cnt, [&](int _a, int _b) {
  207. return c[_a] > c[_b];
  208. });
  209.  
  210. for(int i = 1; i <= m; i++) d[i] = 0;
  211. for(int i = 1; i <= cnt; i++) {
  212. int cand = 1;
  213. for(int j = 2; j <= m; j++) if(d[j] < d[cand]) cand = j;
  214. d[cand] += c[bit[i]];
  215. res[cand].pb(bit[i]);
  216. }
  217.  
  218. for(int i = 1; i <= m; i++) {
  219. int sz = sz(res[i]);
  220. cout << sz << " ";
  221. sort(all(res[i]));
  222. for(int j = 0; j < sz; j++) cout << res[i][j] << " ";
  223. cout << endl;
  224. }
  225. }
  226.  
  227. int main()
  228. {
  229. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  230. #ifndef ONLINE_JUDGE
  231. freopen("input.txt", "r", stdin);
  232. freopen("output.txt", "w", stdout);
  233. #else
  234. //online
  235. #endif
  236.  
  237. int tc = 1, ddd = 0;
  238. // cin >> tc;
  239. while(tc--) {
  240. //ddd++;
  241. //cout << "Case #" << ddd << ": ";
  242. solve();
  243. }
  244. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement