/*
Solution for APIO 2015 - Palembang Bridges
Tags :
*/
#include <bits/stdc++.h>
using namespace std;
#pragma GCC target ("avx2")
#pragma GCC optimization ("Ofast")
#pragma GCC optimization ("unroll-loops")
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef long double ld;
typedef pair<ll, ll> pll;
#define FOR(i, a, b) for(int i = a; i < b; i++)
#define ROF(i, a, b) for(int i = a; i >= b; i--)
#define ms memset
#define pb push_back
#define fi first
#define se second
ll MOD = 1000000007;
ll power(ll base, ll n){
if (n == 0) return 1;
if (n == 1) return base;
ll halfn = power(base, n/2);
if (n % 2 == 0) return (halfn * halfn) % MOD;
return (((halfn * halfn) % MOD) * base) % MOD;
}
ll inverse(ll n){
return power(n, MOD-2);
}
ll add(ll a, ll b){
return (a+b) % MOD;
}
ll mul(ll a, ll b){
a %= MOD;
return (a*b) % MOD;
}
ll gcd(ll a, ll b){
if (a == 1) return 1;
if (a == 0) return b;
return gcd(b%a, a);
}
ll f(vector<ll> l, vector<ll> r){
int n = l.size();
ll mini = MOD*MOD;
vector<ll> pre_r(n), suf_l(n);
pre_r[0] = r[0];
FOR(i,1,n){
pre_r[i] = pre_r[i-1] + r[i];
}
suf_l[n-1] = l[n-1];
ROF(i,n-2,0){
suf_l[i] = suf_l[i+1] + l[i];
}
FOR(i,0,n){
ll ans_r = r[i] * (ll) (i+1) - pre_r[i];
if (r[i] >= l[n-1]){
mini = min(mini, ans_r);
continue;
}
ll pos_r = upper_bound(l.begin(), l.end(), r[i]) - l.begin();
ans_r += suf_l[pos_r] - r[i] * (ll) (n-pos_r);
mini = min(mini, ans_r);
}
FOR(i,0,n){
ll ans_l = suf_l[i] - l[i] * (ll) (n-i);
if (l[i] <= r[0]){
mini = min(mini, ans_l);
continue;
}
ll pos_l = upper_bound(r.begin(), r.end(), l[i]) - r.begin();
ans_l += l[i] * (ll) (pos_l) - pre_r[pos_l-1];
mini = min(mini, ans_l);
}
return mini;
}
void solve(){
int k, n; cin >> k >> n;
ll ans = 0;
vector<pair<ll, ll>> loc;
FOR(i,0,n){
string z1, z2;
ll l, r;
cin >> z1 >> l >> z2 >> r;
if (l > r) swap(l,r);
if (z1 == z2){
ans += (r-l);
}else{
ans += (r-l+1);
loc.pb({l, r});
}
}
n = loc.size();
vector<ll> l(n), r(n);
FOR(i,0,n){
l[i] = loc[i].fi;
r[i] = loc[i].se;
}
if (n==0){
cout<<ans;
return;
}
if (k==1){
sort(l.begin(), l.end());
sort(r.begin(), r.end());
ans+=f(l,r)*2;
cout<<ans;
return;
}
// brute force O(n^3)
ll mini = MOD*MOD;
vector<ll> comb;
FOR(i,0,n){
comb.pb(l[i]);
comb.pb(r[i]);
}
sort(comb.begin(), comb.end());
FOR(i,0,2*n){
FOR(j,i+1,2*n){
ll val = 0;
FOR(k,0,n){
if (r[k] <= comb[i]){
val += comb[i] - r[k]; continue;
}
if (l[k] >= comb[j]){
val += l[k] - comb[j]; continue;
}
if (l[k] >= comb[i] && r[k] <= comb[j]){
val += min(l[k]-comb[i], comb[j] - r[k]);
}
}
mini = min(mini, val);
}
}
ans += mini*2;
cout << ans;
}
int main() {
ios::sync_with_stdio(false);
int TC = 1;
//cin >> TC;
FOR(i, 1, TC+1){
//cout << "Case #" << i << ": ";
solve();
}
return 0;
}
Compilation message
bridge.cpp:10: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
10 | #pragma GCC optimization ("Ofast")
|
bridge.cpp:11: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
11 | #pragma GCC optimization ("unroll-loops")
|
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
360 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
360 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
316 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
32 ms |
7304 KB |
Output is correct |
13 |
Correct |
58 ms |
8840 KB |
Output is correct |
14 |
Correct |
44 ms |
7036 KB |
Output is correct |
15 |
Correct |
33 ms |
5252 KB |
Output is correct |
16 |
Correct |
40 ms |
8260 KB |
Output is correct |
17 |
Correct |
54 ms |
8872 KB |
Output is correct |
18 |
Correct |
41 ms |
8460 KB |
Output is correct |
19 |
Correct |
65 ms |
8812 KB |
Output is correct |
20 |
Correct |
43 ms |
8340 KB |
Output is correct |
21 |
Correct |
57 ms |
8652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
212 KB |
Output is correct |
4 |
Correct |
4 ms |
212 KB |
Output is correct |
5 |
Correct |
2 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
4 ms |
320 KB |
Output is correct |
8 |
Correct |
4 ms |
320 KB |
Output is correct |
9 |
Correct |
4 ms |
328 KB |
Output is correct |
10 |
Correct |
5 ms |
320 KB |
Output is correct |
11 |
Correct |
4 ms |
316 KB |
Output is correct |
12 |
Correct |
4 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
212 KB |
Output is correct |
4 |
Correct |
4 ms |
324 KB |
Output is correct |
5 |
Correct |
2 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
4 ms |
212 KB |
Output is correct |
8 |
Correct |
4 ms |
212 KB |
Output is correct |
9 |
Correct |
4 ms |
212 KB |
Output is correct |
10 |
Correct |
6 ms |
316 KB |
Output is correct |
11 |
Correct |
4 ms |
212 KB |
Output is correct |
12 |
Correct |
4 ms |
212 KB |
Output is correct |
13 |
Execution timed out |
2065 ms |
340 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
3 ms |
212 KB |
Output is correct |
4 |
Correct |
5 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
3 ms |
324 KB |
Output is correct |
8 |
Correct |
4 ms |
320 KB |
Output is correct |
9 |
Correct |
4 ms |
324 KB |
Output is correct |
10 |
Correct |
5 ms |
212 KB |
Output is correct |
11 |
Correct |
4 ms |
212 KB |
Output is correct |
12 |
Correct |
5 ms |
212 KB |
Output is correct |
13 |
Execution timed out |
2087 ms |
324 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |