답안 #354302

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
354302 2021-01-21T17:18:54 Z CaroLinda Palembang Bridges (APIO15_bridge) C++14
100 / 100
347 ms 15316 KB
#include <bits/stdc++.h>

#define ll long long
#define all(x) x.begin(),x.end()
#define sz(x) (int)(x.size() )

const int MAXN = 1e5+10 ;

using namespace std ;

int N , K ;
long long ans ;

vector<ll> solve(vector< pair<int,int> >  v )
{
	multiset<int> s[2] ;
	ll soma[2] = {0,0} ;

	vector<ll> dp( sz(v) , 0 ) ;
	s[0].insert( v[0].first ) ; soma[0] = (ll)v[0].first ;
	s[1].insert( v[0].second ) ; soma[1] = (ll)v[0].second ;
	dp[0] = soma[1] - soma[0] ;

	for(int i = 1 ; i < sz(v) ; i++ )
	{
		for(int e : { v[i].first, v[i].second } )
		{			
			if( e < *s[1].begin() ) s[0].insert(e) , soma[0] += (ll)e ;
			else s[1].insert(e) , soma[1] += (ll)e ;
		}

		while( sz(s[0]) > sz(s[1] ) )
		{
			auto it = prev(s[0].end() ) ;

			s[1].insert( *it ) ;
			soma[1] += *it ;	
			soma[0] -= *it ;
			s[0].erase(it) ;
		}

		while( sz(s[1]) > sz(s[0]) )
		{
			auto it = s[1].begin() ;

			soma[1] -= *it ;
			soma[0] += *it ;
			s[0].insert(*it) ;
			s[1].erase(it) ;
		}

		dp[i] = soma[1] - soma[0] ;

	}

	return dp ;
}

int main()
{

	scanf("%d %d", &K , &N ) ;

	vector< pair<int,int> > v ;

	for(int i = 0 , l , r ; i < N ; i++ )
	{
		char c1, c2 ;
		scanf(" %c %d %c %d", &c1, &l, &c2, &r ) ;

		if( l > r ) swap(l,r) ;

		if(c1 == c2 ) ans += (ll)(r-l);
		else v.push_back(make_pair(l,r) ) , ans++ ;
	}

	if( sz(v) == 0 )
	{
		printf("%lld\n", ans ) ;
		return 0 ;
	}

	sort(all(v) , [&](pair<int,int> a, pair<int,int> b ) { return a.first + a.second < b.first + b.second ; } ) ;

	vector<ll> dpPref = solve(v) ;
	reverse(all(v) ) ;
	vector<ll> dpSuf = solve(v) ;
	reverse(all(dpSuf) ) ;

	if(K == 1 || sz(dpSuf) <= 1 ) 
	{
		printf("%lld\n", ans + dpSuf[0] ) ;
		return 0 ;
	}

	ll mn = dpPref[0] + dpSuf[1] ;

	for(int i = 1 ; i < sz(dpPref)-1 ; i++ ) mn = min(mn, dpPref[i] + dpSuf[i+1] ) ;

	printf("%lld\n", ans + mn ) ;

}

Compilation message

bridge.cpp: In function 'int main()':
bridge.cpp:62:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   62 |  scanf("%d %d", &K , &N ) ;
      |  ~~~~~^~~~~~~~~~~~~~~~~~~
bridge.cpp:69:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   69 |   scanf(" %c %d %c %d", &c1, &l, &c2, &r ) ;
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 2 ms 492 KB Output is correct
4 Correct 2 ms 492 KB Output is correct
5 Correct 2 ms 512 KB Output is correct
6 Correct 2 ms 492 KB Output is correct
7 Correct 2 ms 492 KB Output is correct
8 Correct 2 ms 492 KB Output is correct
9 Correct 2 ms 492 KB Output is correct
10 Correct 2 ms 492 KB Output is correct
11 Correct 2 ms 492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 2 ms 492 KB Output is correct
4 Correct 2 ms 492 KB Output is correct
5 Correct 2 ms 492 KB Output is correct
6 Correct 1 ms 492 KB Output is correct
7 Correct 2 ms 492 KB Output is correct
8 Correct 2 ms 492 KB Output is correct
9 Correct 2 ms 492 KB Output is correct
10 Correct 2 ms 492 KB Output is correct
11 Correct 2 ms 492 KB Output is correct
12 Correct 142 ms 13648 KB Output is correct
13 Correct 311 ms 15312 KB Output is correct
14 Correct 258 ms 12832 KB Output is correct
15 Correct 134 ms 9092 KB Output is correct
16 Correct 126 ms 14672 KB Output is correct
17 Correct 161 ms 15312 KB Output is correct
18 Correct 131 ms 14832 KB Output is correct
19 Correct 162 ms 15204 KB Output is correct
20 Correct 178 ms 14800 KB Output is correct
21 Correct 157 ms 14928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 372 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 2 ms 492 KB Output is correct
14 Correct 2 ms 492 KB Output is correct
15 Correct 2 ms 492 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 2 ms 492 KB Output is correct
20 Correct 2 ms 492 KB Output is correct
21 Correct 2 ms 492 KB Output is correct
22 Correct 2 ms 492 KB Output is correct
23 Correct 2 ms 492 KB Output is correct
24 Correct 2 ms 492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 2 ms 492 KB Output is correct
14 Correct 2 ms 492 KB Output is correct
15 Correct 2 ms 492 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 2 ms 492 KB Output is correct
20 Correct 2 ms 492 KB Output is correct
21 Correct 2 ms 492 KB Output is correct
22 Correct 2 ms 492 KB Output is correct
23 Correct 3 ms 492 KB Output is correct
24 Correct 2 ms 640 KB Output is correct
25 Correct 151 ms 13776 KB Output is correct
26 Correct 225 ms 13776 KB Output is correct
27 Correct 347 ms 14540 KB Output is correct
28 Correct 331 ms 15316 KB Output is correct
29 Correct 328 ms 15184 KB Output is correct
30 Correct 125 ms 8136 KB Output is correct
31 Correct 128 ms 14544 KB Output is correct
32 Correct 163 ms 15184 KB Output is correct
33 Correct 140 ms 14820 KB Output is correct
34 Correct 163 ms 15184 KB Output is correct
35 Correct 152 ms 14800 KB Output is correct
36 Correct 157 ms 14928 KB Output is correct
37 Correct 151 ms 13672 KB Output is correct