Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- using ll = long long;
- using ld = long double;
- using ull = unsigned long long;
- using pii = pair<int, int>;
- using pll = pair<ll, ll>;
- using pld = pair<ld, ld>;
- #define fi first
- #define se second
- #define pb push_back
- #define pf push_front
- #define mp make_pair
- #define ins insert
- #define btpc __builtin_popcount
- #define btclz __builtin_clz
- #define sz(x) (int)(x.size());
- #define all(x) x.begin(), x.end()
- #define debug(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
- mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
- int d4x[4] = {1, 0, -1, 0}; int d4y[4] = {0, 1, 0, -1};
- int d8x[8] = {0, 1, 1, 1, 0, -1, -1, -1};
- int d8y[8] = {1, 1, 0, -1, -1, -1, 0, 1};
- template<class X, class Y>
- bool minimize(X &x, const Y &y) {
- if (x > y)
- {
- x = y;
- return true;
- }
- return false;
- }
- template<class X, class Y>
- bool maximize(X &x, const Y &y) {
- if (x < y)
- {
- x = y;
- return true;
- }
- return false;
- }
- const int MOD = 1e9 + 7; //998244353
- template<class X, class Y>
- void add(X &x, const Y &y) {
- x = (x + y);
- if(x >= MOD) x -= MOD;
- }
- template<class X, class Y>
- void sub(X &x, const Y &y) {
- x = (x - y);
- if(x < 0) x += MOD;
- }
- /* Author : Le Ngoc Bao Anh, 11A5, LQD High School for Gifted Student*/
- const ll INF = 1e9;
- const int N = 500 + 10;
- struct SOL {
- int n;
- vector<int> state;
- ll Time = 0;
- SOL(int _n = 0) {
- n = _n;
- state.resize(n + 2);
- for(int i = 1; i <= n; i++) state[i] = 0;
- Time = 0;
- }
- bool operator < (const SOL & temp) const {
- return Time < temp.Time;
- }
- } ans[N];
- int c[N];
- int dist[N][N];
- int bit[N], d[N];
- vector<int> res[N];
- void solve() {
- int n, m; cin >> n >> m;
- for(int i = 1; i <= n; i++) cin >> c[i];
- for(int i = 0; i <= n; i++) {
- for(int j = 0; j <= n; j++) {
- cin >> dist[i][j];
- }
- }
- int Q = (INF / 2) / n / max(1, (m / 2));
- int D = 200;
- int rep = Q / D;
- auto check = [&](SOL &a, int pos) -> bool {
- if(pos > 2 && a.state[pos - 2] && a.state[pos - 1]) return false;
- if(pos > 1 && pos < n && a.state[pos - 1] && a.state[pos + 1]) return false;
- if(pos + 2 <= n && a.state[pos + 1] && a.state[pos + 2]) return false;
- return true;
- };
- auto cal_cost = [&](SOL &a) -> void {
- ll ans = 0;
- int cnt = 0;
- for(int i = 1; i <= n; i++) if(a.state[i] == 1) bit[++cnt] = i;
- sort(bit + 1, bit + 1 + cnt, [&](int _a, int _b) {
- return c[_a] > c[_b];
- });
- for(int i = 1; i <= m; i++) d[i] = 0;
- for(int i = 1; i <= cnt; i++) {
- int cand = 1;
- for(int j = 2; j <= m; j++) if(d[j] < d[cand]) cand = j;
- d[cand] += c[bit[i]];
- }
- int last = 0;
- for(int i = 1; i <= n; i++) {
- if(a.state[i] == 0) {
- ans += dist[last][i];
- last = i;
- }
- }
- for(int i = 1; i <= m; i++) maximize(ans, d[i]);
- a.Time = ans;
- };
- for(int i = 1; i <= D; i++) {
- ans[i] = SOL(n);
- int b = min(10, n / 3);
- for(int j = 1; j <= b; j++) {
- int r = rng() % n + 1;
- if(check(ans[i], r)) ans[i].state[r] = 1;
- }
- int last = 0;
- for(int j = 1; j <= n; j++) {
- if(ans[i].state[j]) continue;
- if(!check(ans[i], j)) continue;
- int t = rng() % 4;
- if(t == 1) {
- ans[i].state[j] = 1;
- continue;
- }
- if(dist[last][j] > c[j]) {
- ans[i].state[j] = 1;
- } else last = j;
- }
- cal_cost(ans[i]);
- }
- for(int _ = 1; _ <= rep; _++) {
- for(int r = 1; r <= D; r++) {
- int son = D + r;
- int mom = rng() % D + 1;
- int dad = rng() % D + 1;
- while(dad == mom) dad = rng() % D + 1;
- ans[son] = SOL(n);
- int p = rng() % (n - 1) + 1;
- for(int i = 1; i <= p; i++) {
- ans[son].state[i] = ans[mom].state[i];
- }
- for(int i = p + 1; i <= n; i++) {
- if(ans[dad].state[i]) {
- if(check(ans[son], i)) ans[son].state[i] = 1;
- }
- }
- while(true) {
- int p = rng() % n + 1;
- if(ans[son].state[p] == 1) {
- ans[son].state[p] = 0;
- break;
- } else {
- if(check(ans[son], p)) {
- ans[son].state[p] = 1;
- break;
- }
- }
- }
- cal_cost(ans[son]);
- }
- sort(ans + 1, ans + 1 + 2 * D);
- }
- int cnt = 0;
- for(int i = 1; i <= n; i++) if(ans[1].state[i] == 1) bit[++cnt] = i;
- sort(bit + 1, bit + 1 + cnt, [&](int _a, int _b) {
- return c[_a] > c[_b];
- });
- for(int i = 1; i <= m; i++) d[i] = 0;
- for(int i = 1; i <= cnt; i++) {
- int cand = 1;
- for(int j = 2; j <= m; j++) if(d[j] < d[cand]) cand = j;
- d[cand] += c[bit[i]];
- res[cand].pb(bit[i]);
- }
- for(int i = 1; i <= m; i++) {
- int sz = sz(res[i]);
- cout << sz << " ";
- sort(all(res[i]));
- for(int j = 0; j < sz; j++) cout << res[i][j] << " ";
- cout << endl;
- }
- }
- int main()
- {
- ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
- #ifndef ONLINE_JUDGE
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #else
- //online
- #endif
- int tc = 1, ddd = 0;
- // cin >> tc;
- while(tc--) {
- //ddd++;
- //cout << "Case #" << ddd << ": ";
- solve();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement