#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 << 18;
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 |
3 ms |
2412 KB |
Output is correct |
2 |
Correct |
2 ms |
2412 KB |
Output is correct |
3 |
Correct |
2 ms |
2412 KB |
Output is correct |
4 |
Correct |
3 ms |
2412 KB |
Output is correct |
5 |
Correct |
2 ms |
2412 KB |
Output is correct |
6 |
Correct |
2 ms |
2412 KB |
Output is correct |
7 |
Correct |
3 ms |
2412 KB |
Output is correct |
8 |
Correct |
2 ms |
2412 KB |
Output is correct |
9 |
Correct |
2 ms |
2412 KB |
Output is correct |
10 |
Correct |
2 ms |
2412 KB |
Output is correct |
11 |
Correct |
2 ms |
2412 KB |
Output is correct |
12 |
Correct |
2 ms |
2412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2412 KB |
Output is correct |
2 |
Correct |
2 ms |
2412 KB |
Output is correct |
3 |
Correct |
2 ms |
2412 KB |
Output is correct |
4 |
Correct |
3 ms |
2412 KB |
Output is correct |
5 |
Correct |
2 ms |
2412 KB |
Output is correct |
6 |
Correct |
2 ms |
2412 KB |
Output is correct |
7 |
Correct |
3 ms |
2412 KB |
Output is correct |
8 |
Correct |
2 ms |
2412 KB |
Output is correct |
9 |
Correct |
2 ms |
2412 KB |
Output is correct |
10 |
Correct |
2 ms |
2412 KB |
Output is correct |
11 |
Correct |
2 ms |
2412 KB |
Output is correct |
12 |
Correct |
2 ms |
2412 KB |
Output is correct |
13 |
Correct |
2 ms |
2412 KB |
Output is correct |
14 |
Correct |
4 ms |
2412 KB |
Output is correct |
15 |
Correct |
4 ms |
2412 KB |
Output is correct |
16 |
Correct |
3 ms |
2412 KB |
Output is correct |
17 |
Correct |
3 ms |
2540 KB |
Output is correct |
18 |
Correct |
3 ms |
2412 KB |
Output is correct |
19 |
Correct |
3 ms |
2412 KB |
Output is correct |
20 |
Correct |
6 ms |
2412 KB |
Output is correct |
21 |
Correct |
3 ms |
2540 KB |
Output is correct |
22 |
Correct |
3 ms |
2412 KB |
Output is correct |
23 |
Correct |
3 ms |
2412 KB |
Output is correct |
24 |
Correct |
4 ms |
2412 KB |
Output is correct |
25 |
Correct |
3 ms |
2540 KB |
Output is correct |
26 |
Correct |
3 ms |
2540 KB |
Output is correct |
27 |
Correct |
3 ms |
2688 KB |
Output is correct |
28 |
Correct |
3 ms |
2540 KB |
Output is correct |
29 |
Correct |
3 ms |
2432 KB |
Output is correct |
30 |
Correct |
4 ms |
2540 KB |
Output is correct |
31 |
Correct |
3 ms |
2540 KB |
Output is correct |
32 |
Correct |
3 ms |
2540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2412 KB |
Output is correct |
2 |
Correct |
2 ms |
2412 KB |
Output is correct |
3 |
Correct |
2 ms |
2412 KB |
Output is correct |
4 |
Correct |
3 ms |
2412 KB |
Output is correct |
5 |
Correct |
2 ms |
2412 KB |
Output is correct |
6 |
Correct |
2 ms |
2412 KB |
Output is correct |
7 |
Correct |
3 ms |
2412 KB |
Output is correct |
8 |
Correct |
2 ms |
2412 KB |
Output is correct |
9 |
Correct |
2 ms |
2412 KB |
Output is correct |
10 |
Correct |
2 ms |
2412 KB |
Output is correct |
11 |
Correct |
2 ms |
2412 KB |
Output is correct |
12 |
Correct |
2 ms |
2412 KB |
Output is correct |
13 |
Correct |
2 ms |
2412 KB |
Output is correct |
14 |
Correct |
4 ms |
2412 KB |
Output is correct |
15 |
Correct |
4 ms |
2412 KB |
Output is correct |
16 |
Correct |
3 ms |
2412 KB |
Output is correct |
17 |
Correct |
3 ms |
2540 KB |
Output is correct |
18 |
Correct |
3 ms |
2412 KB |
Output is correct |
19 |
Correct |
3 ms |
2412 KB |
Output is correct |
20 |
Correct |
6 ms |
2412 KB |
Output is correct |
21 |
Correct |
3 ms |
2540 KB |
Output is correct |
22 |
Correct |
3 ms |
2412 KB |
Output is correct |
23 |
Correct |
3 ms |
2412 KB |
Output is correct |
24 |
Correct |
4 ms |
2412 KB |
Output is correct |
25 |
Correct |
3 ms |
2540 KB |
Output is correct |
26 |
Correct |
3 ms |
2540 KB |
Output is correct |
27 |
Correct |
3 ms |
2688 KB |
Output is correct |
28 |
Correct |
3 ms |
2540 KB |
Output is correct |
29 |
Correct |
3 ms |
2432 KB |
Output is correct |
30 |
Correct |
4 ms |
2540 KB |
Output is correct |
31 |
Correct |
3 ms |
2540 KB |
Output is correct |
32 |
Correct |
3 ms |
2540 KB |
Output is correct |
33 |
Correct |
124 ms |
11432 KB |
Output is correct |
34 |
Correct |
180 ms |
15596 KB |
Output is correct |
35 |
Correct |
121 ms |
15360 KB |
Output is correct |
36 |
Correct |
114 ms |
15596 KB |
Output is correct |
37 |
Correct |
118 ms |
16108 KB |
Output is correct |
38 |
Correct |
120 ms |
15852 KB |
Output is correct |
39 |
Correct |
219 ms |
14956 KB |
Output is correct |
40 |
Correct |
130 ms |
14876 KB |
Output is correct |
41 |
Correct |
113 ms |
14984 KB |
Output is correct |
42 |
Correct |
112 ms |
14956 KB |
Output is correct |
43 |
Correct |
180 ms |
14860 KB |
Output is correct |
44 |
Correct |
109 ms |
14444 KB |
Output is correct |
45 |
Correct |
116 ms |
14572 KB |
Output is correct |
46 |
Correct |
133 ms |
14444 KB |
Output is correct |
47 |
Correct |
113 ms |
14716 KB |
Output is correct |
48 |
Correct |
109 ms |
14700 KB |
Output is correct |
49 |
Correct |
120 ms |
15340 KB |
Output is correct |
50 |
Correct |
129 ms |
15340 KB |
Output is correct |
51 |
Correct |
125 ms |
15340 KB |
Output is correct |
52 |
Correct |
119 ms |
15340 KB |
Output is correct |
53 |
Correct |
119 ms |
15340 KB |
Output is correct |