Bracket Score ( AtCoder Grand Contest 048)

题解:贪心策略,一个偶数必然要匹配一个奇数,不然中间相差的个数不会构成一个完美括号匹配,初始对b数组进行求和sum,再次对奇数项、偶数项分别对a[i] - b[i]从大到小排序,然后两者都对sum相加(去除b数组中小值的影响) , 每次操作取最大值。

 

code:

/*
	time 2020/10/24
	author by Jade
*/

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int NMAX = 2e5 + 10;
const int MOD = 1e9 + 7;
int c[NMAX] , a[NMAX] , b[NMAX];
int main(int argc, char const *argv[])
{
	int n , val,a_ind =0, b_ind = 0;
	scanf("%d",&n);
	for(register int i = 1;i <= n;i++)	scanf("%d",c+i);
	ll ans = 0 , sum = 0;
	for(register int i = 1;i <= n;i++)
	{
		scanf("%d",&val);
		if(i & 1)
			a[++a_ind] = c[i] - val;
		else
			b[++b_ind] = c[i] - val;
		sum += val;
	}	

	ans = sum;
	sort(a + 1,a + a_ind + 1 , greater<int>());
	sort(b + 1,b + b_ind + 1 , greater<int>());
	for(register int i = 1;i <= a_ind;i++)
	{
		sum += a[i] + b[i];
		ans = max(ans , sum);
	}
	printf("%lld\n",ans );
	return 0;
}
posted @ 2020-10-24 10:02  jadelemon  阅读(191)  评论(0编辑  收藏  举报