Submission #259372

# Submission time Handle Problem Language Result Execution time Memory
259372 2020-08-07T16:57:52 Z shayan_p Palembang Bridges (APIO15_bridge) C++14
100 / 100
977 ms 27764 KB
// And you curse yourself for things you never done
 
#include<bits/stdc++.h>
 
#define F first
#define S second
#define PB push_back
#define sz(s) int((s).size())
#define bit(n,k) (((n)>>(k))&1)
 
using namespace std;
 
typedef long long ll;
typedef pair<int,int> pii;
 
const int maxn = 1e5 + 10, inf = 1e9 + 10;
const ll INF = 1e18;
 
ll sv[maxn];

int pt;
set<int> All;
map<int, int> L, R;
int CL, CR;
ll ANS;
 
void restart(){
    pt = -inf;
    All.clear();
    ANS = 0;
    L.clear(), R.clear();
    CL = CR = 0;	
}
ll add(int f, int s){
    All.insert(f), All.insert(s);
    L[f]++, R[s]++;
    if(pt < f)
	ANS+= f - pt;
    if(s < pt)
	ANS+= pt - s;
    if(pt < f)
	CL++;
    if(s <= pt)
	CR++;
    while(true){
	auto it = All.upper_bound(pt);
	if(it == All.end())
	    break;
	int nw = *it;
	ll df = 1ll * (CR - CL) * (nw - pt);
	ll num = df + ANS;
	if(num < ANS){
	    ANS = num, pt = nw;
	    CR+= R[pt];
	    CL-= L[pt];
	}
	else{
	    break;
	}
    }
    return ANS * 2;
}
 
int main(){
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie();
 
    int k, n;
    cin >> k >> n;
    ll sig = 0;
    vector<pii> inp;
    for(int i = 0; i < n; i++){
	char a, b;
	int l, r;
	cin >> a >> l >> b >> r;
	if(r < l)
	    swap(l, r);
	sig+= r-l;
	if(a != b){
	    sig++;
	    inp.PB({l, r});
	}
    }
    sort(inp.begin(), inp.end(), [](pii a, pii b){ return a.F + a.S < b.F + b.S; } );
    n = sz(inp);
 
    if(n == 0)
	return cout << sig << endl, 0;
    
    restart();
    for(int i = 0; i < n; i++){
	sv[i + 1] = add(inp[i].F, inp[i].S);
    }
    restart();    
    ll ans = INF;
    for(int i = n-1; i >= 0; i--){
	ans = min(ans, sig + sv[i] + add(-inp[i].S, -inp[i].F));
    }
    if(k == 1)
	ans = sig + sv[n];
    return cout << ans << endl, 0;
    
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 3 ms 640 KB Output is correct
5 Correct 3 ms 640 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 3 ms 640 KB Output is correct
8 Correct 2 ms 512 KB Output is correct
9 Correct 2 ms 640 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 2 ms 640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 3 ms 640 KB Output is correct
5 Correct 3 ms 640 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 2 ms 640 KB Output is correct
8 Correct 2 ms 512 KB Output is correct
9 Correct 2 ms 768 KB Output is correct
10 Correct 1 ms 428 KB Output is correct
11 Correct 3 ms 640 KB Output is correct
12 Correct 29 ms 2924 KB Output is correct
13 Correct 826 ms 25832 KB Output is correct
14 Correct 158 ms 4464 KB Output is correct
15 Correct 341 ms 15344 KB Output is correct
16 Correct 38 ms 3696 KB Output is correct
17 Correct 381 ms 27760 KB Output is correct
18 Correct 276 ms 22768 KB Output is correct
19 Correct 363 ms 26992 KB Output is correct
20 Correct 40 ms 4088 KB Output is correct
21 Correct 388 ms 27556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 3 ms 640 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 2 ms 640 KB Output is correct
21 Correct 2 ms 512 KB Output is correct
22 Correct 2 ms 640 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 2 ms 640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 3 ms 640 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 2 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 2 ms 640 KB Output is correct
21 Correct 2 ms 512 KB Output is correct
22 Correct 2 ms 640 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 2 ms 640 KB Output is correct
25 Correct 29 ms 2796 KB Output is correct
26 Correct 49 ms 3052 KB Output is correct
27 Correct 816 ms 23408 KB Output is correct
28 Correct 977 ms 25964 KB Output is correct
29 Correct 827 ms 25956 KB Output is correct
30 Correct 359 ms 13808 KB Output is correct
31 Correct 39 ms 3696 KB Output is correct
32 Correct 333 ms 27764 KB Output is correct
33 Correct 246 ms 22768 KB Output is correct
34 Correct 336 ms 26992 KB Output is correct
35 Correct 40 ms 3952 KB Output is correct
36 Correct 341 ms 27504 KB Output is correct
37 Correct 26 ms 2800 KB Output is correct