#include <bits/stdc++.h>
using namespace std;
#define FOR(i, j, k) for(int i=(j); i<=(k); i++)
#define FFOR(i, j, k) for(int i=(j); i<(k); i++)
#define DFOR(i, j, k) for(int i=(j); i>=(k); i--)
#define bug(x) cerr<<#x<<" = "<<(x)<<'\n'
#define pb push_back
#define mp make_pair
#define setbit(s, i) (s|=(1LL<<(i)))
#define bit(s, i) (((s)>>(i))&1LL)
#define mask(i) ((1LL<<(i)))
#define builtin_popcount __builtin_popcountll
typedef long long ll;
typedef long double ld;
template <typename T> inline void read(T &x){
char c;
bool nega=0;
while((!isdigit(c=getchar()))&&(c!='-'));
if(c=='-'){
nega=1;
c=getchar();
}
x=c-48;
while(isdigit(c=getchar())) x=x*10+c-48;
if(nega) x=-x;
}
template <typename T> inline void writep(T x){
if(x>9) writep(x/10);
putchar(x%10+48);
}
template <typename T> inline void write(T x){
if(x<0){
putchar('-');
x=-x;
}
writep(x);
}
template <typename T> inline void writeln(T x){
write(x);
putchar('\n');
}
#define taskname "Palembang_Bridges"
int k, n;
ll sure=0;
deque <pair <ll, ll> > cross;
namespace one{
vector <ll> pos;
void solve(){
for(pair <ll, ll> p: cross){
pos.pb(p.first);
pos.pb(p.second);
}
sort(pos.begin(), pos.end());
ll x=pos[pos.size()/2];
ll res=0;
for(ll p: pos) res+=abs(p-x);
write(res+sure);
}
}
namespace two{
vector <ll> frwd;
vector <ll> bkwd;
class dynamic_nearest{
public:
multiset <ll> low, high;
ll sumlow, sumhigh;
void shift(){
while(low.size()>high.size()){
high.insert(*low.rbegin());
sumhigh+=(*low.rbegin());
sumlow-=(*low.rbegin());
low.erase(low.find(*low.rbegin()));
}
while((!low.empty())&&((*low.rbegin())>(*high.begin()))){
ll a=*low.rbegin();
ll b=*high.begin();
sumlow-=a;
sumhigh-=b;
low.erase(low.find(*low.rbegin()));
high.erase(high.begin());
low.insert(b);
sumlow+=b;
high.insert(a);
sumhigh+=a;
}
}
void insert(ll x){
low.insert(x);
sumlow+=x;
shift();
}
void clear(){
low.clear();
high.clear();
sumlow=sumhigh=0;
}
ll cost(){
if(high.empty()) return 0;
return (sumhigh-sumlow)-((*high.begin())*(high.size()-low.size()));
}
} DN;
void solve(){
if(cross.empty()){
write(sure);
return;
}
sort(cross.begin(), cross.end(), [](pair <ll, ll> a, pair <ll, ll> b){
return a.first+a.second<b.first+b.second;
});
DN.clear();
for(pair <ll, ll> p: cross){
DN.insert(p.first);
DN.insert(p.second);
frwd.pb(DN.cost());
}
reverse(cross.begin(), cross.end());
DN.clear();
for(pair <ll, ll> p: cross){
DN.insert(p.first);
DN.insert(p.second);
bkwd.pb(DN.cost());
}
reverse(bkwd.begin(), bkwd.end());
ll res=bkwd[0];
bkwd.pb(0);
FFOR(i, 0, cross.size()) res=min(res, frwd[i]+bkwd[i+1]);
writeln(res+sure);
}
}
int main(){
#ifdef Kanikou
if(fopen(taskname".inp", "r"))
freopen(taskname".inp", "r", stdin);
#endif // Kanikou
read(k);
read(n);
sure=n;
{
char c0, c1;
ll x0, x1;
FOR(i, 1, n){
while(!isalpha(c0=getchar()));
read(x0);
while(!isalpha(c1=getchar()));
read(x1);
if(c0==c1) sure+=abs(x0-x1)-1;
else{
if(x0>x1) swap(x0, x1);
cross.pb(mp(x0, x1));
}
}
}
if(k==1) one::solve();
else two::solve();
}
Compilation message
bridge.cpp: In function 'void two::solve()':
bridge.cpp:4:39: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
#define FFOR(i, j, k) for(int i=(j); i<(k); i++)
^
bridge.cpp:126:9: note: in expansion of macro 'FFOR'
FFOR(i, 0, cross.size()) res=min(res, frwd[i]+bkwd[i+1]);
^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
476 KB |
Output is correct |
3 |
Correct |
2 ms |
476 KB |
Output is correct |
4 |
Correct |
2 ms |
476 KB |
Output is correct |
5 |
Correct |
2 ms |
516 KB |
Output is correct |
6 |
Correct |
2 ms |
516 KB |
Output is correct |
7 |
Correct |
2 ms |
576 KB |
Output is correct |
8 |
Correct |
2 ms |
704 KB |
Output is correct |
9 |
Correct |
2 ms |
704 KB |
Output is correct |
10 |
Correct |
2 ms |
704 KB |
Output is correct |
11 |
Correct |
2 ms |
704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
704 KB |
Output is correct |
2 |
Correct |
2 ms |
704 KB |
Output is correct |
3 |
Correct |
2 ms |
704 KB |
Output is correct |
4 |
Correct |
2 ms |
704 KB |
Output is correct |
5 |
Correct |
2 ms |
704 KB |
Output is correct |
6 |
Correct |
2 ms |
704 KB |
Output is correct |
7 |
Correct |
2 ms |
704 KB |
Output is correct |
8 |
Correct |
2 ms |
704 KB |
Output is correct |
9 |
Correct |
2 ms |
704 KB |
Output is correct |
10 |
Correct |
2 ms |
704 KB |
Output is correct |
11 |
Correct |
2 ms |
704 KB |
Output is correct |
12 |
Correct |
22 ms |
4336 KB |
Output is correct |
13 |
Correct |
49 ms |
4336 KB |
Output is correct |
14 |
Correct |
33 ms |
4336 KB |
Output is correct |
15 |
Correct |
28 ms |
4336 KB |
Output is correct |
16 |
Correct |
26 ms |
4340 KB |
Output is correct |
17 |
Correct |
32 ms |
4340 KB |
Output is correct |
18 |
Correct |
36 ms |
4344 KB |
Output is correct |
19 |
Correct |
46 ms |
4412 KB |
Output is correct |
20 |
Correct |
29 ms |
4412 KB |
Output is correct |
21 |
Correct |
41 ms |
4412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4412 KB |
Output is correct |
2 |
Correct |
2 ms |
4412 KB |
Output is correct |
3 |
Correct |
2 ms |
4412 KB |
Output is correct |
4 |
Correct |
2 ms |
4412 KB |
Output is correct |
5 |
Correct |
2 ms |
4412 KB |
Output is correct |
6 |
Correct |
2 ms |
4412 KB |
Output is correct |
7 |
Correct |
2 ms |
4412 KB |
Output is correct |
8 |
Correct |
2 ms |
4412 KB |
Output is correct |
9 |
Correct |
2 ms |
4412 KB |
Output is correct |
10 |
Correct |
2 ms |
4412 KB |
Output is correct |
11 |
Correct |
2 ms |
4412 KB |
Output is correct |
12 |
Correct |
2 ms |
4412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4412 KB |
Output is correct |
2 |
Correct |
2 ms |
4412 KB |
Output is correct |
3 |
Correct |
2 ms |
4412 KB |
Output is correct |
4 |
Correct |
2 ms |
4412 KB |
Output is correct |
5 |
Correct |
2 ms |
4412 KB |
Output is correct |
6 |
Correct |
2 ms |
4412 KB |
Output is correct |
7 |
Correct |
2 ms |
4412 KB |
Output is correct |
8 |
Correct |
2 ms |
4412 KB |
Output is correct |
9 |
Correct |
2 ms |
4412 KB |
Output is correct |
10 |
Correct |
2 ms |
4412 KB |
Output is correct |
11 |
Correct |
2 ms |
4412 KB |
Output is correct |
12 |
Correct |
2 ms |
4412 KB |
Output is correct |
13 |
Correct |
3 ms |
4412 KB |
Output is correct |
14 |
Correct |
4 ms |
4412 KB |
Output is correct |
15 |
Correct |
4 ms |
4412 KB |
Output is correct |
16 |
Correct |
3 ms |
4412 KB |
Output is correct |
17 |
Correct |
4 ms |
4412 KB |
Output is correct |
18 |
Correct |
3 ms |
4412 KB |
Output is correct |
19 |
Correct |
4 ms |
4412 KB |
Output is correct |
20 |
Correct |
3 ms |
4412 KB |
Output is correct |
21 |
Correct |
5 ms |
4412 KB |
Output is correct |
22 |
Correct |
4 ms |
4412 KB |
Output is correct |
23 |
Correct |
4 ms |
4412 KB |
Output is correct |
24 |
Correct |
4 ms |
4412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4412 KB |
Output is correct |
2 |
Correct |
2 ms |
4412 KB |
Output is correct |
3 |
Correct |
2 ms |
4412 KB |
Output is correct |
4 |
Correct |
3 ms |
4412 KB |
Output is correct |
5 |
Correct |
2 ms |
4412 KB |
Output is correct |
6 |
Correct |
2 ms |
4412 KB |
Output is correct |
7 |
Correct |
2 ms |
4412 KB |
Output is correct |
8 |
Correct |
3 ms |
4412 KB |
Output is correct |
9 |
Correct |
2 ms |
4412 KB |
Output is correct |
10 |
Correct |
2 ms |
4412 KB |
Output is correct |
11 |
Correct |
2 ms |
4412 KB |
Output is correct |
12 |
Correct |
2 ms |
4412 KB |
Output is correct |
13 |
Correct |
4 ms |
4412 KB |
Output is correct |
14 |
Correct |
5 ms |
4412 KB |
Output is correct |
15 |
Correct |
5 ms |
4412 KB |
Output is correct |
16 |
Correct |
2 ms |
4412 KB |
Output is correct |
17 |
Correct |
4 ms |
4412 KB |
Output is correct |
18 |
Correct |
2 ms |
4412 KB |
Output is correct |
19 |
Correct |
4 ms |
4412 KB |
Output is correct |
20 |
Correct |
4 ms |
4412 KB |
Output is correct |
21 |
Correct |
4 ms |
4412 KB |
Output is correct |
22 |
Correct |
4 ms |
4412 KB |
Output is correct |
23 |
Correct |
4 ms |
4412 KB |
Output is correct |
24 |
Correct |
3 ms |
4412 KB |
Output is correct |
25 |
Correct |
311 ms |
14808 KB |
Output is correct |
26 |
Correct |
378 ms |
15740 KB |
Output is correct |
27 |
Correct |
506 ms |
17480 KB |
Output is correct |
28 |
Correct |
563 ms |
19772 KB |
Output is correct |
29 |
Correct |
533 ms |
22080 KB |
Output is correct |
30 |
Correct |
223 ms |
22080 KB |
Output is correct |
31 |
Correct |
302 ms |
25056 KB |
Output is correct |
32 |
Correct |
280 ms |
27332 KB |
Output is correct |
33 |
Correct |
184 ms |
29188 KB |
Output is correct |
34 |
Correct |
263 ms |
31532 KB |
Output is correct |
35 |
Correct |
237 ms |
33508 KB |
Output is correct |
36 |
Correct |
285 ms |
35604 KB |
Output is correct |
37 |
Correct |
191 ms |
36384 KB |
Output is correct |