标签:
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 1485 Accepted Submission(s): 588
#include<stdio.h> #include<iostream> #include<string.h> #include <stdlib.h> #include<math.h> #include<algorithm> using namespace std; typedef long long LL; const LL INF = 1<<60; int main(){ int tcase; scanf("%d",&tcase); int t =1; while(tcase--){ int n,a,b; scanf("%d%d%d",&n,&a,&b); LL ans; if(a>=0&&b>=0){ LL fmax = -INF,smax = -INF; for(int i=1;i<=n;i++){ LL num; scanf("%lld",&num); if(num>fmax){ smax = fmax; fmax = num; }else{ smax = max(smax,num); } } ans = max(a*fmax*fmax+b*smax,a*smax*smax+b*fmax); }else if(a>0&&b<0){ LL MAX=-INF,SMAX=-INF; ///平方的最大和次大 LL MIN=INF,SMIN=INF; ///最小和次小 int id1,id2; for(int i=1;i<=n;i++){ LL num; scanf("%lld",&num); LL temp = num*num; if(num<MIN){ SMIN = MIN; MIN = num; id1 = i; }else SMIN = min(SMIN,num); if(temp>MAX){ SMAX = MAX; MAX = temp; id2 = i; }else SMAX = max(SMAX,num); } if(id1!=id2){ ans = a*MAX+b*MIN; }else{ ans = max(a*SMAX+b*MIN,a*MAX+b*SMIN); } }else if(a<0&&b>0){ LL MAX = -INF,MIN = INF; ///注意这里的最小是指平方的最小 LL smin=INF,smax = -INF; ///此处次小是指平方的次小 int id1,id2; ///分别记录ti 和 tj 的位置 for(int i=1;i<=n;i++){ LL num; scanf("%lld",&num); if(num>MAX) { smax = MAX; MAX = num; id1 = i; }else smax = max(smax,num); LL temp = num*num; if(temp<MIN){ smin = MIN; MIN = temp; id2 = i; }else smin = min(smin,temp); } if(id1!=id2){ ans = a*MIN+b*MAX; } else{ ans = max(a*MIN+b*smax,a*smin+b*MAX); } }else { LL MIN = INF,SMIN = INF; ///这两个值代表绝对值次小和最小的平方 LL MIN1 = INF,SMIN1 = INF; ///这两个值代表次小和最小 int id1,id2; for(int i=1;i<=n;i++){ LL num; scanf("%lld",&num); LL temp = num*num; if(num<MIN1) { SMIN1 = MIN1; MIN1 = num; id1 = i; }else SMIN1 = min(SMIN1,num); if(temp<MIN){ SMIN = MIN; MIN = temp; id2 = i; }else SMIN = min(SMIN,temp); } if(id1!=id2){ ans = a*MIN+b*MIN1; }else{ ans = max(a*MIN+b*SMIN1,a*SMIN+b*MIN1); } } printf("Case #%d: %lld\n",t++,ans); } return 0; }
标签:
原文地址:http://www.cnblogs.com/liyinggang/p/5565513.html