답안 #60540

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
60540 2018-07-24T10:12:17 Z istlemin Palembang Bridges (APIO15_bridge) C++14
100 / 100
744 ms 14052 KB
#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for(ll i = a; i < ll(b); ++i)
#define trav(a, v) for(auto& a : v)
#define all(x) x.begin(), x.end()
#define sz(x) (ll)(x).size()
#define D(x) cerr << #x << " = " << x << endl

typedef long long ll;
typedef pair<ll, ll> pii;
typedef vector<ll> vi;

#include <ext/pb_ds/assoc_container.hpp> /** keep-include */
#include <ext/pb_ds/tree_policy.hpp> /** keep-include */

using namespace __gnu_pbds;

template <class T>
using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

int main() {
    cin.sync_with_stdio(false);
	ll k,n;
	cin>>k>>n;
	if(k==1){
		ll totDist = 0;
		vector<pii> intervals;
		vector<ll> points;
		rep(i,0,n){
			char a,b;
			ll c,d;
			cin>>a>>c>>b>>d;
			if(a!=b){
				intervals.push_back(pii(c,d));
				points.push_back(c);
				points.push_back(d);
			}else{
				totDist += abs(c-d);
			}
		}
		ll bestDist = 1e18;
		if(k==1){
			sort(all(points));
			ll ahead = points.size()-1;
			ll behind = 1;
			ll dist = 0;
			rep(i,1,points.size()) dist += points[i]-points[0];
			bestDist = dist;
			rep(i,1,points.size()) {
				ll jump = points[i]-points[i-1];
				dist += jump*behind;
				dist -= jump*ahead;
				bestDist = min(dist,bestDist);
				++behind;
				--ahead;
			}
		}else{
			rep(a,0,points.size()){
				rep(b,a+1,points.size()){
					ll dist = 0;
					rep(i,0,intervals.size()){
						dist += min(abs(intervals[i].first-points[a])+abs(intervals[i].second-points[a]),
									abs(intervals[i].first-points[b])+abs(intervals[i].second-points[b]));
					}
					bestDist = min(dist,bestDist);
				}
			}
		}
		totDist += bestDist;
		totDist += intervals.size();
		cout<<totDist<<endl;
	} else {
		ll totDist = 0;
		vector<pair<ll,pii>> intervals;
		rep(i,0,n){
			char a,b;
			ll c,d;
			cin>>a>>c>>b>>d;
			if(a!=b){
				intervals.push_back(make_pair(c+d,pii(c,d)));
			}else{
				totDist += abs(c-d);
			}
		}
		ll bestDist = 0;
		if(intervals.size()>1){
			sort(all(intervals));
			vi rightTmp;
			vi leftTmp;
			rep(i,0,intervals.size()){
				if(i == 0){
					leftTmp.push_back(intervals[i].second.first);
					leftTmp.push_back(intervals[i].second.second);
				}else{
					rightTmp.push_back(intervals[i].second.first);
					rightTmp.push_back(intervals[i].second.second);
				}
			}
			multiset<ll> right(rightTmp.begin(),rightTmp.end());
			multiset<ll> left(leftTmp.begin(),leftTmp.end());
			auto rAt = right.begin();
			advance(rAt, right.size()/2);
			ll rIndex = right.size()/2;
			ll rSum = 0;
			for(auto it : right) rSum += abs(it - *rAt);
			auto lAt = left.begin();
			ll lIndex = 0;
			ll lSum = abs(intervals[0].second.second-intervals[0].second.first);
			bestDist = lSum + rSum;
			rep(i,1,intervals.size()-1){
				ll a = intervals[i].second.first;
				ll b = intervals[i].second.second;
				lSum += abs(*lAt-a) + abs(*lAt-b);
				rSum -= abs(*rAt-a) + abs(*rAt-b);
				vector<ll> p(2);
				p[0] = a; p[1] = b;
				rep(i,0,2){
					
					if(p[i]<*lAt) ++lIndex;
					left.insert(p[i]);
					
					if(p[i]==*rAt){
						auto eraseAt = rAt;
						if(rIndex>0){
							--rIndex;
							rAt = prev(rAt);
						}else{
							rAt = next(rAt);
						}
						right.erase(eraseAt);
					}else{
						if(p[i]<*rAt) --rIndex;
						right.erase(right.find(p[i]));
					}
				}
				if(lIndex>0){
					--lIndex;
					lAt = prev(lAt);
					lSum -= (lIndex*2 + 2 - left.size())*(*(next(lAt)) - *lAt);
				}
				if(rIndex>0){
					--rIndex;
					rAt = prev(rAt);
					rSum -= (rIndex*2 + 2 - right.size())*(*(next(rAt)) - *rAt);
				}
				//assert(distance(left.begin(),lAt)==lIndex);
				while(lIndex<left.size()/2){
					lSum += (lIndex*2 + 2 - left.size())*(*(next(lAt)) - *lAt);
					advance(lAt,1);
					++lIndex;
				}
				//assert(distance(right.begin(),rAt)==rIndex);
				while(rIndex<right.size()/2){
					rSum += (rIndex*2 + 2 - right.size())*(*(next(rAt)) - *rAt);
					advance(rAt,1);
					++rIndex;
				}
				bestDist = min(bestDist,lSum + rSum);
			}
		}else if(intervals.size()==1){
			bestDist = abs(intervals[0].second.second - intervals[0].second.first);
		}
		totDist += bestDist;
		totDist += intervals.size();
		cout<<totDist<<endl;
	}
}



			/*left.insert(a);
			if(a<*lAt){
				++lIndex;
				//lAt = next(lAt);
			}
			left.insert(b);
			if(b<*lAt){
				++lIndex;
				//lAt = next(lAt);
			}
			rSum -= abs(*rAt-a) + abs(*rAt-b);
			if(a == *rAt){
				auto eraseAt = rAt;
				if(rIndex!=right.size()-1)
					rAt = next(rAt);
				else{
					rAt = prev(rAt);
					--rIndex;
				}
				right.erase(eraseAt);
				//rAt = prev(rAt);
			}else{
				if(a<*rAt){
					--rIndex;
					right.erase(right.lower_bound(a));
					//rAt = prev(rAt);
				}else{
					right.erase(right.lower_bound(a));
				}
			}
			if(b == *rAt){
				auto eraseAt = rAt;
				if(rIndex!=right.size()-1)
					rAt = next(rAt);
				else{
					rAt = prev(rAt);
					--rIndex;
				}
				right.erase(eraseAt);
				//rAt = prev(rAt);
			}else{
				if(b<*rAt){
					--rIndex;
					//rAt = prev(rAt);
				}
				right.erase(right.lower_bound(b));
			}
			if(lIndex>0){
				--lIndex;
				lAt = prev(lAt);
				--lIndex;
				lAt = prev(lAt);
			}
			if(rIndex>0){
				--rIndex;
				lAt = prev(lAt);
				--rIndex;
				lAt = prev(lAt);
			}*/

Compilation message

bridge.cpp: In function 'int main()':
bridge.cpp:149:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     while(lIndex<left.size()/2){
           ~~~~~~^~~~~~~~~~~~~~
bridge.cpp:155:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     while(rIndex<right.size()/2){
           ~~~~~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 2 ms 456 KB Output is correct
3 Correct 1 ms 456 KB Output is correct
4 Correct 4 ms 616 KB Output is correct
5 Correct 3 ms 616 KB Output is correct
6 Correct 3 ms 616 KB Output is correct
7 Correct 3 ms 616 KB Output is correct
8 Correct 4 ms 616 KB Output is correct
9 Correct 3 ms 616 KB Output is correct
10 Correct 3 ms 616 KB Output is correct
11 Correct 2 ms 616 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 616 KB Output is correct
2 Correct 2 ms 616 KB Output is correct
3 Correct 3 ms 616 KB Output is correct
4 Correct 3 ms 616 KB Output is correct
5 Correct 4 ms 616 KB Output is correct
6 Correct 3 ms 616 KB Output is correct
7 Correct 4 ms 616 KB Output is correct
8 Correct 4 ms 616 KB Output is correct
9 Correct 4 ms 616 KB Output is correct
10 Correct 3 ms 616 KB Output is correct
11 Correct 4 ms 616 KB Output is correct
12 Correct 46 ms 3900 KB Output is correct
13 Correct 92 ms 4060 KB Output is correct
14 Correct 51 ms 4060 KB Output is correct
15 Correct 49 ms 4060 KB Output is correct
16 Correct 44 ms 4060 KB Output is correct
17 Correct 54 ms 4060 KB Output is correct
18 Correct 53 ms 4060 KB Output is correct
19 Correct 83 ms 4060 KB Output is correct
20 Correct 44 ms 4060 KB Output is correct
21 Correct 59 ms 4060 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4060 KB Output is correct
2 Correct 4 ms 4060 KB Output is correct
3 Correct 3 ms 4060 KB Output is correct
4 Correct 2 ms 4060 KB Output is correct
5 Correct 4 ms 4060 KB Output is correct
6 Correct 3 ms 4060 KB Output is correct
7 Correct 2 ms 4060 KB Output is correct
8 Correct 3 ms 4060 KB Output is correct
9 Correct 2 ms 4060 KB Output is correct
10 Correct 3 ms 4060 KB Output is correct
11 Correct 3 ms 4060 KB Output is correct
12 Correct 3 ms 4060 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4060 KB Output is correct
2 Correct 2 ms 4060 KB Output is correct
3 Correct 2 ms 4060 KB Output is correct
4 Correct 4 ms 4060 KB Output is correct
5 Correct 2 ms 4060 KB Output is correct
6 Correct 2 ms 4060 KB Output is correct
7 Correct 3 ms 4060 KB Output is correct
8 Correct 2 ms 4060 KB Output is correct
9 Correct 3 ms 4060 KB Output is correct
10 Correct 2 ms 4060 KB Output is correct
11 Correct 2 ms 4060 KB Output is correct
12 Correct 3 ms 4060 KB Output is correct
13 Correct 4 ms 4060 KB Output is correct
14 Correct 4 ms 4060 KB Output is correct
15 Correct 6 ms 4060 KB Output is correct
16 Correct 3 ms 4060 KB Output is correct
17 Correct 4 ms 4060 KB Output is correct
18 Correct 4 ms 4060 KB Output is correct
19 Correct 5 ms 4060 KB Output is correct
20 Correct 4 ms 4060 KB Output is correct
21 Correct 5 ms 4060 KB Output is correct
22 Correct 4 ms 4060 KB Output is correct
23 Correct 4 ms 4060 KB Output is correct
24 Correct 4 ms 4060 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4060 KB Output is correct
2 Correct 2 ms 4060 KB Output is correct
3 Correct 3 ms 4060 KB Output is correct
4 Correct 3 ms 4060 KB Output is correct
5 Correct 3 ms 4060 KB Output is correct
6 Correct 2 ms 4060 KB Output is correct
7 Correct 3 ms 4060 KB Output is correct
8 Correct 3 ms 4060 KB Output is correct
9 Correct 3 ms 4060 KB Output is correct
10 Correct 1 ms 4060 KB Output is correct
11 Correct 2 ms 4060 KB Output is correct
12 Correct 2 ms 4060 KB Output is correct
13 Correct 4 ms 4060 KB Output is correct
14 Correct 6 ms 4060 KB Output is correct
15 Correct 4 ms 4060 KB Output is correct
16 Correct 3 ms 4060 KB Output is correct
17 Correct 3 ms 4060 KB Output is correct
18 Correct 3 ms 4060 KB Output is correct
19 Correct 5 ms 4060 KB Output is correct
20 Correct 4 ms 4060 KB Output is correct
21 Correct 4 ms 4060 KB Output is correct
22 Correct 5 ms 4060 KB Output is correct
23 Correct 3 ms 4060 KB Output is correct
24 Correct 4 ms 4060 KB Output is correct
25 Correct 211 ms 13924 KB Output is correct
26 Correct 311 ms 13924 KB Output is correct
27 Correct 693 ms 13924 KB Output is correct
28 Correct 656 ms 13924 KB Output is correct
29 Correct 744 ms 14052 KB Output is correct
30 Correct 250 ms 14052 KB Output is correct
31 Correct 231 ms 14052 KB Output is correct
32 Correct 203 ms 14052 KB Output is correct
33 Correct 225 ms 14052 KB Output is correct
34 Correct 202 ms 14052 KB Output is correct
35 Correct 200 ms 14052 KB Output is correct
36 Correct 206 ms 14052 KB Output is correct
37 Correct 170 ms 14052 KB Output is correct