답안 #114333

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
114333 2019-05-31T23:07:55 Z Bruteforceman Palembang Bridges (APIO15_bridge) C++11
100 / 100
337 ms 23912 KB
#include "bits/stdc++.h"
using namespace std;
typedef pair <int, int> pii;

vector <pii> v;
long long pre[1000010], suf[1000010];

bool cmp(pii a, pii b) {
	return a.first + a.second < b.first + b.second;
}

long long solver(vector <pii> u) {
	vector <int> a;
	for(auto i : u) {
		a.push_back(i.first);
		a.push_back(i.second);
	}
	sort(a.begin(), a.end());
	long long ans = 0;
	for(int i : a) {
		ans += abs(a[a.size() / 2] - i);
	}
	return ans;
}

struct median_ds {
	multiset <int> P, Q;
	long long sumP, sumQ;
	median_ds () {
		sumP = sumQ = 0;
	}
	void add(int x) {
		if(P.empty()) {
			P.insert(x);
			sumP += x;
		} else if (*P.rbegin() < x) {
			Q.insert(x);
			sumQ += x;
		} else {
			P.insert(x);
			sumP += x;
		}
		if(P.size() < Q.size()) {
			sumQ -= *Q.begin();
			sumP += *Q.begin();
			P.insert(*Q.begin());
			Q.erase(Q.begin());
		}
		if(P.size() > Q.size() + 1) {
			sumP -= *P.rbegin();
			sumQ += *P.rbegin();
			Q.insert(*P.rbegin());
			P.erase(P.find(*P.rbegin()));
		}
	}
	long long getCost() {
		long long med = *P.rbegin();
		long long ans = sumQ - sumP;
		ans += med * P.size();
		ans -= med * Q.size();
		return ans;
	}
} S, T; 

int main(int argc, char const *argv[])
{
	int k, n;
	scanf("%d %d", &k, &n);
	
	long long add = 0;
	for(int i = 1; i <= n; i++) {
		char p, q;
		int s, t;
		scanf(" %c %d %c %d", &p, &s, &q, &t);
		if(p == q) {
			add += abs(s - t);
		} else {
			add += 1;
			if(s > t) swap(s, t);
			v.push_back(pii(s, t));
		}
	}
	if(v.size() == 0) {
		printf("%lld\n", add);
		exit(0);
	}
	sort(v.begin(), v.end(), cmp);
	for(int i = 0; i < v.size(); i++) {
		S.add(v[i].first);
		S.add(v[i].second);
		pre[i] = S.getCost();
	}
	for(int i = v.size() - 1; i >= 0; i--) {
		T.add(v[i].first);
		T.add(v[i].second);
		suf[i] = T.getCost();
	}
	
	long long ans = LLONG_MAX;
	if(k == 2) {
		for(int i = 0; i < v.size(); i++) {
			ans = min(ans, pre[i] + suf[i + 1]);
		}
	} else {
		ans = pre[v.size() - 1];
	}
	printf("%lld\n", ans + add);
	return 0;
}

Compilation message

bridge.cpp: In function 'int main(int, const char**)':
bridge.cpp:88:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < v.size(); i++) {
                 ~~^~~~~~~~~~
bridge.cpp:101:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 0; i < v.size(); i++) {
                  ~~^~~~~~~~~~
bridge.cpp:68: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:74:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf(" %c %d %c %d", &p, &s, &q, &t);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 3 ms 512 KB Output is correct
4 Correct 3 ms 512 KB Output is correct
5 Correct 3 ms 512 KB Output is correct
6 Correct 2 ms 512 KB Output is correct
7 Correct 3 ms 512 KB Output is correct
8 Correct 3 ms 512 KB Output is correct
9 Correct 3 ms 512 KB Output is correct
10 Correct 3 ms 512 KB Output is correct
11 Correct 3 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 3 ms 512 KB Output is correct
4 Correct 3 ms 512 KB Output is correct
5 Correct 3 ms 512 KB Output is correct
6 Correct 3 ms 512 KB Output is correct
7 Correct 3 ms 512 KB Output is correct
8 Correct 3 ms 512 KB Output is correct
9 Correct 3 ms 512 KB Output is correct
10 Correct 3 ms 512 KB Output is correct
11 Correct 3 ms 512 KB Output is correct
12 Correct 165 ms 21456 KB Output is correct
13 Correct 313 ms 23864 KB Output is correct
14 Correct 267 ms 20460 KB Output is correct
15 Correct 158 ms 14192 KB Output is correct
16 Correct 138 ms 23180 KB Output is correct
17 Correct 182 ms 23832 KB Output is correct
18 Correct 110 ms 23404 KB Output is correct
19 Correct 182 ms 23788 KB Output is correct
20 Correct 182 ms 23448 KB Output is correct
21 Correct 186 ms 23580 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 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 3 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 3 ms 512 KB Output is correct
14 Correct 3 ms 640 KB Output is correct
15 Correct 3 ms 512 KB Output is correct
16 Correct 2 ms 384 KB Output is correct
17 Correct 3 ms 512 KB Output is correct
18 Correct 3 ms 384 KB Output is correct
19 Correct 2 ms 512 KB Output is correct
20 Correct 3 ms 512 KB Output is correct
21 Correct 3 ms 512 KB Output is correct
22 Correct 3 ms 512 KB Output is correct
23 Correct 3 ms 512 KB Output is correct
24 Correct 3 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 3 ms 640 KB Output is correct
14 Correct 3 ms 508 KB Output is correct
15 Correct 3 ms 512 KB Output is correct
16 Correct 2 ms 384 KB Output is correct
17 Correct 3 ms 384 KB Output is correct
18 Correct 2 ms 384 KB Output is correct
19 Correct 3 ms 512 KB Output is correct
20 Correct 3 ms 512 KB Output is correct
21 Correct 3 ms 512 KB Output is correct
22 Correct 3 ms 512 KB Output is correct
23 Correct 3 ms 512 KB Output is correct
24 Correct 3 ms 512 KB Output is correct
25 Correct 168 ms 21484 KB Output is correct
26 Correct 229 ms 22484 KB Output is correct
27 Correct 333 ms 23280 KB Output is correct
28 Correct 337 ms 23912 KB Output is correct
29 Correct 333 ms 23876 KB Output is correct
30 Correct 133 ms 12784 KB Output is correct
31 Correct 137 ms 23144 KB Output is correct
32 Correct 179 ms 23888 KB Output is correct
33 Correct 110 ms 23500 KB Output is correct
34 Correct 179 ms 23900 KB Output is correct
35 Correct 181 ms 23300 KB Output is correct
36 Correct 190 ms 23592 KB Output is correct
37 Correct 180 ms 22252 KB Output is correct