답안 #185618

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
185618 2020-01-12T02:07:58 Z dndhk Palembang Bridges (APIO15_bridge) C++14
100 / 100
115 ms 8912 KB
#include <bits/stdc++.h>

#define pb push_back

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAX_N = 100000;
int N, K;
ll ans = 0;

vector<pll> vt;
vector<pll> v;

void solve1(){
	ll sidx = 0, scnt = 0;
	for(int i=0; i<vt.size(); i++){
		v.pb({vt[i].first, 1});
		v.pb({vt[i].second, -1});
		sidx+=vt[i].first;
		scnt--;
	}
	sort(v.begin(), v.end());
	ll sum = -1;
	for(int i=0; i<v.size(); i++){
		//cout<<v[i].first<<" "<<2LL * (sidx+scnt*v[i].first)+ans<<endl;
		if(sum==-1 || sum>sidx+scnt*v[i].first){
			sum = sidx + scnt*v[i].first;
		}
		if(v[i].second==1){
			sidx-=v[i].first;
			scnt++;
		}else{
			scnt++;
			sidx-=v[i].first;
		}
	}
	if(sum!=-1)	ans += 2LL * sum;
}

bool sf1(pll a, pll b){
	return a.first+a.second<b.first+b.second;
}
bool sf2(pll a, pll b){
	return a.first+a.second>b.first+b.second;
}

ll cost[2][MAX_N+1];

priority_queue<ll> pq1;
priority_queue<ll, vector<ll>, greater<ll> > pq2;

void calc(int idx){
	while(!pq1.empty())	pq1.pop();
	while(!pq2.empty())	pq2.pop();
	cost[idx][1] = 0;
	ll h = 0;
	pq1.push(vt[0].first); pq2.push(vt[0].second);
	for(int i=1; i<vt.size(); i++){
		ll l = vt[i].first, r = vt[i].second;
		if(l>pq2.top()){
			h+=l-pq2.top();
			pq1.push(pq2.top());
			pq2.pop();
			pq2.push(l);
			pq2.push(r);
		}else if(r<pq1.top()){
			h+=pq1.top()-r;
			pq2.push(pq1.top());
			pq1.pop();
			pq1.push(r);
			pq1.push(l);
		}else{
			pq1.push(l);
			pq2.push(r);
		}
		cost[idx][i+1] = h;
	}
}

void solve2(){
	if(vt.empty())	return;
	sort(vt.begin(), vt.end(), sf1);
	calc(0);
	for(int j=0; j<vt.size()-1; j++){
		if(vt.size()-1-j<=j)	break;
		pll tmp = vt[j];
		vt[j] = vt[vt.size()-1-j];
		vt[vt.size()-1-j] = tmp;

	}
	calc(1);
	ll sum = cost[0][0] + cost[1][vt.size()];
	for(int j=1; j<=vt.size(); j++){
		sum = min(sum, cost[0][j] + cost[1][vt.size()-j]);
	}
	ans+=2LL*sum;
}

int main(){
	scanf("%d%d", &K, &N);
	for(int i=0; i<N; i++){
		getchar();
		char c1, c2; 
		int a, b;
		scanf("%c %d %c %d", &c1, &a, &c2, &b);
		if(c1==c2){
			ans+=(ll)(max(a, b) - min(a, b));
		}else{
			ans += (ll)(max(a, b) - min(a, b) + 1);
			vt.pb({(ll)min(a, b), (ll)max(a, b)});
		}
	}
	if(K==1){
		if(!vt.empty()){
			calc(0);
			ans += 2LL * (ll)cost[0][vt.size()];
		}
		//solve1();
	}else if(K==2){
		solve2();
	}
	cout<<ans;
	return 0;
}

Compilation message

bridge.cpp: In function 'void solve1()':
bridge.cpp:20:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0; i<vt.size(); i++){
               ~^~~~~~~~~~
bridge.cpp:28:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0; i<v.size(); i++){
               ~^~~~~~~~~
bridge.cpp: In function 'void calc(int)':
bridge.cpp:62:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=1; i<vt.size(); i++){
               ~^~~~~~~~~~
bridge.cpp: In function 'void solve2()':
bridge.cpp:88:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int j=0; j<vt.size()-1; j++){
               ~^~~~~~~~~~~~
bridge.cpp:89:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   if(vt.size()-1-j<=j) break;
      ~~~~~~~~~~~~~^~~
bridge.cpp:97:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int j=1; j<=vt.size(); j++){
               ~^~~~~~~~~~~
bridge.cpp: In function 'int main()':
bridge.cpp:104:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &K, &N);
  ~~~~~^~~~~~~~~~~~~~~~
bridge.cpp:109:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%c %d %c %d", &c1, &a, &c2, &b);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 3 ms 380 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 3 ms 380 KB Output is correct
9 Correct 3 ms 248 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 3 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 376 KB Output is correct
2 Correct 2 ms 424 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 3 ms 376 KB Output is correct
8 Correct 0 ms 248 KB Output is correct
9 Correct 3 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 3 ms 376 KB Output is correct
12 Correct 35 ms 5604 KB Output is correct
13 Correct 61 ms 6116 KB Output is correct
14 Correct 43 ms 4956 KB Output is correct
15 Correct 38 ms 3660 KB Output is correct
16 Correct 37 ms 5580 KB Output is correct
17 Correct 65 ms 5604 KB Output is correct
18 Correct 50 ms 5604 KB Output is correct
19 Correct 69 ms 6244 KB Output is correct
20 Correct 56 ms 5980 KB Output is correct
21 Correct 62 ms 5572 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 11 ms 348 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 1 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 3 ms 380 KB Output is correct
7 Correct 3 ms 376 KB Output is correct
8 Correct 3 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 400 KB Output is correct
12 Correct 3 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 2 ms 252 KB Output is correct
5 Correct 7 ms 504 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
7 Correct 3 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 3 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 12 ms 376 KB Output is correct
14 Correct 3 ms 376 KB Output is correct
15 Correct 5 ms 376 KB Output is correct
16 Correct 3 ms 376 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 380 KB Output is correct
19 Correct 6 ms 376 KB Output is correct
20 Correct 7 ms 376 KB Output is correct
21 Correct 3 ms 376 KB Output is correct
22 Correct 2 ms 376 KB Output is correct
23 Correct 4 ms 376 KB Output is correct
24 Correct 3 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 504 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 3 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 380 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 8 ms 380 KB Output is correct
14 Correct 1 ms 376 KB Output is correct
15 Correct 3 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 380 KB Output is correct
18 Correct 3 ms 376 KB Output is correct
19 Correct 3 ms 376 KB Output is correct
20 Correct 9 ms 376 KB Output is correct
21 Correct 3 ms 376 KB Output is correct
22 Correct 9 ms 376 KB Output is correct
23 Correct 3 ms 376 KB Output is correct
24 Correct 3 ms 376 KB Output is correct
25 Correct 50 ms 7268 KB Output is correct
26 Correct 80 ms 7268 KB Output is correct
27 Correct 108 ms 8164 KB Output is correct
28 Correct 115 ms 8676 KB Output is correct
29 Correct 115 ms 8780 KB Output is correct
30 Correct 59 ms 4460 KB Output is correct
31 Correct 52 ms 8020 KB Output is correct
32 Correct 104 ms 8820 KB Output is correct
33 Correct 73 ms 8432 KB Output is correct
34 Correct 107 ms 8912 KB Output is correct
35 Correct 72 ms 8340 KB Output is correct
36 Correct 97 ms 8420 KB Output is correct
37 Correct 44 ms 7400 KB Output is correct