题目;http://acm.hdu.edu.cn/showproblem.php?pid=5461
题意就是在数组中找出a*t[i]*t[i]+b*t[j]的最大值,特别注意的是这里i和i不能相等,想不到的是居然直接暴力排序都能过,而且这题的数据很水,好多有bug的代码都能过
但是我们要对自己要求严一点,尽量吧各种情况考虑到位,如果下面的代码还有不妥,欢迎指正
1 #include<cstdio> 2 #include<cmath> 3 #include<algorithm> 4 using namespace std; 5 typedef long long ll; 6 ll max(ll x,ll y) { 7 if (x>y) return x; 8 else return y; 9 } 10 ll a[5000001]; 11 int main() 12 { 13 ll t,q,i,n,c,w,b,num1,num2; 14 while (~scanf("%I64d",&t)) 15 { 16 ll ans=1; 17 while (t--){ 18 scanf("%I64d %I64d %I64d",&n,&c,&b); 19 q=100000000; 20 for (i=1;i<=n;i++) 21 scanf("%I64d",&a[i]); 22 sort(a+1,a+n+1); 23 for (i=1;i<=n;i++) 24 if (fabs(a[i])<q) q=fabs(a[i]),w=i; 25 printf("Case #%I64d: ",ans++); 26 if (c<0&&b<0) 27 { 28 if (w!=1) printf("%I64d ",c*a[w]*a[w]+b*a[1]); 29 else 30 { 31 num1=c*a[1]*a[1]+b*a[2]; 32 num2=c*a[2]*a[2]+b*a[1]; 33 printf("%d ",max(num1,num2)); 34 } 35 continue; 36 } 37 else if (c>0&&b>0) 38 { 39 printf ("%I64d ",max(max(c*a[n]*a[n]+b*a[n-1], c*a[n-1]*a[n-1]+b*a[n]), c*a[1]*a[1]+b*a[n])); 40 continue; 41 } 42 else if (c>=0&&b<=0) 43 { 44 printf ("%I64d ",max(max(c*a[n]*a[n]+b*a[1], c*a[1]*a[1]+b*a[2]), c*a[2]*a[2]+b*a[1])); 45 continue; 46 } 47 else if (c<=0&&b>=0) 48 { 49 if (w!=n) printf("%I64d ",c*a[w]*a[w]+b*a[n]); 50 else 51 { 52 num1=c*a[n]*a[n]+b*a[n-1]; 53 num2=c*a[n-1]*a[n-1]+b*a[n]; 54 printf("%d ",max(num1,num2)); 55 } 56 continue; 57 } 58 } 59 } 60 return 0; 61 }