#include <bits/stdc++.h>
using namespace std;
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];
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++;
}
}
void solve() {
vector<int> dp(1 << g, INT_MAX), prev(1 << g);
dp[0] = 0;
prev[0] = -1;
for(int i = 1; i < (1 << g); i++) {
int sum = 0;
for(int j = 0; j < g; j++) {
if(i & (1 << j)) sum += cyc[j].size();
}
for(int j = 0; j < g; j++) {
if(i & (1 << j)) {
int s = cyc[j].size();
sum -= s;
int pot = max(dp[i ^ (1 << j)], ran[sum][sum + s - 1]);
if(pot < dp[i]) {
dp[i] = pot;
prev[i] = i ^ (1 << j);
}
sum += s;
}
}
}
vector<int> vis;
int ind = (1 << g) - 1;
while(ind != -1) {
vis.push_back(ind);
ind = prev[ind];
}
reverse(vis.begin(), vis.end());
int curn = 0;
for(int i = 1; i < vis.size(); i++) {
int cm = vis[i];
int pm = vis[i - 1];
for(int j = 0; j < g; j++) {
if((cm ^ pm) == (1 << j)) {
int s = cyc[j].size();
for(int k = 0; k < s; k++) {
outp[cyc[j][k]] = dis[curn][curn + s - 1][k];
}
curn += s;
break;
}
}
}
}
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();
solve();
int bv = INT_MIN;
for(int i = 0; i < n; i++) bv = max(bv, abs(outp[i] - outp[a[i]]));
cout << bv << "\n";
for(int i = 0; i < n - 1; i++) cout << outp[i] << " ";
cout << outp[n - 1] << "\n";
}
Compilation message
vrtic.cpp: In function 'void solve()':
vrtic.cpp:76:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
76 | for(int i = 1; i < vis.size(); i++) {
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
3420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
321 ms |
18220 KB |
Output is correct |
2 |
Incorrect |
2 ms |
1624 KB |
no bag with given amount of candy |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
343 ms |
19080 KB |
Output is correct |
2 |
Incorrect |
3 ms |
2648 KB |
no bag with given amount of candy |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
319 ms |
19036 KB |
Output is correct |
2 |
Incorrect |
3 ms |
2652 KB |
no bag with given amount of candy |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
312 ms |
19916 KB |
Output is correct |
2 |
Incorrect |
5 ms |
3416 KB |
no bag with given amount of candy |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
315 ms |
19924 KB |
Output is correct |
2 |
Incorrect |
5 ms |
3416 KB |
no bag with given amount of candy |
3 |
Halted |
0 ms |
0 KB |
- |