Largest Point(2015年吉林网络赛)

本文探讨了如何使用贪心策略来解决最大化多项式求和的问题。通过统计集合元素的最大值和次大值,并标记所取元素的位置,算法能够有效地在重合情况下替换较小的值,实现最优解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

思路:

所求多项式由两项相加

贪心思路:

每项尽可能大


扫描集合元素统计其最大值

可能会出现两项取相同的数时值最大

所以需要统计次大值和所取元素的位置标记

重合时替换其中一个为次大值

替换方案有两种,取较大的即可

#include <algorithm>
#include <stdio.h>
#include <math.h>
#define LL long long
#define MIN (-1e18-1)

using namespace std;

struct node
{
    int key;
    LL v;
};

int main()
{
	int T;
	scanf("%d", &T);
	for(int j = 1; j <= T; j++)
	{
	    node res1a, res1b, res2a, res2b;
	    res1a.v = MIN;
	    res1b.v = MIN;
	    res2a.v = MIN;
	    res2b.v = MIN;
	    LL node_num, a, b;
		scanf("%lld %lld %lld", &node_num, &a, &b);
		for (int i = 0; i < node_num; ++i)
		{
			LL temp, temp2, temp_res1, temp_res2;
			scanf("%lld", &temp);
			temp2 = temp*temp;
			temp_res1 = a*temp2;
			temp_res2 = b*temp;
			if(temp_res1 > res1a.v) // 统计二次项最大值
            {
                res1b.v = res1a.v;
                res1a.v = temp_res1;
                res1a.key = i;
            }
            else if(temp_res1 > res1b.v) // 统计二次项次大值
            {
                res1b.v = temp_res1;
                res1b.key = i;
            }
            if(temp_res2 > res2a.v) // 统计一次项最大值
            {
                res2b.v = res2a.v;
                res2a.v = temp_res2;
                res2a.key = i;
            }
            else if(temp_res2 > res2b.v) // // 统计一次项次大值
            {
                res2b.v = temp_res2;
                res2b.key = i;
            }
		}
		LL res;
		if(res1a.key!=res2a.key) // 判重
            res = res1a.v+res2a.v;
        else
        {
            res = res1a.v+res2b.v;
            res = max(res, res1b.v+res2a.v);
        }
		printf("Case #%d: %lld\n",j, res);
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值