답안 #660000

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
660000 2022-11-20T08:29:36 Z highway_to_hell Palembang Bridges (APIO15_bridge) C++14
100 / 100
92 ms 20120 KB
#include <bits/stdc++.h>
#include <math.h>
//in the name of god,aka allah
#pragma GCC optimize("unroll-loops")
using namespace std;
#define pi pair<long long , long long>
#define pii pair<long long , pair<long long , long long>>
const int maxm = 2e5 + 5;
const long long mod = 1000000001;
const int sq = 740;
typedef long long ll;
ll l,r,mid;
ll n,m;
ll dis[maxm] , sum[maxm];
bool isval(int mid){
    //cout << mid <<" " << mid*mid-mid <<endl;
    if (((mid-1)*mid)/2 < m) return 0;
    return 1;
}
ll darage[maxm] , ss , mm;
queue<int> q;
vector<int> g[maxm] , z[maxm];
ll sath[maxm];
bool vis[maxm] , gos[maxm];
ll pedaret[maxm];
ll get_par(ll v){
    if (pedaret[v]==v) return v;
    return pedaret[v] = get_par(pedaret[v]);
}
void merge(ll r , ll q){
    if (get_par(r)!=get_par(q))l+=max(darage[r],darage[q])*1ll*sath[r]*1ll*sath[q];
    r = get_par(r) , q = get_par(q);
    if (r!=q){
        if (sath[r]<sath[q]) swap(r,q);
        pedaret[q] = r;
        sath[r] += sath[q];
    }
    return ;
}
ll pars1[maxm] , pars2[maxm];
vector<ll> se[maxm];
set<ll> st;
ll rp[maxm];
ll dp[maxm];
pi W[maxm];
int rw[400][400];
int dage[305];
vector<int> vec;
map<ll,ll> mp;
void dfs(int u , int rang , int jad){
	darage[u] = rang;
	pedaret[rang]++;
	for (auto x : g[u]){
		if (x!=jad) dfs(x,1-rang,u);
	}
	return ;
}
priority_queue<int> lowq;
priority_queue<int, vector<int>, greater<int> > upq;
int h,w;
void prep(){
	memset(dage,0,sizeof dage);
}
void solve(int x){
	ll ans = 0;
	for (int i=1; i<=n; i++){
		for (int j=1; j<=m; j++){
			if(!dage[rw[i][j]]) ans++;
			dage[rw[i][j]]++;
		}
	}
	for (int i=1; i<=w; i++){
		for (int j=x; j<=x+h-1; j++){
			if (dage[rw[j][i]]==1) ans--;
			dage[rw[j][i]]--;
		}
	}
	//cout<<ans<<endl;
	vec.push_back(ans);
	for (int i=2; i<=m-w+1; i++){
		if (i!=1){
			for (int j=x; j<=x+h-1; j++){
				if (dage[rw[j][i-1]]==0) ans++;
				dage[rw[j][i-1]]++;
			}
		}
		for (int j=x; j<=x+h-1; j++){
			if (dage[rw[j][i+w-1]]==1) ans--;
			dage[rw[j][i+w-1]]--;
		}
		vec.push_back(ans);
	}
}
bool cmp(pi x, pi y){ return x.first+x.second<y.first+y.second; }
void appen(int x){
    mm = (lowq.size()?lowq.top():mod);
    if (x<=mm) lowq.push(x) , r+=x;
	else upq.push(x) , l+=x;	
    if (upq.size() + 1 < lowq.size()) {
		auto y = lowq.top();
		lowq.pop();
		upq.push(y);
		r-=y;
		l+=y;
	} 
	else if (lowq.size()<upq.size()) {
		auto y=upq.top();
		upq.pop();
		lowq.push(y);
		l-=y;
		r+=y;
	}
}
int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin >>m>>n;
	for (int i=1; i<=n; i++){
		char x,y;
		cin >>x>>l>>y>>r;
		if (y==x) mid+=abs(r-l);
		else{
			ss++;
			mid++;
			W[ss] = {l,r};
		}
	}
	if (m==1){
	    for(int i=1; i<=ss; i++){
	        darage[i]=W[i].first;
	        darage[i+ss]=W[i].second;
	    }
	    sort(darage+1,darage+1+2*ss);
	    mm = darage[ss];
	    for(int i=1; i<=2*ss; i++) mid+=abs(darage[i]-mm);
	    cout<<mid<<endl;
	}
	else{
		sort(W+1ll,W+1+ss,cmp);
		l = 0 , r = 0;
		for (int i=1; i<=ss; i++){
	        appen(W[i].first) , appen(W[i].second);
	        pedaret[i]=l-r;
	    }
	    while(!lowq.empty()) lowq.pop();
	    while (!upq.empty()) upq.pop();
	    l = 0 , r = 0; 
	    ll ans = pedaret[ss];
	    for(int i=ss; i>0; i--){
	        appen(W[i].first) , appen(W[i].second);
	        ans = min(ans,l-r+pedaret[i-1]);
	    }
	    cout<<mid+ans<<endl;
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14420 KB Output is correct
2 Correct 8 ms 14420 KB Output is correct
3 Correct 8 ms 14396 KB Output is correct
4 Correct 8 ms 14448 KB Output is correct
5 Correct 8 ms 14420 KB Output is correct
6 Correct 8 ms 14448 KB Output is correct
7 Correct 8 ms 14420 KB Output is correct
8 Correct 8 ms 14420 KB Output is correct
9 Correct 8 ms 14548 KB Output is correct
10 Correct 8 ms 14420 KB Output is correct
11 Correct 8 ms 14420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 14484 KB Output is correct
2 Correct 7 ms 14420 KB Output is correct
3 Correct 7 ms 14400 KB Output is correct
4 Correct 8 ms 14448 KB Output is correct
5 Correct 8 ms 14424 KB Output is correct
6 Correct 8 ms 14392 KB Output is correct
7 Correct 9 ms 14484 KB Output is correct
8 Correct 7 ms 14420 KB Output is correct
9 Correct 8 ms 14448 KB Output is correct
10 Correct 7 ms 14420 KB Output is correct
11 Correct 8 ms 14468 KB Output is correct
12 Correct 25 ms 18276 KB Output is correct
13 Correct 47 ms 19888 KB Output is correct
14 Correct 34 ms 18388 KB Output is correct
15 Correct 31 ms 17612 KB Output is correct
16 Correct 34 ms 19196 KB Output is correct
17 Correct 47 ms 19788 KB Output is correct
18 Correct 38 ms 19400 KB Output is correct
19 Correct 47 ms 19808 KB Output is correct
20 Correct 33 ms 19400 KB Output is correct
21 Correct 42 ms 19612 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14420 KB Output is correct
2 Correct 8 ms 14420 KB Output is correct
3 Correct 8 ms 14400 KB Output is correct
4 Correct 8 ms 14436 KB Output is correct
5 Correct 8 ms 14440 KB Output is correct
6 Correct 8 ms 14440 KB Output is correct
7 Correct 8 ms 14420 KB Output is correct
8 Correct 8 ms 14420 KB Output is correct
9 Correct 8 ms 14420 KB Output is correct
10 Correct 7 ms 14420 KB Output is correct
11 Correct 9 ms 14460 KB Output is correct
12 Correct 8 ms 14428 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14436 KB Output is correct
2 Correct 7 ms 14420 KB Output is correct
3 Correct 8 ms 14444 KB Output is correct
4 Correct 8 ms 14420 KB Output is correct
5 Correct 7 ms 14436 KB Output is correct
6 Correct 8 ms 14344 KB Output is correct
7 Correct 9 ms 14420 KB Output is correct
8 Correct 8 ms 14440 KB Output is correct
9 Correct 8 ms 14420 KB Output is correct
10 Correct 7 ms 14368 KB Output is correct
11 Correct 8 ms 14420 KB Output is correct
12 Correct 8 ms 14440 KB Output is correct
13 Correct 8 ms 14420 KB Output is correct
14 Correct 8 ms 14420 KB Output is correct
15 Correct 9 ms 14420 KB Output is correct
16 Correct 8 ms 14420 KB Output is correct
17 Correct 9 ms 14448 KB Output is correct
18 Correct 8 ms 14444 KB Output is correct
19 Correct 8 ms 14420 KB Output is correct
20 Correct 9 ms 14480 KB Output is correct
21 Correct 9 ms 14448 KB Output is correct
22 Correct 10 ms 14420 KB Output is correct
23 Correct 8 ms 14468 KB Output is correct
24 Correct 9 ms 14420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14376 KB Output is correct
2 Correct 7 ms 14436 KB Output is correct
3 Correct 7 ms 14420 KB Output is correct
4 Correct 8 ms 14436 KB Output is correct
5 Correct 8 ms 14340 KB Output is correct
6 Correct 8 ms 14356 KB Output is correct
7 Correct 7 ms 14420 KB Output is correct
8 Correct 7 ms 14420 KB Output is correct
9 Correct 9 ms 14436 KB Output is correct
10 Correct 9 ms 14436 KB Output is correct
11 Correct 8 ms 14420 KB Output is correct
12 Correct 9 ms 14436 KB Output is correct
13 Correct 8 ms 14492 KB Output is correct
14 Correct 8 ms 14424 KB Output is correct
15 Correct 9 ms 14420 KB Output is correct
16 Correct 8 ms 14420 KB Output is correct
17 Correct 9 ms 14452 KB Output is correct
18 Correct 9 ms 14472 KB Output is correct
19 Correct 8 ms 14424 KB Output is correct
20 Correct 8 ms 14420 KB Output is correct
21 Correct 8 ms 14452 KB Output is correct
22 Correct 8 ms 14420 KB Output is correct
23 Correct 8 ms 14420 KB Output is correct
24 Correct 8 ms 14420 KB Output is correct
25 Correct 43 ms 18560 KB Output is correct
26 Correct 68 ms 18652 KB Output is correct
27 Correct 87 ms 19528 KB Output is correct
28 Correct 89 ms 19944 KB Output is correct
29 Correct 92 ms 20036 KB Output is correct
30 Correct 57 ms 17480 KB Output is correct
31 Correct 49 ms 19276 KB Output is correct
32 Correct 70 ms 20120 KB Output is correct
33 Correct 70 ms 19592 KB Output is correct
34 Correct 74 ms 20028 KB Output is correct
35 Correct 51 ms 19684 KB Output is correct
36 Correct 74 ms 19780 KB Output is correct
37 Correct 36 ms 18584 KB Output is correct