zoukankan      html  css  js  c++  java
  • 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
    ",ans );
    	return 0;
    }
  • 相关阅读:
    PHP
    PHP
    密码修改机制
    PHP
    PHP
    PHP
    PHP
    Java并发编程:进程和线程的由来(转)
    Java获取文件大小的正确方法(转)
    J2EE开发中常用的缓存策略
  • 原文地址:https://www.cnblogs.com/lemon-jade/p/13867710.html
Copyright © 2011-2022 走看看