#include<bits/stdc++.h>
//#define int long long
#define pb push_back
#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
#define MOD 1000000007
#define INF 1e18
#define fi first
#define se second
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORD(i,a,b) for(int i=a;i>=b;i--)
#define sz(a) ((int)(a).size())
#define endl '\n'
#define pi 3.14159265359
#define TASKNAME "vrtic"
template<typename T> bool maximize(T &res, const T &val) { if (res < val){ res = val; return true; }; return false; }
template<typename T> bool minimize(T &res, const T &val) { if (res > val){ res = val; return true; }; return false; }
using namespace std;
typedef pair<int,int> ii;
typedef pair<int,ii> iii;
typedef vector<int> vi;
const int MAXN = 159;
int n;
int best_friend[MAXN], bag_of_candy[MAXN];
namespace subtask1{
bool checkSubtask1(){
for(int i = 1; i <= n; i++){
if (best_friend[i] != i % n + 1) return false;
}
return true;
}
int arr[MAXN];
void solveSubtask1(){
int res = 0, splitting_point = 0;
sort(bag_of_candy + 1, bag_of_candy + 1 + n);
int cur = 0;
for(int i = 3; i <= n; i++){
maximize(res, abs(bag_of_candy[i] - bag_of_candy[i - 2]));
}
cout << res << endl;
for(int i = 1; i <= n; i += 2){
cout << bag_of_candy[i] << ' ';
}
for(int i = n - (n & 1); i > 0; i -= 2){
cout << bag_of_candy[i] << ' ';
}
cout << endl;
}
}
namespace subtask2{ //not usable yet
bool checkSubtask2(){
return n <= 20;
}
int MinDistriRange[MAXN][MAXN], ok[MAXN], answer[MAXN];
vector<vector<int>> Listcycle;
vector<ii> Nxtmask;
int dp[21][(1 << 20)], trace[21][(1 << 20)];
void prep(){
sort(bag_of_candy + 1, bag_of_candy + 1 + n);
for(int i = 1; i <= n; i++){
for(int j = i; j <= n; j++){
int length = j - i + 1;
if (length == 1) MinDistriRange[i][j] = 0;
else if (length == 2) MinDistriRange[i][j] = abs(bag_of_candy[j] - bag_of_candy[i]);
else {
for(int k = i + 2; k <= j; k++){
maximize(MinDistriRange[i][j], abs(bag_of_candy[k] - bag_of_candy[k - 2]) );
}
}
}
}
}
void AssignPermutation(int pos, int msk){
if (pos == 0) return;
int taken = trace[pos][msk];
int numbit = __builtin_popcount(taken);
vector<int> arr;
vector<int> ans;
for(int i = 0; i < n; i++){
if (taken & (1 << i)) arr.pb(bag_of_candy[i + 1]);
}
for(int i = 0; i < numbit; i += 2){
ans.pb(arr[i]);
}
for(int i = numbit - 1 - !((numbit - 1)&1); i >= 0; i -= 2){
ans.pb(arr[i]);
}
for(int i = 0; i < Listcycle[pos - 1].size(); i++){
answer[Listcycle[pos - 1][i]] = ans[i];
}
AssignPermutation(pos - 1, msk ^ taken);
}
void solveSubtask2(){
prep();
memset(ok, 0, sizeof(ok));
int numcycle = 0;
for(int i = 1; i <= n; i++){
if (ok[i]) continue;
vector<int> oneCycle;
numcycle++;
for(int j = i; !ok[j] ; j = best_friend[j]){
oneCycle.pb(j);
ok[j] = 1;
}
Listcycle.pb(oneCycle);
}
memset(dp, 0x3f, sizeof(dp));
sort(Listcycle.begin(), Listcycle.end(), [&](const vector<int> &a, const vector<int> &b){
return a.size() > b.size();
});
queue<array<int, 3>> q;
q.push({0, 0, 0});
dp[0][0] = 0;
int sz = 0;
while(!q.empty()){
int du = q.front()[0];
int pos = q.front()[1];
int msk = q.front()[2];
int testmsk = 0;
q.pop();
if (du > dp[pos][msk] or pos == (int) Listcycle.size()) continue;
int sz = Listcycle[pos].size();
for(int i = 0; i < sz; i++){
testmsk <<= 1;
testmsk++;
}
// cout << sz << ' ' << testmsk << endl;
for(int j = 0; j < n + 1 - sz; j++){
if (((testmsk << j) & msk) == 0) {
if (minimize(dp[pos + 1][msk ^ (testmsk << j)], max(du, MinDistriRange[j + 1][j + sz]))){
q.push({dp[pos + 1][msk ^ (testmsk << j)], pos + 1, msk ^ (testmsk << j) });
trace[pos + 1][msk ^ (testmsk << j)] = (testmsk << j);
}
}
}
}
cout << dp[Listcycle.size()][(1 << n) - 1] << endl;
AssignPermutation(Listcycle.size(), (1 << n) - 1);
for(int i = 1; i <= n; i++){
cout << answer[i] << ' ';
}
cout << endl;
}
}
struct hash_pair {
template <class T1, class T2>
size_t operator()(const pair<T1, T2>& p) const
{
auto hash1 = hash<T1>{}(p.first);
auto hash2 = hash<T2>{}(p.second);
if (hash1 != hash2) {
return hash1 ^ hash2;
}
// If hash1 == hash2, their XOR is zero.
return hash1;
}
};
namespace subtask3{
int MinDistriRange[MAXN][MAXN], ok[MAXN], answer[MAXN], add[MAXN][MAXN];
vector<vector<int>> Listcycle;
unordered_map<ii, int, hash_pair> dp;
unordered_map<ii, int, hash_pair> trace;
map<int, bitset<MAXN>> transfer;
const int base = 113;
int binpow(int a,int b){
int res = 1;
while(b > 0){
if (b & 1) res = (1LL * res * a) % MOD;
b >>= 1;
a = (1LL * a * a) % MOD;
}
return res;
}
void prep(){
sort(bag_of_candy + 1, bag_of_candy + 1 + n);
for(int i = 1; i <= n; i++){
for(int j = i; j <= n; j++){
int length = j - i + 1;
if (length == 1) MinDistriRange[i][j] = 0;
else if (length == 2) MinDistriRange[i][j] = abs(bag_of_candy[j] - bag_of_candy[i]);
else {
for(int k = i + 2; k <= j; k++){
maximize(MinDistriRange[i][j], abs(bag_of_candy[k] - bag_of_candy[k - 2]) );
}
}
}
}
for(int i = 0; i <= n; i++){
for(int j = i; j <= n; j++){
add[i][j] -= j - i + 1;
for(int k = i; k <= j; k++){
add[i][j] = (1LL * (1LL * add[i][j] + 1LL * binpow(2, 1 + k * base)) % MOD + MOD) % MOD;
}
}
}
}
bitset<MAXN> Mask;
bitset<MAXN> prepmask[MAXN];
int hashBitSet(bitset<MAXN> &b){
int res = 0;
for(int i = 0; i < MAXN; i++){
res = 1LL * (1LL * res + 1LL * binpow(b[i] + 1, 1 + i * base)) % MOD;;
}
transfer[res] = b;
return res;
}
void Assign(int pos, bitset<MAXN> &mask){
if (pos == 0) return;
int getcode = hashBitSet(mask);
int prvcode = trace[ii(pos, getcode)];
bitset<MAXN> tmp = transfer[prvcode];
bitset<MAXN> taken = mask ^ tmp;
vector<int> ans;
vector<int> arr;
for(int i = 0; i < n; i++){
if (taken[i] == 1) arr.pb(bag_of_candy[i + 1]);
}
int numbit = taken.count();
for(int i = 0; i < numbit; i += 2){
ans.pb(arr[i]);
}
for(int i = numbit - 1 - !((numbit - 1) & 1); i >= 0; i -= 2){
ans.pb(arr[i]);
}
for(int i = 0; i < Listcycle[pos - 1].size(); i++){
answer[Listcycle[pos - 1][i]] = ans[i];
}
mask = tmp;
Assign(pos - 1, mask);
}
void solveSubtask3(){
prep();
memset(ok, 0, sizeof(ok));
int numcycle = 0;
for(int i = 1; i <= n; i++){
if (ok[i]) continue;
vector<int> oneCycle;
numcycle++;
for(int j = i; !ok[j] ; j = best_friend[j]){
oneCycle.pb(j);
ok[j] = 1;
}
Listcycle.pb(oneCycle);
}
sort(Listcycle.begin(), Listcycle.end(), [&](const vector<int> &a, const vector<int> &b){
return a.size() > b.size();
});
Mask.reset();
int gethash = hashBitSet(Mask);
queue<array<int, 3>> q;
cerr << Listcycle.size() << endl;
bitset<MAXN> testmask;
prepmask[0][0] = 1;
for(int i = 1; i <= n; i++){
prepmask[i] = prepmask[i - 1];
prepmask[i][i] = 1;
}
dp[ii(0, gethash)] = 0;
q.push({0, 0, gethash});
int cnt = 0;
// cout << gethash << endl;
Mask[0] = 1;
// cout << hashBitSet(Mask) << ' ' << gethash + add[0][0] << endl;
while(!q.empty()){
int du = q.front()[0];
int pos = q.front()[1];
int hashBIT = q.front()[2];
q.pop();
// cerr << du << ' ' << pos << ' ' << hashBIT << endl;
if (du > dp[ii(pos, hashBIT)] or pos == (int) Listcycle.size()) continue;
cnt++;
Mask = transfer[hashBIT];
int sz = Listcycle[pos].size();
testmask = prepmask[sz - 1];
for(int j = 0; j < n - sz + 1; j++){
if ((Mask & testmask).count() == 0){
Mask ^= testmask;
int newmsk = (1LL * hashBIT + 1LL * add[j][j + sz - 1]) % MOD;
transfer[newmsk] = Mask;
if (dp.find({pos + 1, newmsk}) == dp.end()) dp[ii(pos + 1, newmsk)] = 2e9;
if (minimize(dp[ii(pos + 1, newmsk)], max(du, MinDistriRange[j + 1][j + sz]) )){
trace[ii(pos + 1, newmsk)] = hashBIT;
q.push({max(du, MinDistriRange[j + 1][j + sz]), pos + 1, newmsk});
}
Mask ^= testmask;
}
testmask <<= 1;
}
}
// cout << cnt << endl;
for(int i = 0; i < n; i++){
Mask[i] = 1;
}
int getcode = hashBitSet(Mask);
// cerr << Listcycle.size() << ' ' << getcode << endl;
cout << dp[ii(Listcycle.size(), getcode)] << endl;
Assign(Listcycle.size(), Mask);
// for(int i = 1; i <= n; i++){
// cout << answer[i] << ' ';
// }
// cout << endl;
}
}
main()
{
fast;
if (fopen(TASKNAME".inp","r")){
freopen(TASKNAME".inp","r",stdin);
freopen(TASKNAME".out","w",stdout);
}
cin >> n;
for(int i = 1; i <= n; i++){
cin >> best_friend[i];
}
for(int i = 1; i <= n; i++){
cin >> bag_of_candy[i];
}
// if (subtask1::checkSubtask1()) return subtask1::solveSubtask1(), 0;
// if (subtask2::checkSubtask2()) return subtask2::solveSubtask2(), 0;
return subtask3::solveSubtask3(), 0;
}
/**
Warning:
- MLE / TLE?
- Gioi han mang?
- Gia tri max phai luon gan cho -INF
- long long co can thiet khong?
- tran mang.
- code can than hon
- Nho sinh test de tranh RTE / TLE
--> Coi lai truoc khi nop
**/
Compilation message
vrtic.cpp: In function 'void subtask1::solveSubtask1()':
vrtic.cpp:36:22: warning: unused variable 'splitting_point' [-Wunused-variable]
36 | int res = 0, splitting_point = 0;
| ^~~~~~~~~~~~~~~
vrtic.cpp:38:13: warning: unused variable 'cur' [-Wunused-variable]
38 | int cur = 0;
| ^~~
vrtic.cpp: In function 'void subtask2::AssignPermutation(int, int)':
vrtic.cpp:98:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
98 | for(int i = 0; i < Listcycle[pos - 1].size(); i++){
| ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
vrtic.cpp: In function 'void subtask2::solveSubtask2()':
vrtic.cpp:125:13: warning: unused variable 'sz' [-Wunused-variable]
125 | int sz = 0;
| ^~
vrtic.cpp: In function 'void subtask3::Assign(int, std::bitset<159>&)':
vrtic.cpp:245:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
245 | for(int i = 0; i < Listcycle[pos - 1].size(); i++){
| ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
vrtic.cpp: At global scope:
vrtic.cpp:331:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
331 | main()
| ^~~~
vrtic.cpp: In function 'int main()':
vrtic.cpp:335:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
335 | freopen(TASKNAME".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
vrtic.cpp:336:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
336 | freopen(TASKNAME".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
348 KB |
Unexpected end of file - int32 expected |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
36 ms |
2396 KB |
Unexpected end of file - int32 expected |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
95 ms |
10232 KB |
Unexpected end of file - int32 expected |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
86 ms |
10072 KB |
Unexpected end of file - int32 expected |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
96 ms |
10112 KB |
Unexpected end of file - int32 expected |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2045 ms |
141848 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2054 ms |
162048 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2041 ms |
198632 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2056 ms |
224740 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2031 ms |
163672 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |