답안 #487891

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
487891 2021-11-16T21:57:07 Z MilosMilutinovic Palembang Bridges (APIO15_bridge) C++14
100 / 100
230 ms 22876 KB
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
mt19937 mrand(random_device{}()); 
const ll mod=1000000007;
const ll mod2=998244353;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head

const int N=101000;
int k,n,m,s[N],t[N],a[N],b[N],p[N];
char ss[N],tt[N];
ll pf[N],sf[N];

struct ds{
	int med;
	ll sm;
	ds() { sm=0; }
	multiset<int> L,R;
	void add(int x,int y) {
		if (x>y) swap(x,y);
		if (SZ(L)==0) {
			sm=y-x;
			L.insert(x);
			R.insert(y);
			med=x;
			return;
		}
		(x<=med?L:R).insert(x);
		(y<=med?L:R).insert(y);
		while (SZ(L)>SZ(R)) {
			R.insert(*prev(L.end()));
			L.erase(prev(L.end()));
		}
		while (SZ(L)<SZ(R)) {
			L.insert(*R.begin());
			R.erase(R.begin());
		}
		int nm=*prev(L.end());
		sm+=abs(nm-x)+abs(nm-y);
		if (med>nm) sm+=abs(med-nm)*2;
		med=nm;
	}
};

int main() {
	scanf("%d%d",&k,&n);
	rep(i,1,n+1) scanf("\n%c %d %c %d",ss+i,s+i,tt+i,t+i);
	ll ans=1e18,same=0;
	rep(i,1,n+1) {
		if (ss[i]==tt[i]) {
			same+=abs(s[i]-t[i]);
		}
		else a[++m]=s[i],b[m]=t[i];
	}
	if (m==0) {
		printf("%lld\n",same);
		return 0;
	}
	rep(i,1,m+1) p[i]=i;
	sort(p+1,p+1+m,[&](int i,int j) {
		return a[i]+b[i]<a[j]+b[j];
	});
	ds pref;
	pref.sm=0;
	rep(i,1,m+1) {
		pref.add(a[p[i]],b[p[i]]);
		pf[i]=pref.sm;
	}
	ds suff;
	suff.sm=0;
	per(i,1,m+1) {
		suff.add(a[p[i]],b[p[i]]);
		sf[i]=suff.sm;
	}
	/*rep(i,1,m+1) {
		printf("%lld %lld\n",pf[i],sf[i]);
	}*/
	rep(i,1,m+1) ans=min(ans,pf[i]+sf[i+1]);
	if (k==2) printf("%lld",ans+same+m);
	else printf("%lld\n",pf[m]+same+m);
}

/*
2 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7

1 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7
*/

Compilation message

bridge.cpp: In function 'int main()':
bridge.cpp:60:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   60 |  scanf("%d%d",&k,&n);
      |  ~~~~~^~~~~~~~~~~~~~
bridge.cpp:61:20: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   61 |  rep(i,1,n+1) scanf("\n%c %d %c %d",ss+i,s+i,tt+i,t+i);
      |               ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 460 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 1 ms 460 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 2 ms 460 KB Output is correct
10 Correct 1 ms 460 KB Output is correct
11 Correct 1 ms 460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 460 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 1 ms 460 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 1 ms 460 KB Output is correct
11 Correct 1 ms 460 KB Output is correct
12 Correct 140 ms 22724 KB Output is correct
13 Correct 211 ms 22788 KB Output is correct
14 Correct 186 ms 20504 KB Output is correct
15 Correct 105 ms 13608 KB Output is correct
16 Correct 112 ms 22816 KB Output is correct
17 Correct 147 ms 22804 KB Output is correct
18 Correct 100 ms 22704 KB Output is correct
19 Correct 153 ms 22700 KB Output is correct
20 Correct 148 ms 22828 KB Output is correct
21 Correct 153 ms 22796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 1 ms 460 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
15 Correct 1 ms 460 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 460 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 448 KB Output is correct
20 Correct 1 ms 460 KB Output is correct
21 Correct 1 ms 460 KB Output is correct
22 Correct 1 ms 460 KB Output is correct
23 Correct 1 ms 460 KB Output is correct
24 Correct 1 ms 460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 1 ms 460 KB Output is correct
14 Correct 2 ms 460 KB Output is correct
15 Correct 1 ms 460 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 460 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 460 KB Output is correct
20 Correct 2 ms 460 KB Output is correct
21 Correct 1 ms 460 KB Output is correct
22 Correct 1 ms 460 KB Output is correct
23 Correct 1 ms 460 KB Output is correct
24 Correct 1 ms 460 KB Output is correct
25 Correct 133 ms 22736 KB Output is correct
26 Correct 179 ms 22728 KB Output is correct
27 Correct 205 ms 22772 KB Output is correct
28 Correct 228 ms 22720 KB Output is correct
29 Correct 230 ms 22876 KB Output is correct
30 Correct 89 ms 12180 KB Output is correct
31 Correct 120 ms 22804 KB Output is correct
32 Correct 143 ms 22788 KB Output is correct
33 Correct 108 ms 22808 KB Output is correct
34 Correct 159 ms 22796 KB Output is correct
35 Correct 152 ms 22788 KB Output is correct
36 Correct 153 ms 22736 KB Output is correct
37 Correct 150 ms 22712 KB Output is correct