# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
945275 | 2024-03-13T15:36:41 Z | RGBB | Vrtić (COCI18_vrtic) | C++14 | 1806 ms | 101584 KB |
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> using namespace std; using namespace __gnu_pbds; typedef pair<int, int> pi; const int MAX = 150; int n; int a[MAX], b[MAX], outp[MAX]; int ran[MAX][MAX]; vector<int> dis[MAX][MAX]; void calcRan() { for(int i = 0; i < n; i++) { for(int j = i; j < n; j++) { int tsi = j - i + 1; vector<int> sv(tsi), tout(tsi); for(int k = i; k <= j; k++) sv[k - i] = b[k]; tout[0] = sv[0]; for(int k = 1; k < tsi; k++) { if(k % 2) tout[(k + 1) / 2] = sv[k]; else tout[tsi - k / 2] = sv[k]; } ran[i][j] = abs(tout[tsi - 1] - tout[0]); for(int k = 0; k < tsi - 1; k++) ran[i][j] = max(ran[i][j], abs(tout[k + 1] - tout[k])); dis[i][j] = tout; } } } int g; vector<int> cyc[MAX]; vector<pi> mp; void calcCycles() { vector<bool> flag(n); for(int i = 0; i < n; i++) { if(flag[i]) continue; int cur = i; while(!flag[cur]) { flag[cur] = true; cyc[g].push_back(cur); cur = a[cur]; } g++; } map<int, int> tmp; for(int i = 0; i < g; i++) tmp[cyc[i].size()]++; for(auto& [i, j]: tmp) mp.push_back({i, j}); reverse(mp.begin(), mp.end()); } /* Hash func: Base 16 digit, base 150 */ const int MOD = 1e7 + 19; int origHash, base[16]; void calcBase() { base[0] = 1; for(int i = 1; i < 16; i++) base[i] = (150 * base[i - 1]) % MOD; } vector<int> ord; gp_hash_table<int, bool> memo; bool dfs(int p, vector<pi>& freq, int cap, int curHash) { if(memo.find(curHash) != memo.end()) return false; memo[curHash] = true; if(p == n) return true; for(int i = 0; i < freq.size(); i++) { auto& [a, b] = freq[i]; if(!b) continue; if(ran[p][p + a - 1] > cap) continue; b--; ord.push_back(a); curHash -= base[i]; curHash = (curHash + MOD) % MOD; if(dfs(p + a, freq, cap, curHash)) return true; b++; ord.pop_back(); curHash += base[i]; curHash %= MOD; } return false; } bool isGood(int cap) { ord.clear(); memo.clear(); vector<pi> freq = mp; return dfs(0, freq, cap, origHash); } void processAnswer() { vector<bool> flag(g); int sum = 0; for(int i: ord) { for(int j = 0; j < g; j++) { if(flag[j]) continue; if(cyc[j].size() != i) continue; flag[j] = true; for(int k = 0; k < i; k++) { outp[cyc[j][k]] = dis[sum][sum + i - 1][k]; } break; } sum += i; } for(int i = 0; i < n - 1; i++) cout << outp[i] << " "; cout << outp[n - 1] << "\n"; } void solve() { for(int i = 0; i < mp.size(); i++) { origHash += base[i] * mp[i].second; origHash %= MOD; } int l = 0; int r = 1e9; while(r - l > 1) { int m = (l + r) / 2; if(isGood(m)) r = m; else l = m; } if(isGood(l)) { cout << l << "\n"; processAnswer(); } else { cout << r << "\n"; isGood(r); processAnswer(); } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; for(int i = 0; i < n; i++) { cin >> a[i]; a[i]--; } for(int i = 0; i < n; i++) cin >> b[i]; sort(b, b + n); calcRan(); calcCycles(); calcBase(); solve(); }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 1 ms | 1116 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 4 ms | 3420 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 1 ms | 860 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 1 ms | 856 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 1 ms | 860 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 4 ms | 1884 KB | Output is correct |
2 | Correct | 42 ms | 4920 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 21 ms | 4072 KB | Output is correct |
2 | Correct | 124 ms | 14784 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 9 ms | 3388 KB | Output is correct |
2 | Correct | 335 ms | 27040 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 26 ms | 4844 KB | Output is correct |
2 | Correct | 683 ms | 49544 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 24 ms | 4920 KB | Output is correct |
2 | Correct | 1806 ms | 101584 KB | Output is correct |