您的位置:首页 > 理论基础 > 计算机网络

HDU5461 沈阳网络赛题

2016-03-30 15:09 253 查看
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5461

  题目的意识是给你n个数以及a, b,从n个数中选两个数使得a*t1^2 + b*t2最大, 思路很简单。 见代码解释:

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
typedef long long LL;
int n, a, b;

struct node{
LL x; int idx;
bool operator<(const node&r) const{
return x < r.x;
}
}n1[1000000 + 100], n2[1000000 + 100];

int main() {
int T;
scanf("%d", &T);
int kase = 0;
while(T--) {
scanf("%d%d%d", &n, &a, &b);
for(int i=0; i<n; i++) {
int t;
scanf("%d", &t);
n1[i] = (node){(LL)a*(LL)t*(LL)t, i}; //每个数的平方*a
n2[i] = (node){(LL)b*(LL)t, i};       //每个数*b
}
sort(n1, n1+n);     //将每个数的权从小到大排序
sort(n2, n2+n);
if(n1[n-1].idx != n2[n-1].idx) {    //如果最大的两个数的下标不一样
printf("Case #%d: %lld\n", ++kase, n1[n-1].x+n2[n-1].x);
}else{
LL res = max(n1[n-1].x+n2[n-2].x, n1[n-2].x+n2[n-1].x);
printf("Case #%d: %lld\n", ++kase, res);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: