hdu5461 Largest Point(沈阳网赛)

Largest Point

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


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
题解:
ax2和bx分开考虑;
代码:
 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<math.h>
 4 #define MAX(x,y)(x>y?x:y)
 5 #define F for(int i=0;i<n;i++)
 6 const int MAXN=5000010;
 7 const int INF=0x3f3f3f3f;
 8 int m[MAXN],vis[MAXN];
 9 int main(){
10     int T;
11     int n,a,b,flot=0;
12     scanf("%d",&T);
13     while(T--){
14         memset(vis,0,sizeof(vis));
15         scanf("%d%d%d",&n,&a,&b);
16         long long sum=0,x,k;//xΪlong long 
17         F scanf("%d",m+i);
18         if(a>0){
19             x=-INF;
20             F if(x<fabs(m[i]))x=fabs(m[i]),k=i;
21             vis[k]=1;
22             sum+=a*x*x;
23         }
24         else if(a<0){
25             x=INF;
26                 F if(x>fabs(m[i]))x=fabs(m[i]),k=i;
27                 vis[k]=1;
28             sum+=a*x*x;
29         }
30         if(b>0){
31              x=-INF;
32             F if(x<m[i]&&!vis[i])x=m[i],k=i;
33             vis[k]=1;
34             sum+=b*x;
35         }
36         else if(b<0){
37              x=INF;
38                 F if(x>m[i]&&!vis[i])x=m[i],k=i;
39                 vis[k]=1;
40             sum+=b*x;
41         }
42         printf("Case #%d: %lld\n",++flot,sum);
43     }
44     return 0;
45 }
46 /*#include<stdio.h>
47 #include<algorithm>
48 #include<math.h>
49 #define MAX(x,y)(x>y?x:y)
50 #define js(x,y)(a*x*x+b*y)
51 using namespace std;
52 const int MAXN=5000010;
53 const int INF=0x3f3f3f3f;
54 int m[MAXN],ml[MAXN];
55 int main(){
56     int T,a,b,n,t[5],ans;
57     scanf("%d",&T);
58     for(int i=1;i<=T;i++){
59         scanf("%d%d%d",&n,&a,&b);
60         for(int j=0;j<n;j++)scanf("%d",m+j),ml[j]=fabs(m[j]);
61             t[0]=*max_element(m,m+n);t[1]=*min_element(m,m+n);
62             *max_element(m,m+n)=-INF;
63             t[2]=*max_element(m,m+n);
64             *min_element(m,m+n)=INF;
65             *min_element(m,m+n)=INF;
66             t[3]=*min_element(m,m+n);
67             ans=-INF;
68             printf("%d %d %d %d\n",t[0],t[1],t[2],t[3]);
69             if(a>=0&&b>=0){
70                 if(fabs(t[1])>=fabs(t[0]))
71                     ans=js(t[1],t[0]);
72                 else
73                     ans=MAX(js(t[0],t[2]),js(t[2],t[0]));
74             }
75             else if(a>=0&&b<0){
76                 if(fabs(t[0])>=fabs(t[1]))
77                     ans=js(t[0],t[1]);
78                 else
79                     ans=MAX(js(t[1],t[3]),js(t[3],t[1]));
80             }
81             else{
82                 int x=*min_element(ml,ml+n);
83                 
84             }
85             printf("Case #%d: %d\n",i,ans);
86     }
87     return 0;
88 }*/

 

posted @ 2015-09-24 12:59  handsomecui  阅读(165)  评论(0编辑  收藏  举报