Largest Point(2015年吉林网络赛)

来源:互联网 发布:mac电脑pdf转换成ppt 编辑:程序博客网 时间:2024/04/29 15:10

思路:

所求多项式由两项相加

贪心思路:

每项尽可能大


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

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

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

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

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

#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);}}


0 0
原创粉丝点击