Submission #51129

# Submission time Handle Problem Language Result Execution time Memory
51129 2018-06-16T06:53:41 Z Utaha Palembang Bridges (APIO15_bridge) C++14
100 / 100
153 ms 45792 KB
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<double,double> pdd;
#define IOS ios_base::sync_with_stdio(0); cin.tie(0)
#define ALL(a) a.begin(),a.end()
#define SZ(a) ((int)a.size())
#define F first
#define S second
#define REP(i,n) for(int i=0;i<((int)n);i++)
#define pb push_back
#define MP(a,b) make_pair(a,b)
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#ifdef leowang
#define debug(...) do{\
	fprintf(stderr,"%s - %d : (%s) = ",__PRETTY_FUNCTION__,__LINE__,#__VA_ARGS__);\
	_DO(__VA_ARGS__);\
}while(0)
template<typename I> void _DO(I&&x){cerr<<x<<endl;}
template<typename I,typename...T> void _DO(I&&x,T&&...tail){cerr<<x<<", ";_DO(tail...);}
#else
#define debug(...)
#endif
template<typename T1,typename T2>
ostream& operator<<(ostream& out,pair<T1,T2> P){
	out<<'('<<P.F<<','<<P.S<<')';
	return out;
}
//}}}
const ll maxn=300005;
const ll maxlg=__lg(maxn)+2;
const ll INF64=8000000000000000000LL;
const int INF=2000000000;
const ll MOD=ll(1e9+7);
const double PI=acos(-1);
//const ll p=880301;
//const ll P=31;

ll mypow(ll a,ll b){
	ll res=1LL;
	while(b){
		if(b&1) res=res*a%MOD;
		a=a*a%MOD;
		b>>=1;
	}
	return res;
}

bool cmp(pll A,pll B){
	return A.F+A.S<B.F+B.S;
}

vector<pii> v;

vector<int> tmp;

priority_queue<int> sm;
priority_queue<int,vector<int>,greater<int>> bg;

ll a[maxn][2];

int main()
{
	IOS;
	int k,n;
	cin>>k>>n;

	ll ans=0;
	while(n--){
		string s,t;
		int a,b;
		cin>>s>>a>>t>>b;
		if(a>b) swap(a,b);
		if(s==t) ans+=b-a;
		else v.pb(MP(a,b)),ans++;
	}
	n=SZ(v);
	if(k>n) k=n;
	if(n==0){
		cout<<ans<<'\n';
		return 0;
	}

	if(k==1){
		for(pll i:v) tmp.pb(i.F),tmp.pb(i.S);
		sort(ALL(tmp));
		for(int i=0;i<n;i++) ans-=tmp[i];
		for(int i=n;i<2*n;i++) ans+=tmp[i];
		cout<<ans<<'\n';
	}
	else{
		sort(ALL(v),cmp);
		ll cur=0;
		for(int i=0;i<n;i++){
			sm.push(v[i].F);
			bg.push(v[i].S);
			cur+=v[i].S-v[i].F;
			while(bg.top()<sm.top()){
				int s=sm.top();
				int b=bg.top();
				bg.pop();sm.pop();
				sm.push(b);
				bg.push(s);
				cur+=2*s-2*b;
			}
			a[i][0]=cur;
		}
		cur=0;
		while(SZ(sm)) sm.pop();
		while(SZ(bg)) bg.pop();
		for(int i=n-1;i>=0;i--){
			sm.push(v[i].F);
			bg.push(v[i].S);
			cur+=v[i].S-v[i].F;
			while(bg.top()<sm.top()){
				int s=sm.top();
				int b=bg.top();
				bg.pop();sm.pop();
				sm.push(b);
				bg.push(s);
				cur+=2*s-2*b;
			}
			a[i][1]=cur;
		}
		ll meow=INF64;
		for(int i=0;i<n-1;i++) meow=min(meow,a[i][0]+a[i+1][1]);
		cout<<meow+ans<<'\n';
	}
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 380 KB Output is correct
2 Correct 2 ms 496 KB Output is correct
3 Correct 2 ms 496 KB Output is correct
4 Correct 3 ms 612 KB Output is correct
5 Correct 2 ms 736 KB Output is correct
6 Correct 3 ms 736 KB Output is correct
7 Correct 3 ms 884 KB Output is correct
8 Correct 3 ms 884 KB Output is correct
9 Correct 2 ms 884 KB Output is correct
10 Correct 3 ms 956 KB Output is correct
11 Correct 3 ms 1064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1064 KB Output is correct
2 Correct 2 ms 1064 KB Output is correct
3 Correct 2 ms 1064 KB Output is correct
4 Correct 2 ms 1064 KB Output is correct
5 Correct 3 ms 1064 KB Output is correct
6 Correct 3 ms 1100 KB Output is correct
7 Correct 3 ms 1100 KB Output is correct
8 Correct 2 ms 1116 KB Output is correct
9 Correct 3 ms 1156 KB Output is correct
10 Correct 3 ms 1180 KB Output is correct
11 Correct 4 ms 1328 KB Output is correct
12 Correct 41 ms 4304 KB Output is correct
13 Correct 70 ms 6640 KB Output is correct
14 Correct 54 ms 7844 KB Output is correct
15 Correct 39 ms 8184 KB Output is correct
16 Correct 46 ms 11048 KB Output is correct
17 Correct 54 ms 13244 KB Output is correct
18 Correct 60 ms 15308 KB Output is correct
19 Correct 62 ms 17576 KB Output is correct
20 Correct 57 ms 19472 KB Output is correct
21 Correct 59 ms 21472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 21472 KB Output is correct
2 Correct 2 ms 21472 KB Output is correct
3 Correct 2 ms 21472 KB Output is correct
4 Correct 3 ms 21472 KB Output is correct
5 Correct 2 ms 21472 KB Output is correct
6 Correct 2 ms 21472 KB Output is correct
7 Correct 2 ms 21472 KB Output is correct
8 Correct 2 ms 21472 KB Output is correct
9 Correct 3 ms 21472 KB Output is correct
10 Correct 2 ms 21472 KB Output is correct
11 Correct 2 ms 21472 KB Output is correct
12 Correct 2 ms 21472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 21472 KB Output is correct
2 Correct 2 ms 21472 KB Output is correct
3 Correct 2 ms 21472 KB Output is correct
4 Correct 2 ms 21472 KB Output is correct
5 Correct 2 ms 21472 KB Output is correct
6 Correct 2 ms 21472 KB Output is correct
7 Correct 2 ms 21472 KB Output is correct
8 Correct 2 ms 21472 KB Output is correct
9 Correct 2 ms 21472 KB Output is correct
10 Correct 2 ms 21472 KB Output is correct
11 Correct 2 ms 21472 KB Output is correct
12 Correct 3 ms 21472 KB Output is correct
13 Correct 3 ms 21472 KB Output is correct
14 Correct 4 ms 21472 KB Output is correct
15 Correct 3 ms 21472 KB Output is correct
16 Correct 2 ms 21472 KB Output is correct
17 Correct 2 ms 21472 KB Output is correct
18 Correct 3 ms 21472 KB Output is correct
19 Correct 3 ms 21472 KB Output is correct
20 Correct 3 ms 21472 KB Output is correct
21 Correct 2 ms 21472 KB Output is correct
22 Correct 3 ms 21472 KB Output is correct
23 Correct 3 ms 21472 KB Output is correct
24 Correct 3 ms 21472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 21472 KB Output is correct
2 Correct 2 ms 21472 KB Output is correct
3 Correct 2 ms 21472 KB Output is correct
4 Correct 2 ms 21472 KB Output is correct
5 Correct 2 ms 21472 KB Output is correct
6 Correct 2 ms 21472 KB Output is correct
7 Correct 2 ms 21472 KB Output is correct
8 Correct 2 ms 21472 KB Output is correct
9 Correct 2 ms 21472 KB Output is correct
10 Correct 2 ms 21472 KB Output is correct
11 Correct 2 ms 21472 KB Output is correct
12 Correct 2 ms 21472 KB Output is correct
13 Correct 3 ms 21472 KB Output is correct
14 Correct 3 ms 21472 KB Output is correct
15 Correct 3 ms 21472 KB Output is correct
16 Correct 3 ms 21472 KB Output is correct
17 Correct 2 ms 21472 KB Output is correct
18 Correct 3 ms 21472 KB Output is correct
19 Correct 3 ms 21472 KB Output is correct
20 Correct 3 ms 21472 KB Output is correct
21 Correct 2 ms 21472 KB Output is correct
22 Correct 5 ms 21472 KB Output is correct
23 Correct 3 ms 21472 KB Output is correct
24 Correct 3 ms 21472 KB Output is correct
25 Correct 52 ms 24144 KB Output is correct
26 Correct 93 ms 25056 KB Output is correct
27 Correct 114 ms 26932 KB Output is correct
28 Correct 153 ms 29184 KB Output is correct
29 Correct 137 ms 31632 KB Output is correct
30 Correct 60 ms 31632 KB Output is correct
31 Correct 52 ms 34440 KB Output is correct
32 Correct 110 ms 36756 KB Output is correct
33 Correct 82 ms 38680 KB Output is correct
34 Correct 115 ms 41008 KB Output is correct
35 Correct 77 ms 42972 KB Output is correct
36 Correct 117 ms 45112 KB Output is correct
37 Correct 44 ms 45792 KB Output is correct