2015 ACM/ICPC Asia Regional Shenyang Online-1012 Largest Point

kewlgrl發表於2015-09-19

Largest Point

Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 0    Accepted Submission(s): 0


Problem Description
Given the sequence A with n integers t1,t2,,tn. Given the integral coefficients a and b. The fact that select two elements ti and tj of A and ij to maximize the value of at2i+btj, becomes the largest point.
 

Input
An positive integer T, indicating there are T test cases.
For each test case, the first line contains three integers corresponding to n (2n5×106), a (0|a|106) and b (0|b|106). The second line contains n integers t1,t2,,tn where 0|ti|106 for 1in.

The sum of n for all cases would not be larger than 5×106.
 

Output
The output contains exactly T lines.
For each test case, you should output the maximum value of at2i+btj.
 

Sample Input
2 3 2 1 1 2 3 5 -1 0 -3 -3 0 3 3
 

Sample Output
Case #1: 20 Case #2: 0
 
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
const long long N=5000010;
int num[N];
typedef struct AA
{
    long long f;//儲存ati^2
    long long Count;//儲存編號
} A;
A s[N];
typedef struct BB
{
    long long ff;//儲存btj
    long long ccount;//儲存編號
} B;
B ss[N];
int cmp(const void* x1,const void* y1)//ati^2降序
{
    A* x=(A*)x1;
    A* y=(A*)y1;
    long long m=y->f-x->f;
    return m;
}
int com(const void* x1,const void* y1)//btj降序
{
    B* x=(B*)x1;
    B* y=(B*)y1;
    long long m=y->ff-x->ff;
    return m;
}
int main()
{
    long long c,n,a,b,i,cnt=1;
    long long sum;
    scanf("%d",&c);
    while(c--)
    {
        sum=0;
        scanf("%d%d%d",&n,&a,&b);
        for(i=0; i<n; ++i)//輸入處理儲存資料
        {
            scanf("%d",&num[i]);
            s[i].f=a*num[i]*num[i];
            ss[i].ff=b*num[i];
            s[i].Count=i;
            ss[i].ccount=i;
        }
        qsort(s,n,sizeof(AA),cmp);//排序
        qsort(ss,n,sizeof(BB),com);
        if(s[0].Count==ss[0].ccount)//如果最大的兩個數編號相同
        {
            if((s[0].f+ss[1].ff)>(s[1].f+ss[0].ff))
                sum=s[0].f+ss[1].ff;
            else
                sum=s[1].f+ss[0].ff;
        }
        else
            sum=s[0].f+ss[0].ff;
        printf("Case #%d: %I64d\n",cnt++,sum);
    }
    return 0;
}

嗯,所有資料都要用long long。。

相關文章