题目连接: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 ", ++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 ", ++kase, res); } } return 0; }