답안 #29379

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
29379 2017-07-19T08:02:56 Z Namnamseo Palembang Bridges (APIO15_bridge) C++14
100 / 100
959 ms 27136 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pp;
typedef pair<ll,ll> pll;
void read(int& x){ scanf("%d",&x); }
void read(ll& x){ scanf("%lld",&x); }
template<typename T,typename... Args>
void read(T& a,Args&... b){ read(a); read(b...); }
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define eb emplace_back
#define x first
#define y second

int k, n;
vector<pp> d;
ll ans_base;

void in(){
	read(k, n);
	for(int i=1; i<=n; ++i){
		char p, q;
		int s, t;
		auto f = [](){char t; do{t=getchar();}while(t!='A'&&t!='B'); return t;};
		p = f(); read(s);
		q = f(); read(t);
		if(p == q){
			ans_base += abs(s - t);
		} else {
			++ans_base;
			d.eb(min(s, t), max(s, t));
		}
	}
	n = d.size();
}

ll dp1[100010];
ll dp2[100010];

struct SEG {
	const static int M = 262144;
	ll tree[M<<1];
	void init(){ memset(tree, 0, sizeof(tree)); }
	void upd(int p, ll v){for(p+=M;p;p/=2)tree[p]+=v;}
	ll s(int l,int r){
		ll ret=0;
		l+=M; r+=M;
		while(l<=r){
			if(l&1) ret+=tree[l++];
			if(r%2==0) ret+=tree[r--];
			l>>=1; r>>=1;
		}
		return ret;
	}
} sc, sl, sr;

int pt[200010], ptn;
multiset<int> low, high;
void Work(ll dp[100010]){
	sc.init(); sl.init(); sr.init();
	low.clear(); high.clear();
	auto add = [&](int a){
		((high.size() && a < (*high.begin())) ? low : high).insert(a);
		int b = lower_bound(pt, pt+ptn, a) - pt;
		sc.upd(b, 1);
		sl.upd(b, -a);
		sr.upd(b, a);
	};
	for(int i=1; i<=n; ++i){
		add(d[i-1].x);
		add(d[i-1].y);
		while(low.size() < high.size()){
			low.insert(*high.begin()); high.erase(high.begin());
		}
		while(low.size() > high.size()){
			high.insert(*--low.end()); low.erase(--low.end());
		}
		int mid = *high.begin();
		int p = lower_bound(pt, pt+ptn, mid) - pt;
		dp[i] = sc.s(0, p-1) * mid + sl.s(0, p-1) + 
			sr.s(p+1, ptn-1) - sc.s(p+1, ptn-1) * mid;
	}
}

int main()
{
	in();
	for(auto tmp:d) pt[ptn++]=tmp.x, pt[ptn++]=tmp.y;
	sort(pt, pt+ptn);
	if(k == 1){
		ll ans=0; int p=pt[n];
		for(auto tmp:d) ans += abs(tmp.x-p) + abs(tmp.y-p);
		printf("%lld\n", ans + ans_base);
		return 0;
	}
	ptn=unique(pt,pt+ptn)-pt;
	sort(all(d), [&](const pp& a, const pp& b){
		return a.x+a.y < b.x+b.y;
	});
	Work(dp1);
	reverse(all(d));
	Work(dp2); reverse(dp2, dp2+n+1);
	ll ans = 1LL << 60;
	for(int i=0; i<=n; ++i) ans = min(ans, dp1[i] + dp2[i]);
	printf("%lld\n", ans + ans_base);
	return 0;
}

Compilation message

bridge.cpp: In function 'void read(int&)':
bridge.cpp:6:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 void read(int& x){ scanf("%d",&x); }
                                  ^
bridge.cpp: In function 'void read(ll&)':
bridge.cpp:7:35: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 void read(ll& x){ scanf("%lld",&x); }
                                   ^
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 16660 KB Output is correct
2 Correct 0 ms 16660 KB Output is correct
3 Correct 0 ms 16660 KB Output is correct
4 Correct 0 ms 16660 KB Output is correct
5 Correct 0 ms 16660 KB Output is correct
6 Correct 0 ms 16660 KB Output is correct
7 Correct 0 ms 16660 KB Output is correct
8 Correct 0 ms 16660 KB Output is correct
9 Correct 0 ms 16660 KB Output is correct
10 Correct 0 ms 16660 KB Output is correct
11 Correct 0 ms 16660 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 16660 KB Output is correct
2 Correct 0 ms 16660 KB Output is correct
3 Correct 0 ms 16660 KB Output is correct
4 Correct 0 ms 16660 KB Output is correct
5 Correct 0 ms 16660 KB Output is correct
6 Correct 0 ms 16660 KB Output is correct
7 Correct 0 ms 16660 KB Output is correct
8 Correct 0 ms 16660 KB Output is correct
9 Correct 0 ms 16660 KB Output is correct
10 Correct 0 ms 16660 KB Output is correct
11 Correct 0 ms 16660 KB Output is correct
12 Correct 39 ms 18280 KB Output is correct
13 Correct 69 ms 18280 KB Output is correct
14 Correct 43 ms 18280 KB Output is correct
15 Correct 33 ms 17512 KB Output is correct
16 Correct 29 ms 18280 KB Output is correct
17 Correct 39 ms 18280 KB Output is correct
18 Correct 39 ms 18280 KB Output is correct
19 Correct 86 ms 18280 KB Output is correct
20 Correct 33 ms 18280 KB Output is correct
21 Correct 53 ms 18280 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 16660 KB Output is correct
2 Correct 0 ms 16660 KB Output is correct
3 Correct 0 ms 16660 KB Output is correct
4 Correct 3 ms 16660 KB Output is correct
5 Correct 3 ms 16660 KB Output is correct
6 Correct 0 ms 16660 KB Output is correct
7 Correct 3 ms 16660 KB Output is correct
8 Correct 0 ms 16660 KB Output is correct
9 Correct 3 ms 16660 KB Output is correct
10 Correct 3 ms 16660 KB Output is correct
11 Correct 6 ms 16660 KB Output is correct
12 Correct 3 ms 16660 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 16660 KB Output is correct
2 Correct 0 ms 16660 KB Output is correct
3 Correct 3 ms 16660 KB Output is correct
4 Correct 0 ms 16660 KB Output is correct
5 Correct 3 ms 16660 KB Output is correct
6 Correct 3 ms 16660 KB Output is correct
7 Correct 3 ms 16660 KB Output is correct
8 Correct 3 ms 16660 KB Output is correct
9 Correct 3 ms 16660 KB Output is correct
10 Correct 3 ms 16660 KB Output is correct
11 Correct 3 ms 16660 KB Output is correct
12 Correct 0 ms 16660 KB Output is correct
13 Correct 3 ms 16792 KB Output is correct
14 Correct 0 ms 16792 KB Output is correct
15 Correct 9 ms 16792 KB Output is correct
16 Correct 3 ms 16660 KB Output is correct
17 Correct 6 ms 16792 KB Output is correct
18 Correct 0 ms 16660 KB Output is correct
19 Correct 6 ms 16792 KB Output is correct
20 Correct 3 ms 16792 KB Output is correct
21 Correct 9 ms 16792 KB Output is correct
22 Correct 6 ms 16792 KB Output is correct
23 Correct 3 ms 16792 KB Output is correct
24 Correct 9 ms 16792 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 16660 KB Output is correct
2 Correct 3 ms 16660 KB Output is correct
3 Correct 3 ms 16660 KB Output is correct
4 Correct 3 ms 16660 KB Output is correct
5 Correct 3 ms 16660 KB Output is correct
6 Correct 6 ms 16660 KB Output is correct
7 Correct 0 ms 16660 KB Output is correct
8 Correct 3 ms 16660 KB Output is correct
9 Correct 0 ms 16660 KB Output is correct
10 Correct 0 ms 16660 KB Output is correct
11 Correct 0 ms 16660 KB Output is correct
12 Correct 0 ms 16660 KB Output is correct
13 Correct 3 ms 16792 KB Output is correct
14 Correct 6 ms 16792 KB Output is correct
15 Correct 3 ms 16792 KB Output is correct
16 Correct 3 ms 16660 KB Output is correct
17 Correct 0 ms 16792 KB Output is correct
18 Correct 6 ms 16660 KB Output is correct
19 Correct 9 ms 16792 KB Output is correct
20 Correct 6 ms 16792 KB Output is correct
21 Correct 6 ms 16792 KB Output is correct
22 Correct 3 ms 16792 KB Output is correct
23 Correct 3 ms 16792 KB Output is correct
24 Correct 0 ms 16792 KB Output is correct
25 Correct 306 ms 27136 KB Output is correct
26 Correct 393 ms 27136 KB Output is correct
27 Correct 779 ms 27136 KB Output is correct
28 Correct 899 ms 27136 KB Output is correct
29 Correct 959 ms 27136 KB Output is correct
30 Correct 259 ms 22136 KB Output is correct
31 Correct 263 ms 27136 KB Output is correct
32 Correct 403 ms 27136 KB Output is correct
33 Correct 283 ms 27136 KB Output is correct
34 Correct 409 ms 27136 KB Output is correct
35 Correct 293 ms 27136 KB Output is correct
36 Correct 396 ms 27136 KB Output is correct
37 Correct 286 ms 27136 KB Output is correct