// Problem :
//
//
// Contest : DMOJ
// URL : https://dmoj.ca/problem/apio15p3
// Memory Limit : 256 MB
// Time Limit : 1000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)
#include <bits/stdc++.h>
using namespace std;
struct FenwickTree{
long long bit[200005];
int N;
void upd(int n, int v){
while(n <= N){
bit[n] += v;
n += n&-n;
}
}
long long query(int n){
long long ret = 0;
while(n){
ret += bit[n];
n -= n&-n;
}
return ret;
}
int clmb(long long t){
int ret = 0;
long long sum = 0;
for(int d = 17; d>=0; d--){
if(ret+(1<<d) <= N && bit[ret+(1<<d)] + sum < t){
sum += bit[ret+(1<<d)];
ret += (1<<d);
}
}
return ret+1;
}
void clr(){
fill(bit, bit+1+N, 0);
}
};
int T, N;
pair<int, int> arr[100005];
vector<int> cmp;
FenwickTree cnt, sum;
long long pre[100005], suf[100005];
int getidx(int n){
return lower_bound(cmp.begin(), cmp.end(), n) - cmp.begin() + 1;
}
int main(){
cin.sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> T >> N;
long long c = 0;
int idx = 0;
for(int i =1 ; i<=N; i++){
char a, b;
int A, B;
cin >> a >> A >> b >> B;
if(a == b){
c += abs(A-B);
}
else{
c++;
if(A > B){
swap(A, B);
}
cmp.push_back(A);
cmp.push_back(B);
arr[++idx] = {A, B};
}
}
N = idx;
sort(arr+1, arr+1+N, [](pair<int, int> a, pair<int, int> b){
return a.first+a.second < b.first+b.second;
}
);
sort(cmp.begin(), cmp.end());
cmp.erase(unique(cmp.begin(), cmp.end()), cmp.end());
sum.N = cnt.N = cmp.size();
for(int i = 1; i<=N; i++){
int a = getidx(arr[i].first), b = getidx(arr[i].second);
cnt.upd(a, 1);
cnt.upd(b, 1);
sum.upd(a, arr[i].first);
sum.upd(b, arr[i].second);
int idx = cnt.clmb(i);
long long pos = cmp[idx-1];
pre[i] = pos*(cnt.query(idx)) - sum.query(idx);
pre[i] += sum.query(sum.N) - sum.query(idx) - pos*(2*i-cnt.query(idx));
}
sum.clr();
cnt.clr();
for(int i = N; i; i--){
int a = getidx(arr[i].first), b = getidx(arr[i].second);
cnt.upd(a, 1);
cnt.upd(b, 1);
sum.upd(a, arr[i].first);
sum.upd(b, arr[i].second);
int idx = cnt.clmb(N-i+1);
long long pos = cmp[idx-1];
suf[i] = pos*(cnt.query(idx)) - sum.query(idx);
suf[i] += sum.query(sum.N) - sum.query(idx) - pos*(2*(N-i+1)-cnt.query(idx));
}
long long out = pre[N];
if(T == 2){
for(int i = 0; i<=N; i++){
out = min(pre[i] + suf[i+1], out);
}
}
cout << out+c << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
6 ms |
512 KB |
Output is correct |
5 |
Correct |
6 ms |
512 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
512 KB |
Output is correct |
9 |
Correct |
6 ms |
512 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
432 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
6 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
512 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
6 ms |
512 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
38 ms |
4472 KB |
Output is correct |
13 |
Correct |
179 ms |
9072 KB |
Output is correct |
14 |
Correct |
105 ms |
4848 KB |
Output is correct |
15 |
Correct |
100 ms |
5616 KB |
Output is correct |
16 |
Correct |
44 ms |
5232 KB |
Output is correct |
17 |
Correct |
116 ms |
9068 KB |
Output is correct |
18 |
Correct |
116 ms |
8684 KB |
Output is correct |
19 |
Correct |
135 ms |
9068 KB |
Output is correct |
20 |
Correct |
50 ms |
5616 KB |
Output is correct |
21 |
Correct |
126 ms |
8816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
6 ms |
512 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
6 ms |
512 KB |
Output is correct |
21 |
Correct |
6 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
4 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
512 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
6 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
512 KB |
Output is correct |
21 |
Correct |
5 ms |
512 KB |
Output is correct |
22 |
Correct |
6 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
37 ms |
4464 KB |
Output is correct |
26 |
Correct |
64 ms |
4592 KB |
Output is correct |
27 |
Correct |
167 ms |
8176 KB |
Output is correct |
28 |
Correct |
180 ms |
9072 KB |
Output is correct |
29 |
Correct |
174 ms |
9072 KB |
Output is correct |
30 |
Correct |
90 ms |
5108 KB |
Output is correct |
31 |
Correct |
45 ms |
5232 KB |
Output is correct |
32 |
Correct |
115 ms |
9100 KB |
Output is correct |
33 |
Correct |
114 ms |
8656 KB |
Output is correct |
34 |
Correct |
135 ms |
8920 KB |
Output is correct |
35 |
Correct |
48 ms |
5488 KB |
Output is correct |
36 |
Correct |
123 ms |
8820 KB |
Output is correct |
37 |
Correct |
37 ms |
4464 KB |
Output is correct |