답안 #14389

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
14389 2015-05-13T16:49:19 Z kriii Palembang Bridges (APIO15_bridge) C++14
100 / 100
1323 ms 22008 KB
#include <stdio.h>
#include <functional>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
using namespace std;

struct intv{
	intv(long long s_, long long e_){
		s = s_; e = e_;
	}
	long long s,e;
};
bool cmpm(const intv &a, const intv &b){return a.s + a.e < b.s + b.e;}
vector<intv> seg;
set<int> xs;

map<int, int, greater<int> > A[2]; long long sza[2],sa[2];
map<int, int> B[2]; long long szb[2],sb[2];

int K,N;

void push(int i, int x)
{
	A[i][x]++; sza[i]++; sa[i] += x;
	int p = A[i].begin()->first;
	B[i][p]++; szb[i]++; sb[i] += p;
	if (--A[i][p] == 0) A[i].erase(p); sza[i]--; sa[i] -= p;
	if (szb[i] > sza[i]){
		p = B[i].begin()->first;
		A[i][p]++; sza[i]++; sa[i] += p;
		if (--B[i][p] == 0) B[i].erase(p); szb[i]--; sb[i] -= p;
	}
}

void pop(int i, int x)
{
	if (A[i].count(x)){
		if (--A[i][x] == 0) A[i].erase(x); sza[i]--; sa[i] -= x;
	}
	else if (B[i].count(x)){
		if (--B[i][x] == 0) B[i].erase(x); szb[i]--; sb[i] -= x;
	}
	while (sza[i] >= szb[i] + 2){
		int p = A[i].begin()->first;
		B[i][p]++; szb[i]++; sb[i] += p;
		if (--A[i][p] == 0) A[i].erase(p); sza[i]--; sa[i] -= p;
	}
	while (sza[i] + 1 <= szb[i]){
		int p = B[i].begin()->first;
		A[i][p]++; sza[i]++; sa[i] += p;
		if (--B[i][p] == 0) B[i].erase(p); szb[i]--; sb[i] -= p;
	}
}

long long sum(int i)
{
	if (A[i].empty()) return 0;
	long long pos = A[i].begin()->first, res = 0;
	res += pos * sza[i] - sa[i];
	res += sb[i] - pos * szb[i];
	return res;
}

int main()
{
	long long ans = 0;
	scanf ("%d %d ",&K,&N);
	while (N--){
		char a,b; int x,y;
		scanf ("%c %d %c %d ",&a,&x,&b,&y);
		if (a == b){
			ans += abs(x-y);
		}
		else{
			ans++;
			if (x > y) swap(x,y);
			seg.push_back(intv(x,y));

			push(0,x); push(0,y);
			xs.insert(x); xs.insert(y);
		}
	}

	if (K == 1){
		ans += sum(0);
	}
	else{
		sort(seg.begin(),seg.end(),cmpm);
		long long good = sum(0);
		for (auto p : seg){
			push(1,p.s); push(1,p.e);
			pop(0,p.s); pop(0,p.e);
			good = min(good,sum(0)+sum(1));
		}
		ans += good;
	}

	printf ("%lld\n",ans);

	return 0;
}

Compilation message

bridge.cpp: In function 'int main()':
bridge.cpp:69:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf ("%d %d ",&K,&N);
                        ^
bridge.cpp:72:37: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf ("%c %d %c %d ",&a,&x,&b,&y);
                                     ^
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 1184 KB Output is correct
2 Correct 0 ms 1184 KB Output is correct
3 Correct 0 ms 1184 KB Output is correct
4 Correct 0 ms 1452 KB Output is correct
5 Correct 0 ms 1452 KB Output is correct
6 Correct 0 ms 1184 KB Output is correct
7 Correct 0 ms 1452 KB Output is correct
8 Correct 0 ms 1452 KB Output is correct
9 Correct 0 ms 1452 KB Output is correct
10 Correct 0 ms 1184 KB Output is correct
11 Correct 3 ms 1452 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 1184 KB Output is correct
2 Correct 0 ms 1184 KB Output is correct
3 Correct 0 ms 1184 KB Output is correct
4 Correct 0 ms 1452 KB Output is correct
5 Correct 0 ms 1452 KB Output is correct
6 Correct 0 ms 1184 KB Output is correct
7 Correct 0 ms 1452 KB Output is correct
8 Correct 0 ms 1452 KB Output is correct
9 Correct 0 ms 1452 KB Output is correct
10 Correct 0 ms 1184 KB Output is correct
11 Correct 0 ms 1452 KB Output is correct
12 Correct 43 ms 4340 KB Output is correct
13 Correct 536 ms 22008 KB Output is correct
14 Correct 146 ms 5192 KB Output is correct
15 Correct 299 ms 13328 KB Output is correct
16 Correct 49 ms 4340 KB Output is correct
17 Correct 323 ms 22008 KB Output is correct
18 Correct 409 ms 22008 KB Output is correct
19 Correct 436 ms 21064 KB Output is correct
20 Correct 36 ms 4340 KB Output is correct
21 Correct 266 ms 22008 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 1184 KB Output is correct
2 Correct 0 ms 1184 KB Output is correct
3 Correct 0 ms 1184 KB Output is correct
4 Correct 0 ms 1184 KB Output is correct
5 Correct 0 ms 1184 KB Output is correct
6 Correct 0 ms 1184 KB Output is correct
7 Correct 0 ms 1184 KB Output is correct
8 Correct 0 ms 1184 KB Output is correct
9 Correct 0 ms 1184 KB Output is correct
10 Correct 0 ms 1184 KB Output is correct
11 Correct 0 ms 1184 KB Output is correct
12 Correct 0 ms 1184 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 1184 KB Output is correct
2 Correct 0 ms 1184 KB Output is correct
3 Correct 0 ms 1184 KB Output is correct
4 Correct 0 ms 1184 KB Output is correct
5 Correct 0 ms 1184 KB Output is correct
6 Correct 0 ms 1184 KB Output is correct
7 Correct 0 ms 1184 KB Output is correct
8 Correct 0 ms 1184 KB Output is correct
9 Correct 0 ms 1184 KB Output is correct
10 Correct 0 ms 1184 KB Output is correct
11 Correct 0 ms 1184 KB Output is correct
12 Correct 0 ms 1184 KB Output is correct
13 Correct 0 ms 1184 KB Output is correct
14 Correct 0 ms 1184 KB Output is correct
15 Correct 6 ms 1452 KB Output is correct
16 Correct 0 ms 1184 KB Output is correct
17 Correct 0 ms 1184 KB Output is correct
18 Correct 0 ms 1320 KB Output is correct
19 Correct 0 ms 1184 KB Output is correct
20 Correct 3 ms 1452 KB Output is correct
21 Correct 3 ms 1452 KB Output is correct
22 Correct 3 ms 1448 KB Output is correct
23 Correct 0 ms 1184 KB Output is correct
24 Correct 3 ms 1452 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 1184 KB Output is correct
2 Correct 0 ms 1184 KB Output is correct
3 Correct 0 ms 1184 KB Output is correct
4 Correct 0 ms 1184 KB Output is correct
5 Correct 0 ms 1184 KB Output is correct
6 Correct 0 ms 1184 KB Output is correct
7 Correct 0 ms 1184 KB Output is correct
8 Correct 0 ms 1184 KB Output is correct
9 Correct 0 ms 1184 KB Output is correct
10 Correct 0 ms 1184 KB Output is correct
11 Correct 0 ms 1184 KB Output is correct
12 Correct 0 ms 1184 KB Output is correct
13 Correct 0 ms 1184 KB Output is correct
14 Correct 0 ms 1184 KB Output is correct
15 Correct 3 ms 1452 KB Output is correct
16 Correct 0 ms 1184 KB Output is correct
17 Correct 0 ms 1184 KB Output is correct
18 Correct 0 ms 1320 KB Output is correct
19 Correct 0 ms 1184 KB Output is correct
20 Correct 3 ms 1452 KB Output is correct
21 Correct 3 ms 1452 KB Output is correct
22 Correct 3 ms 1448 KB Output is correct
23 Correct 0 ms 1184 KB Output is correct
24 Correct 0 ms 1452 KB Output is correct
25 Correct 49 ms 4340 KB Output is correct
26 Correct 109 ms 4352 KB Output is correct
27 Correct 1323 ms 20556 KB Output is correct
28 Correct 1136 ms 22008 KB Output is correct
29 Correct 1323 ms 22008 KB Output is correct
30 Correct 593 ms 12140 KB Output is correct
31 Correct 93 ms 4340 KB Output is correct
32 Correct 666 ms 22008 KB Output is correct
33 Correct 653 ms 22008 KB Output is correct
34 Correct 659 ms 21200 KB Output is correct
35 Correct 86 ms 4340 KB Output is correct
36 Correct 466 ms 22008 KB Output is correct
37 Correct 43 ms 4340 KB Output is correct