#include "bits/stdc++.h"
#include<ext/pb_ds/assoc_container.hpp>
#define MAXN 200009
#define INF 1000000007
#define mp(x,y) make_pair(x,y)
#define all(v) v.begin(),v.end()
#define pb(x) push_back(x)
#define wr cout<<"----------------"<<endl;
#define ppb() pop_back()
#define tr(ii,c) for(__typeof((c).begin()) ii=(c).begin();ii!=(c).end();ii++)
#define ff first
#define ss second
#define my_little_dodge 46
#define debug(x) cerr<< #x <<" = "<< x<<endl;
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> PII;
typedef tree<PII, null_type, less<PII>, rb_tree_tag, tree_order_statistics_node_update> tes;
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
ll pre[MAXN],suf[MAXN];
vector<PII>v;
vector<int>comp;
bool cmp(PII x,PII y){
return (x.ff+x.ss<y.ff+y.ss);
}
tes s;
map<int,int>cnt;
pair<ll,int>F[MAXN],sum;
void update(int x,int y){
sum=mp(sum.ff+y,sum.ss+1);
for(;x<MAXN;x+=x&(-x))
F[x]=mp(F[x].ff+y,F[x].ss+1);
}
void upd(int x){
//~ cout<<"upd " <<x<<endl;
cnt[x]++;
s.insert(mp(x,cnt[x]));
update(upper_bound(all(comp),x)-comp.begin(),x);
}
pair<ll,int>tap(int x){
pair<ll,int>res=mp(0,0);
for(;x;x-=x&(-x))
res=mp(res.ff+F[x].ff,res.ss+F[x].ss);
return res;
}
ll solve(int x){
//~ printf("median %d\n",x);
pair<ll,int> res=tap(upper_bound(all(comp),x)-comp.begin());
return (res.ss*1LL*x-res.ff)+((sum.ff-res.ff)-(sum.ss-res.ss)*1LL*x);
}
int main(){
//~ freopen("file.in", "r", stdin);
int k,n;
scanf("%d%d",&k,&n);
ll ans=0;
for(int i=1;i<=n;i++){
char a,c;
int b,d;
scanf(" %c%d %c%d",&a,&b,&c,&d);
if(a!=c){
v.pb(mp(b,d));
comp.pb(b);comp.pb(d);
}
else
ans+=abs(b-d);
}
sort(all(comp));
comp.erase(unique(all(comp)),comp.end());
//~ tr(it,comp)
//~ cout<<*it<<" ";
//~ cout<<endl;
sort(all(v),cmp);
for(int i=0;i<int(v.size());i++){
upd(v[i].ff);upd(v[i].ss);
pre[i]=solve((s.find_by_order(s.size()/2-1)->ff+s.find_by_order(s.size()/2)->ff)/2);
//~ tr(it,s)
//~ cout<<it->ff<<" ";
//~ cout<<endl;
//~ cout<<pre[i]<<endl;
//~ wr
}
s.clear();cnt.clear();
sum=mp(0,0);
memset(F,0,sizeof F);
for(int i=int(v.size())-1;i>=0;i--){
upd(v[i].ff);upd(v[i].ss);
suf[i]=solve((s.find_by_order(s.size()/2-1)->ff+s.find_by_order(s.size()/2)->ff)/2);
}
ll res=0;
if(v.size())
res=pre[v.size()-1];
if(k==1)
printf("%lld\n",ans+res+v.size());
else{
for(int i=0;i<int(v.size())-1;i++)
umin(res,pre[i]+suf[i+1]);
printf("%lld\n",ans+res+v.size());
}
return 0;
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:59:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&k,&n);
~~~~~^~~~~~~~~~~~~~
bridge.cpp:64:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf(" %c%d %c%d",&a,&b,&c,&d);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
3448 KB |
Output is correct |
2 |
Correct |
4 ms |
3448 KB |
Output is correct |
3 |
Correct |
6 ms |
3756 KB |
Output is correct |
4 |
Correct |
7 ms |
3808 KB |
Output is correct |
5 |
Correct |
9 ms |
3812 KB |
Output is correct |
6 |
Correct |
6 ms |
3812 KB |
Output is correct |
7 |
Correct |
8 ms |
3888 KB |
Output is correct |
8 |
Correct |
10 ms |
3888 KB |
Output is correct |
9 |
Correct |
7 ms |
3888 KB |
Output is correct |
10 |
Correct |
7 ms |
3888 KB |
Output is correct |
11 |
Correct |
7 ms |
3892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
3892 KB |
Output is correct |
2 |
Correct |
4 ms |
3892 KB |
Output is correct |
3 |
Correct |
6 ms |
3892 KB |
Output is correct |
4 |
Correct |
8 ms |
3908 KB |
Output is correct |
5 |
Correct |
9 ms |
3908 KB |
Output is correct |
6 |
Correct |
6 ms |
3908 KB |
Output is correct |
7 |
Correct |
7 ms |
3912 KB |
Output is correct |
8 |
Correct |
7 ms |
3912 KB |
Output is correct |
9 |
Correct |
7 ms |
3912 KB |
Output is correct |
10 |
Correct |
6 ms |
3912 KB |
Output is correct |
11 |
Correct |
7 ms |
3912 KB |
Output is correct |
12 |
Correct |
374 ms |
19536 KB |
Output is correct |
13 |
Correct |
1128 ms |
29084 KB |
Output is correct |
14 |
Correct |
670 ms |
29084 KB |
Output is correct |
15 |
Correct |
570 ms |
29084 KB |
Output is correct |
16 |
Correct |
402 ms |
29084 KB |
Output is correct |
17 |
Correct |
565 ms |
29084 KB |
Output is correct |
18 |
Correct |
476 ms |
29084 KB |
Output is correct |
19 |
Correct |
507 ms |
29084 KB |
Output is correct |
20 |
Correct |
396 ms |
29084 KB |
Output is correct |
21 |
Correct |
587 ms |
29084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
29084 KB |
Output is correct |
2 |
Correct |
4 ms |
29084 KB |
Output is correct |
3 |
Correct |
4 ms |
29084 KB |
Output is correct |
4 |
Correct |
4 ms |
29084 KB |
Output is correct |
5 |
Correct |
4 ms |
29084 KB |
Output is correct |
6 |
Correct |
4 ms |
29084 KB |
Output is correct |
7 |
Correct |
4 ms |
29084 KB |
Output is correct |
8 |
Correct |
4 ms |
29084 KB |
Output is correct |
9 |
Correct |
4 ms |
29084 KB |
Output is correct |
10 |
Correct |
4 ms |
29084 KB |
Output is correct |
11 |
Correct |
4 ms |
29084 KB |
Output is correct |
12 |
Correct |
4 ms |
29084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
29084 KB |
Output is correct |
2 |
Correct |
4 ms |
29084 KB |
Output is correct |
3 |
Correct |
5 ms |
29084 KB |
Output is correct |
4 |
Correct |
6 ms |
29084 KB |
Output is correct |
5 |
Correct |
4 ms |
29084 KB |
Output is correct |
6 |
Correct |
4 ms |
29084 KB |
Output is correct |
7 |
Correct |
5 ms |
29084 KB |
Output is correct |
8 |
Correct |
4 ms |
29084 KB |
Output is correct |
9 |
Correct |
5 ms |
29084 KB |
Output is correct |
10 |
Correct |
4 ms |
29084 KB |
Output is correct |
11 |
Correct |
5 ms |
29084 KB |
Output is correct |
12 |
Correct |
5 ms |
29084 KB |
Output is correct |
13 |
Correct |
6 ms |
29084 KB |
Output is correct |
14 |
Correct |
6 ms |
29084 KB |
Output is correct |
15 |
Correct |
8 ms |
29084 KB |
Output is correct |
16 |
Correct |
5 ms |
29084 KB |
Output is correct |
17 |
Correct |
5 ms |
29084 KB |
Output is correct |
18 |
Correct |
5 ms |
29084 KB |
Output is correct |
19 |
Correct |
7 ms |
29084 KB |
Output is correct |
20 |
Correct |
9 ms |
29084 KB |
Output is correct |
21 |
Correct |
9 ms |
29084 KB |
Output is correct |
22 |
Correct |
9 ms |
29084 KB |
Output is correct |
23 |
Correct |
8 ms |
29084 KB |
Output is correct |
24 |
Correct |
7 ms |
29084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
29084 KB |
Output is correct |
2 |
Correct |
4 ms |
29084 KB |
Output is correct |
3 |
Correct |
5 ms |
29084 KB |
Output is correct |
4 |
Correct |
4 ms |
29084 KB |
Output is correct |
5 |
Correct |
4 ms |
29084 KB |
Output is correct |
6 |
Correct |
4 ms |
29084 KB |
Output is correct |
7 |
Correct |
4 ms |
29084 KB |
Output is correct |
8 |
Correct |
5 ms |
29084 KB |
Output is correct |
9 |
Correct |
5 ms |
29084 KB |
Output is correct |
10 |
Correct |
5 ms |
29084 KB |
Output is correct |
11 |
Correct |
4 ms |
29084 KB |
Output is correct |
12 |
Correct |
4 ms |
29084 KB |
Output is correct |
13 |
Correct |
6 ms |
29084 KB |
Output is correct |
14 |
Correct |
7 ms |
29084 KB |
Output is correct |
15 |
Correct |
8 ms |
29084 KB |
Output is correct |
16 |
Correct |
5 ms |
29084 KB |
Output is correct |
17 |
Correct |
6 ms |
29084 KB |
Output is correct |
18 |
Correct |
5 ms |
29084 KB |
Output is correct |
19 |
Correct |
6 ms |
29084 KB |
Output is correct |
20 |
Correct |
7 ms |
29084 KB |
Output is correct |
21 |
Correct |
7 ms |
29084 KB |
Output is correct |
22 |
Correct |
7 ms |
29084 KB |
Output is correct |
23 |
Correct |
6 ms |
29084 KB |
Output is correct |
24 |
Correct |
7 ms |
29084 KB |
Output is correct |
25 |
Correct |
365 ms |
29084 KB |
Output is correct |
26 |
Correct |
521 ms |
29084 KB |
Output is correct |
27 |
Correct |
1168 ms |
30740 KB |
Output is correct |
28 |
Correct |
1227 ms |
33836 KB |
Output is correct |
29 |
Correct |
1227 ms |
36404 KB |
Output is correct |
30 |
Correct |
531 ms |
36404 KB |
Output is correct |
31 |
Correct |
417 ms |
36404 KB |
Output is correct |
32 |
Correct |
524 ms |
41528 KB |
Output is correct |
33 |
Correct |
517 ms |
43376 KB |
Output is correct |
34 |
Correct |
488 ms |
45316 KB |
Output is correct |
35 |
Correct |
451 ms |
45316 KB |
Output is correct |
36 |
Correct |
585 ms |
49972 KB |
Output is correct |
37 |
Correct |
436 ms |
49972 KB |
Output is correct |