Largest Point

Largest Point

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


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 nintegers 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
Source

 题意:在一个数组里找两个下标不一样的数,使at2i+btj最大,下标不一样,数值可能一样

 1 #include <iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<math.h>
 5 #include<algorithm>
 6 
 7 using namespace std;
 8 
 9 const int maxn = 5*1e6+7;
10 #define INF 0x3f3f3f3f
11 
12 struct node
13 {
14     int x, id;
15 }P[maxn];
16 
17 int cmp(node a, node b)
18 {
19     return abs(a.x) < abs(b.x);
20 }
21 
22 int cmp1(node a, node b)
23 {
24     return a.x < b.x;
25 }
26 
27 int main()
28 {
29     int t, n, a, b, l = 1;
30     int A1, a1, ma, B1, b1, mb;
31     scanf("%d", &t);
32     long long sum, ans;
33     while(t--)
34     {
35         scanf("%d%d%d", &n, &a, &b);
36         for(int i = 0; i < n; i++)
37         {
38             scanf("%d", &P[i].x);
39             P[i].id = i;
40         }
41         A1 = a1 = ma = B1 = b1 = mb = 0;
42         sort(P, P+n, cmp);
43         if(a >= 0)
44             A1 = P[n-1].x, a1 = P[n-1].id, ma = P[n-2].x;
45         else
46             A1 = P[0].x, a1 = P[0].id, ma = P[1].x;
47         sort(P, P+n, cmp1);
48         if(b >= 0)
49             B1 = P[n-1].x, b1 = P[n-1].id, mb = P[n-2].x;
50         else
51             B1 = P[0].x, b1 = P[0].id, mb = P[1].x;
52         if(a1 != b1)
53         {
54             sum = (long long)a * A1 *A1;
55             sum += (long long)b * B1;
56         }
57 
58         else
59         {
60             sum = (long long)a * A1*A1;
61             sum += (long long)b*mb;   // 为什么非得这么加,这么强制转换,orWA
62             ans = (long long)a*ma*ma;
63             ans += (long long)b*B1;
64             sum = sum > ans ? sum : ans;
65         }
66         printf("Case #%d: %I64d
",l++,  sum);
67     }
68     return 0;
69 }
原文地址:https://www.cnblogs.com/Tinamei/p/4831553.html