# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
244325 |
2020-07-03T15:50:33 Z |
Vimmer |
Vrtić (COCI18_vrtic) |
C++14 |
|
2000 ms |
262148 KB |
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-O3")
#pragma GCC optimize("Ofast")
//#pragma GCC optimize("fast-math")
//#pragma GCC optimize("no-stack-protector")
#define F first
#define S second
#define sz(x) int(x.size())
#define pb push_back
#define N 100101
#define M ll(1e9 + 7)
#define inf 1e9 + 1e9
using namespace std;
//using namespace __gnu_pbds;
typedef long double ld;
typedef long long ll;
typedef short int si;
typedef pair <int, int> par;
typedef array <int, 2> a2;
//typedef tree <int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
int a[160], b[160], kol[160], siz[160], cost[160][160], id, idr[160];
vector <int> koler[160], who[160];
vector <int> gr[160][160];
unordered_map <ll, int> f[160];
unordered_map <ll, pair <int, ll> > from[160];
ll mult[160];
vector <int> sizes;
int calc(int l, int r)
{
for (int i = l; i <= r; i += 2) gr[l][r].pb(b[i]);
reverse(gr[l][r].begin(), gr[l][r].end());
for (int i = l + 1; i <= r; i += 2) gr[l][r].pb(b[i]);
int ans = 0;
for (int i = 0; i < sz(gr[l][r]); i++) ans = max(ans, abs(gr[l][r][i] - gr[l][r][(i + 1) % sz(gr[l][r])]));
return ans;
}
int main()
{
//freopen("input.txt", "r", stdin); //freopen("output4.txt", "w", stdout);
ios_base::sync_with_stdio(0); istream::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n;
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);
for (int i = 0; i < n; i++)
for (int j = i; j < n; j++) cost[i][j] = calc(i, j);
memset(idr, -1, sizeof(idr));
for (int i = 0; i < n; i++)
if (idr[i] == -1)
{
idr[i] = id;
int nxt = a[i];
siz[id]++;
who[id].pb(i);
while (idr[nxt] == -1)
{
who[id].pb(nxt);
idr[nxt] = id;
nxt = a[nxt];
siz[id]++;
}
kol[siz[id]]++;
koler[siz[id]].pb(id);
if (kol[siz[id]] == 1) sizes.pb(siz[id]);
id++;
}
sort(sizes.begin(), sizes.end());
mult[sizes[0]] = 1;
ll sm = kol[sizes[0]];
for (int i = 1; i < sz(sizes); i++) {mult[sizes[i]] = mult[sizes[i - 1]] * (1 + kol[sizes[i - 1]]); sm += kol[sizes[i]] * mult[sizes[i]];}
f[0][0] = 0;
for (int r = 0; r < n; r++)
for (auto it : f[r])
{
int val = it.S;
ll ml = it.F;
for (int i = 0; i < sz(sizes); i++)
{
int st = sizes[i];
if (ml / mult[st] == kol[st]) continue;
ll nml = ml + mult[st];
int nval = max(val, cost[r][r + st - 1]);
if (f[r + st].find(nml) == f[r + st].end() || f[r + st][nml] > nval) {f[r + st][nml] = nval; from[r + st][nml] = {r, ml};}
}
}
cout << f[n][sm] << endl;
int ans[n];
int r = n;
ll ml = sm;
while (r != 0)
{
int nxt = from[r][ml].F;
ll nx = from[r][ml].S;
int st = r - nxt;
int id = koler[st].back();
koler[st].pop_back();
for (int i = 0; i < sz(who[id]); i++) ans[who[id][i]] = gr[nxt][r - 1][i];
r = nxt;
ml = nx;
}
for (int i = 0; i < n; i++) cout << ans[i] << " ";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
1152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
4352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
1024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
1024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
1024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1167 ms |
133664 KB |
Output is correct |
2 |
Execution timed out |
2105 ms |
173656 KB |
Time limit exceeded |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2094 ms |
199048 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2058 ms |
262148 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2090 ms |
236544 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2107 ms |
210352 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |