题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5461
题意:求a*c[i]*c[i]+b*c[j]的最大值;
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
先贴一个还不明白该怎么改的代码:
1 #include<iostream> 2 #include<algorithm> 3 #include<cstdio> 4 #include<cstring> 5 #include<queue> 6 #include<stdlib.h> 7 #include<map> 8 #include<cmath> 9 10 using namespace std; 11 12 #define N 500010 13 #define INF 0x3f3f3f3f 14 15 long long sum[N]; 16 17 struct node 18 { 19 long long s,o; 20 }p[N]; 21 22 bool cmp1(node a,node b) 23 { 24 return a.s<b.s; 25 } 26 27 bool cmp2(node a,node b) 28 { 29 return a.o<b.o; 30 } 31 32 int main() 33 { 34 int T,k=1,i,j; 35 long long n,a,b,x,y,minn,minn2,maxx,maxx2,minabs,maxabs; 36 37 scanf("%d", &T); 38 39 while(T--) 40 { 41 scanf("%lld %lld %lld", &n,&a,&b); 42 memset(sum,-INF,sizeof(sum)); 43 44 for(i=0;i<n;i++) 45 { 46 scanf("%lld", &p[i].s); 47 p[i].o=abs(p[i].s); 48 } 49 50 sort(p,p+n,cmp1);///数从小到大排序 51 minn=p[0].s; 52 minn2=p[1].s; 53 maxx=p[n-1].s; 54 maxx2=p[n-2].s; 55 sort(p,p+n,cmp2);///绝对值排序 56 minabs=p[0].s; 57 maxabs=p[n-1].s; 58 59 i=0; 60 if(a>=0) 61 { 62 x=maxabs; 63 if(b>=0) 64 { 65 if(maxx==x) 66 { 67 y=maxx2; 68 sum[i++]=max(a*x*x+b*y,a*y*y+b*x); 69 } 70 else 71 { 72 y=maxx; 73 sum[i++]=a*x*x+b*y; 74 } 75 76 } 77 else 78 { 79 if(minn==x) 80 { 81 y=minn2; 82 sum[i++]=max(a*x*x+b*y,a*y*y+b*x); 83 } 84 else 85 { 86 y=minn; 87 sum[i++]=a*x*x+b*y; 88 } 89 90 } 91 } 92 else 93 { 94 x=minabs; 95 if(b>=0) 96 { 97 if(maxx==x) 98 { 99 y=maxx2; 100 sum[i++]=max(a*x*x+b*y,a*y*y+b*x); 101 } 102 else 103 { 104 y=maxx; 105 sum[i++]=a*x*x+b*y; 106 } 107 108 } 109 else 110 { 111 if(minn==x) 112 { 113 y=minn2; 114 sum[i++]=max(a*x*x+b*y,a*y*y+b*x); 115 } 116 else 117 { 118 y=minn; 119 sum[i++]=a*x*x+b*y; 120 } 121 122 } 123 } 124 125 long long maxsum=-INF; 126 for(j=0;j<i;j++) 127 maxsum=max(maxsum,sum[j]); 128 129 printf("Case #%d: %lld ",k++,maxsum); 130 } 131 return 0; 132 } 133 /*HDU 5461*/
AC代码:直接暴力
1 #include<iostream> 2 #include<algorithm> 3 #include<cstdio> 4 #include<cstring> 5 #include<queue> 6 #include<stdlib.h> 7 #include<map> 8 #include<cmath> 9 10 using namespace std; 11 12 #define N 5100000 13 #define INF 0x3f3f3f3f 14 15 long long c[N]; 16 17 int main() 18 { 19 int T,k=1,i; 20 long long n,a,b,x,y,minn,minn2,maxx,maxx2,maxsum,sum; 21 22 scanf("%d", &T); 23 24 while(T--) 25 { 26 scanf("%lld %lld %lld", &n,&a,&b); 27 x=INF; 28 29 for(i=0;i<n;i++) 30 scanf("%lld", &c[i]); 31 32 sort(c,c+n); 33 34 for(i=0;i<n;i++) 35 { 36 if(c[i]<=0&&c[i+1]>=0) 37 x=min(-c[i],c[i+1]); 38 ///对于全体绝对值排序找最小的情况来说,这样会更方便,反正其他的值也用不上 39 } 40 41 minn=c[0]; 42 minn2=c[1]; 43 maxx=c[n-1]; 44 maxx2=c[n-2]; 45 46 47 if(a<0&&b<0) 48 { 49 y=minn; 50 maxsum=a*x*x+b*y; 51 } 52 else if(a<0&&b>0) 53 { 54 y=maxx; 55 maxsum=a*x*x+b*y; 56 } 57 else if(a>0&&b>0) 58 { 59 x=maxx; 60 y=maxx2; 61 sum=max(a*x*x+b*y,a*y*y+b*x); 62 maxsum=max(sum,a*minn*minn+b*maxx); 63 } 64 else if(a>0&&b<0) 65 { 66 x=minn; 67 y=minn2; 68 sum=max(a*maxx*maxx+b*minn, a*x*x+b*y); 69 maxsum=max(sum,a*y*y+b*x); 70 } 71 else 72 maxsum=0; 73 74 printf("Case #%d: %lld ",k++,maxsum); 75 } 76 return 0; 77 } 78 /*HDU 5461*/