原题链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5461
小技巧:
把ti计算成ti*ti的形式。
代码如下:
#include<iostream>
#include<cstdio>
#include<utility>
#include<algorithm>
using namespace std;
typedef long long LL;
typedef pair<LL,int> PLL;//first存数值,second存id
PLL x[5],y[5];
bool cmp1(const PLL &x,const PLL &y) {return x.first>y.first;}
bool cmp2(const PLL &x,const PLL &y) {return x.first<y.first;}
int main()
{
int T;
scanf("%d",&T);
int n,a,b;
LL z;
for(int k=1;k<=T;k++)
{
scanf("%d%d%d",&n,&a,&b);
for(int i=0;i<n;i++)
{
scanf("%lld",&z);
x[4]=make_pair(z*z,i);
y[4]=make_pair(z,i);
if(i<=4)
{
x[i]=x[4];
y[i]=y[4];
}else
{
if(a>0)//对前五个值排序
sort(x,x+5,cmp1);
else sort(x,x+5,cmp2);
if(b>0)
sort(y,y+5,cmp1);
else sort(y,y+5,cmp2);
}
}
LL ans=-1e20;//ans范围较大
for(int i=0;i<min(n,3);i++)
for(int j=0;j<min(n,3);j++)
if(x[i].second!=y[j].second)
ans=max(ans,x[i].first*a+b*y[j].first);
printf("Case #%d: %lld\n",k,ans);
}
return 0;
}
本文提供了一道来自HDU在线评测系统的编程题(PID 5461)的解决方案。该题要求从输入的一系列整数中选出两个数,一个用于平方后乘以系数a,另一个直接乘以系数b,使得最终结果最大。文章通过使用优先队列维护前五大的平方数和原数值,并进行组合比较以得到最优解。

902

被折叠的 条评论
为什么被折叠?



