Submission #56627

# Submission time Handle Problem Language Result Execution time Memory
56627 2018-07-12T03:36:48 Z leejseo Palembang Bridges (APIO15_bridge) C++17
63 / 100
1675 ms 3868 KB
#include <bits/stdc++.h>
using namespace std;
 
int K, N;
 
void solve1(){
	long long ans = 0;
	vector<int> L;
	int u, v;
	char x, y;
	for(int i=0; i<N; i++){
		scanf("%c %d %c %d\n", &x, &u, &y, &v);		
		if (x == y) ans += abs(u-v);
		else{
			L.push_back(u);
			L.push_back(v);
		}
	}
	sort(L.begin(), L.end());
	int M = L.size();
	if (M == 0){
		printf("%lld\n", ans);
		return;
	}
	long long mid = L[M/2];
	for (int i=0; i<M; i++) ans += abs(L[i] - mid);
	ans += M/2;
	printf("%lld\n", ans);
	L.clear();
	return;
}
 
long long f(vector<int>&X, vector<int>&Y, int s){
	vector<int> lo, hi;
	int N = X.size();
	for (int i=0; i<N; i++){
		if (X[i] + Y[i] < s){
			lo.push_back(X[i]);
			lo.push_back(Y[i]);
		}
		else{
			hi.push_back(X[i]);
			hi.push_back(Y[i]);
		}
	}
	long long ans = 0LL;
	sort(lo.begin(), lo.end());
	sort(hi.begin(), hi.end());
	for (int i=0; i<N; i++){
		if (X[i] + Y[i] < s) ans += abs(X[i]-lo[lo.size()/2])+abs(Y[i]-lo[lo.size()/2]); 
		else ans += abs(X[i]-hi[hi.size()/2])+abs(Y[i]-hi[hi.size()/2]);
	}
	lo.clear();
	hi.clear();
	return ans;
}
 
void solve2(){
	long long ans = 0;
	vector<int> S;
	vector<int> X;
	vector<int> Y;
	int u, v;
	char x, y;
	for(int i=0; i<N; i++){
		scanf("%c %d %c %d\n", &x, &u, &y, &v);		
		if (x == y) ans += abs(u-v);
		else{
			S.push_back(u+v);
			X.push_back(u);
			Y.push_back(v);
		}
	}
	int M = X.size();
	if (M == 0){
		printf("%lld\n", ans);
		return;
	}
	sort(S.begin(), S.end());
	unique(S.begin(), S.end());
	int lo = 0, hi = S.size()-1;
	long long temp = 1LL << 61;
    if (hi <= 2000){
		for (int i=lo; i<=hi; i++){
			temp = min(temp, f(X, Y, S[i]));
		}
    }
    else{
      while (lo + 55 < hi){
			int llh = (lo + lo + hi)/3;
			int lhh = (lo + hi + hi + 1)/3;
			long long left = f(X, Y, S[llh]), right = f(X, Y, S[lhh]);
			if (left < right) hi = lhh - 1;
			else if (left == right){
				lo = llh;
				hi = lhh;
			}
			else lo = llh+1;
		}
		for (int i=lo; i<=hi; i++){
			temp = min(temp, f(X, Y, S[i]));
		}
    }
	ans += temp;
	ans += M;
	printf("%lld\n", ans);
	return;
}
int main(void){
	scanf("%d%d\n", &K, &N);
	if (K == 1) solve1();
	else solve2();
}

Compilation message

bridge.cpp: In function 'void solve1()':
bridge.cpp:12:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%c %d %c %d\n", &x, &u, &y, &v);  
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bridge.cpp: In function 'void solve2()':
bridge.cpp:66:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%c %d %c %d\n", &x, &u, &y, &v);  
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bridge.cpp: In function 'int main()':
bridge.cpp:110:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d\n", &K, &N);
  ~~~~~^~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 3 ms 488 KB Output is correct
3 Correct 3 ms 488 KB Output is correct
4 Correct 2 ms 488 KB Output is correct
5 Correct 3 ms 496 KB Output is correct
6 Correct 2 ms 624 KB Output is correct
7 Correct 3 ms 624 KB Output is correct
8 Correct 3 ms 624 KB Output is correct
9 Correct 4 ms 624 KB Output is correct
10 Correct 3 ms 624 KB Output is correct
11 Correct 3 ms 624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 624 KB Output is correct
2 Correct 2 ms 624 KB Output is correct
3 Correct 3 ms 624 KB Output is correct
4 Correct 3 ms 624 KB Output is correct
5 Correct 3 ms 624 KB Output is correct
6 Correct 3 ms 624 KB Output is correct
7 Correct 2 ms 624 KB Output is correct
8 Correct 3 ms 624 KB Output is correct
9 Correct 3 ms 624 KB Output is correct
10 Correct 3 ms 624 KB Output is correct
11 Correct 3 ms 624 KB Output is correct
12 Correct 39 ms 1764 KB Output is correct
13 Correct 82 ms 1768 KB Output is correct
14 Correct 52 ms 1768 KB Output is correct
15 Correct 43 ms 1768 KB Output is correct
16 Correct 48 ms 1768 KB Output is correct
17 Correct 74 ms 1768 KB Output is correct
18 Correct 64 ms 1768 KB Output is correct
19 Correct 86 ms 1768 KB Output is correct
20 Correct 42 ms 1768 KB Output is correct
21 Correct 67 ms 1768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1768 KB Output is correct
2 Correct 2 ms 1768 KB Output is correct
3 Correct 2 ms 1768 KB Output is correct
4 Correct 3 ms 1768 KB Output is correct
5 Correct 3 ms 1768 KB Output is correct
6 Correct 2 ms 1768 KB Output is correct
7 Correct 3 ms 1768 KB Output is correct
8 Correct 3 ms 1768 KB Output is correct
9 Correct 3 ms 1768 KB Output is correct
10 Correct 3 ms 1768 KB Output is correct
11 Correct 4 ms 1768 KB Output is correct
12 Correct 3 ms 1768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1768 KB Output is correct
2 Correct 2 ms 1768 KB Output is correct
3 Correct 2 ms 1768 KB Output is correct
4 Correct 3 ms 1768 KB Output is correct
5 Correct 2 ms 1768 KB Output is correct
6 Correct 2 ms 1768 KB Output is correct
7 Correct 2 ms 1768 KB Output is correct
8 Correct 3 ms 1768 KB Output is correct
9 Correct 3 ms 1768 KB Output is correct
10 Correct 3 ms 1768 KB Output is correct
11 Correct 3 ms 1768 KB Output is correct
12 Correct 3 ms 1768 KB Output is correct
13 Correct 36 ms 1768 KB Output is correct
14 Correct 104 ms 1768 KB Output is correct
15 Correct 131 ms 1768 KB Output is correct
16 Correct 5 ms 1768 KB Output is correct
17 Correct 30 ms 1768 KB Output is correct
18 Correct 23 ms 1768 KB Output is correct
19 Correct 55 ms 1768 KB Output is correct
20 Correct 66 ms 1768 KB Output is correct
21 Correct 77 ms 1768 KB Output is correct
22 Correct 119 ms 1768 KB Output is correct
23 Correct 37 ms 1768 KB Output is correct
24 Correct 95 ms 1768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1768 KB Output is correct
2 Correct 3 ms 1768 KB Output is correct
3 Correct 3 ms 1768 KB Output is correct
4 Correct 3 ms 1768 KB Output is correct
5 Correct 4 ms 1768 KB Output is correct
6 Correct 3 ms 1768 KB Output is correct
7 Correct 3 ms 1768 KB Output is correct
8 Correct 4 ms 1768 KB Output is correct
9 Correct 2 ms 1768 KB Output is correct
10 Correct 4 ms 1768 KB Output is correct
11 Correct 4 ms 1768 KB Output is correct
12 Correct 3 ms 1768 KB Output is correct
13 Correct 33 ms 1768 KB Output is correct
14 Correct 99 ms 1768 KB Output is correct
15 Correct 131 ms 1768 KB Output is correct
16 Correct 8 ms 1768 KB Output is correct
17 Correct 35 ms 1768 KB Output is correct
18 Correct 19 ms 1768 KB Output is correct
19 Correct 36 ms 1768 KB Output is correct
20 Correct 51 ms 1768 KB Output is correct
21 Correct 90 ms 1768 KB Output is correct
22 Correct 109 ms 1768 KB Output is correct
23 Correct 37 ms 1768 KB Output is correct
24 Correct 85 ms 1768 KB Output is correct
25 Correct 383 ms 3428 KB Output is correct
26 Correct 600 ms 3796 KB Output is correct
27 Correct 1573 ms 3796 KB Output is correct
28 Correct 1664 ms 3796 KB Output is correct
29 Correct 1675 ms 3860 KB Output is correct
30 Correct 827 ms 3860 KB Output is correct
31 Correct 489 ms 3868 KB Output is correct
32 Correct 536 ms 3868 KB Output is correct
33 Correct 1014 ms 3868 KB Output is correct
34 Correct 1560 ms 3868 KB Output is correct
35 Correct 325 ms 3868 KB Output is correct
36 Incorrect 1033 ms 3868 KB Output isn't correct
37 Halted 0 ms 0 KB -