hdoj 5461 Largest Point
Largest Point
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 702 Accepted Submission(s):
293
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5461
题意:给三个数n,a,b,接下来给出n个数的数列,从数列中找到ti和tj使a*ti*ti+b*tj最大且ti不能等于tj
题解:分别考虑a和b的正负情况,如果a<0则求数组中绝对值最小的为ti如果a>0则求数组中绝对值最大的为ti,如果b<0则求数组中最小的为tj如果b>0则求数组中最大的为tj;注意求出ti后将这个位置标记
#include<stdio.h> #include<string.h> #include<algorithm> #include<math.h> #include<stack> #define INF 0x7ffffff #define MAX 5000010 #define LL long long using namespace std; int s[MAX]; int vis[MAX]; int main() { int t,k,n,m,j,i,p; int a,b; LL mina,maxb,maxa,minb,sum,M,N; scanf("%d",&t); k=0; while(t--) { scanf("%d%d%d",&m,&a,&b); memset(vis,0,sizeof(vis)); for(i=0;i<m;i++) scanf("%d",&s[i]); sum=0; mina=minb=INF;maxa=maxb=-INF; N=M=0; if(a<0) { for(i=0;i<m;i++) if(mina>abs(s[i])) { mina=abs(s[i]); p=i; } M=a*mina*mina; vis[p]=1; } else if(a>0) { for(i=0;i<m;i++) if(maxa<abs(s[i])) { maxa=abs(s[i]);p=i; } vis[p]=1; M=a*maxa*maxa; } if(b<0) { for(i=0;i<m;i++) if(minb>s[i]&&!vis[i]) minb=s[i]; N=b*minb; } else if(b>0) { for(i=0;i<m;i++) if(maxb<s[i]&&!vis[i]) maxb=s[i]; N=b*maxb; } sum=N+M; printf("Case #%d: %lld\n",++k,sum); } return 0; }