#include <bits/stdc++.h>
#define int long long
using namespace std;
/*
* Si prend plus petit : cout = 0
* sinon cout > 0
* Si N de chaque côté :
* a_1 <= ... <= a_n
* b_1 <= ... <= b_n
* Si b_1 <= a_1 : mieux de mettre avec a_1
* Sinon b_1 > a_1 :
* SI on met a_1 avec b_i, i > 1 et b_1 avec j, j > 1
* cout : max(bi - a1, max(b1 - aj, 0))
* S max(bi-aj, 0) = 0 : mieux echanger
* Tri par ordre croissant !
*/
const int nbFeuilles = 1 << 17;
int curCost[2 * nbFeuilles];
void maj(int pos, int val)
{
pos += nbFeuilles;
curCost[pos] = val;
while (pos > 1)
{
pos /= 2;
curCost[pos] = max(curCost[2*pos], curCost[2*pos+1]);
}
}
signed main(void)
{
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int N;
cin >> N;
vector<pair<int, int>> A(N+1);
vector<int> B(N);
for (int i(0); i <= N; ++i)
{
cin >> A[i].first;
A[i].second = i;
}
for (auto &v : B) cin >> v;
sort(A.begin(), A.end());
sort(B.begin(), B.end());
for (int i(0); i < N; ++i)
curCost[i + nbFeuilles] = A[i+1].first - B[i];
for (int i(nbFeuilles-1); i >= 1; --i)
curCost[i] = max(curCost[2*i], curCost[2*i+1]);
vector<int> sol(N+1);
sol[A[0].second] = max(0LL, curCost[1]);
for (int i(0); i < N; ++i)
{
maj(i, A[i].first - B[i]);
sol[A[i+1].second] = max(0LL, curCost[1]);
}
for (auto v: sol)
cout << v<< ' ';
cout << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1388 KB |
Output is correct |
2 |
Correct |
1 ms |
1388 KB |
Output is correct |
3 |
Correct |
1 ms |
1388 KB |
Output is correct |
4 |
Correct |
2 ms |
1388 KB |
Output is correct |
5 |
Correct |
2 ms |
1408 KB |
Output is correct |
6 |
Correct |
1 ms |
1388 KB |
Output is correct |
7 |
Correct |
1 ms |
1388 KB |
Output is correct |
8 |
Correct |
1 ms |
1388 KB |
Output is correct |
9 |
Correct |
1 ms |
1388 KB |
Output is correct |
10 |
Correct |
1 ms |
1388 KB |
Output is correct |
11 |
Correct |
1 ms |
1388 KB |
Output is correct |
12 |
Correct |
1 ms |
1388 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1388 KB |
Output is correct |
2 |
Correct |
1 ms |
1388 KB |
Output is correct |
3 |
Correct |
1 ms |
1388 KB |
Output is correct |
4 |
Correct |
2 ms |
1388 KB |
Output is correct |
5 |
Correct |
2 ms |
1408 KB |
Output is correct |
6 |
Correct |
1 ms |
1388 KB |
Output is correct |
7 |
Correct |
1 ms |
1388 KB |
Output is correct |
8 |
Correct |
1 ms |
1388 KB |
Output is correct |
9 |
Correct |
1 ms |
1388 KB |
Output is correct |
10 |
Correct |
1 ms |
1388 KB |
Output is correct |
11 |
Correct |
1 ms |
1388 KB |
Output is correct |
12 |
Correct |
1 ms |
1388 KB |
Output is correct |
13 |
Correct |
2 ms |
1516 KB |
Output is correct |
14 |
Correct |
2 ms |
1408 KB |
Output is correct |
15 |
Correct |
2 ms |
1388 KB |
Output is correct |
16 |
Correct |
1 ms |
1408 KB |
Output is correct |
17 |
Correct |
2 ms |
1516 KB |
Output is correct |
18 |
Correct |
2 ms |
1388 KB |
Output is correct |
19 |
Correct |
2 ms |
1408 KB |
Output is correct |
20 |
Correct |
2 ms |
1388 KB |
Output is correct |
21 |
Correct |
2 ms |
1388 KB |
Output is correct |
22 |
Correct |
2 ms |
1388 KB |
Output is correct |
23 |
Correct |
2 ms |
1388 KB |
Output is correct |
24 |
Correct |
2 ms |
1388 KB |
Output is correct |
25 |
Correct |
2 ms |
1516 KB |
Output is correct |
26 |
Correct |
2 ms |
1388 KB |
Output is correct |
27 |
Correct |
2 ms |
1388 KB |
Output is correct |
28 |
Correct |
2 ms |
1516 KB |
Output is correct |
29 |
Correct |
2 ms |
1388 KB |
Output is correct |
30 |
Correct |
2 ms |
1388 KB |
Output is correct |
31 |
Correct |
3 ms |
1516 KB |
Output is correct |
32 |
Correct |
2 ms |
1516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1388 KB |
Output is correct |
2 |
Correct |
1 ms |
1388 KB |
Output is correct |
3 |
Correct |
1 ms |
1388 KB |
Output is correct |
4 |
Correct |
2 ms |
1388 KB |
Output is correct |
5 |
Correct |
2 ms |
1408 KB |
Output is correct |
6 |
Correct |
1 ms |
1388 KB |
Output is correct |
7 |
Correct |
1 ms |
1388 KB |
Output is correct |
8 |
Correct |
1 ms |
1388 KB |
Output is correct |
9 |
Correct |
1 ms |
1388 KB |
Output is correct |
10 |
Correct |
1 ms |
1388 KB |
Output is correct |
11 |
Correct |
1 ms |
1388 KB |
Output is correct |
12 |
Correct |
1 ms |
1388 KB |
Output is correct |
13 |
Correct |
2 ms |
1516 KB |
Output is correct |
14 |
Correct |
2 ms |
1408 KB |
Output is correct |
15 |
Correct |
2 ms |
1388 KB |
Output is correct |
16 |
Correct |
1 ms |
1408 KB |
Output is correct |
17 |
Correct |
2 ms |
1516 KB |
Output is correct |
18 |
Correct |
2 ms |
1388 KB |
Output is correct |
19 |
Correct |
2 ms |
1408 KB |
Output is correct |
20 |
Correct |
2 ms |
1388 KB |
Output is correct |
21 |
Correct |
2 ms |
1388 KB |
Output is correct |
22 |
Correct |
2 ms |
1388 KB |
Output is correct |
23 |
Correct |
2 ms |
1388 KB |
Output is correct |
24 |
Correct |
2 ms |
1388 KB |
Output is correct |
25 |
Correct |
2 ms |
1516 KB |
Output is correct |
26 |
Correct |
2 ms |
1388 KB |
Output is correct |
27 |
Correct |
2 ms |
1388 KB |
Output is correct |
28 |
Correct |
2 ms |
1516 KB |
Output is correct |
29 |
Correct |
2 ms |
1388 KB |
Output is correct |
30 |
Correct |
2 ms |
1388 KB |
Output is correct |
31 |
Correct |
3 ms |
1516 KB |
Output is correct |
32 |
Correct |
2 ms |
1516 KB |
Output is correct |
33 |
Runtime error |
86 ms |
12140 KB |
Execution killed with signal 11 |
34 |
Halted |
0 ms |
0 KB |
- |