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 with integers . Given the integral coefficients and . The fact that select two elements and of and to maximize the value of , becomes the largest point.
Input
An positive integer , indicating there are test cases.
For each test case, the first line contains three integers corresponding to and . The second line contains integers where for .
The sum of for all cases would not be larger than .
For each test case, the first line contains three integers corresponding to and . The second line contains integers where for .
The sum of for all cases would not be larger than .
Output
The output contains exactly lines.
For each test case, you should output the maximum value of .
For each test case, you should output the maximum value of .
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 }*/