网络赛水题,主要是对t[i]平方,和t[i]分别排序,,然后先根据b的正负判断b该与哪个数相乘,然后在给a找数乘。
#pragma warning(disable:4996) #include <cmath> #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <vector> using namespace std; const int N = 6000005; typedef long long LL; LL t[N]; int n; LL a, b; vector<LL>vec; int main(){ int T; scanf("%d", &T); int kase = 1; while (T--){ scanf("%d %lld %lld", &n, &a, &b); vec.clear(); for (int i = 1; i <= n; i++){ int x; scanf("%d", &x); t[i] = (LL)x; vec.push_back(t[i] * t[i]); } sort(t + 1, t + 1 + n); sort(vec.begin(), vec.end()); LL ans = 0; if (a > 0 && b > 0){ if (vec[vec.size() - 1] == t[n] * t[n]) { ans = vec[vec.size() - 2] * a + b*t[n]; } else{ ans = vec[vec.size() - 1] * a + b*t[n]; } ans = max(ans, a*t[n] * t[n] + b*t[n - 1]); } else if (a > 0 && b < 0) { if (vec[vec.size() - 1] == t[1] * t[1]) { ans = vec[vec.size() - 2] * a + b*t[1]; } else{ ans = vec[vec.size() - 1] * a + b*t[1]; } ans = max(ans, a*t[1] * t[1] + b*t[2]); } else if (a<0 && b>0) { if (vec[0] == t[n] * t[n]){ ans = vec[1] * a + b*t[n]; } else{ ans = vec[0] * a + b*t[n]; } ans = max(ans, a*t[n] * t[n] + b*t[n - 1]); } else if (a < 0 && b < 0){ if (vec[0] == t[1] * t[1]){ ans = vec[1] * a + b*t[1]; } else{ ans = vec[0] * a + b*t[1]; } ans = max(ans, a*t[1] * t[1] + b*t[2]); } else if (a == 0) { if (b >= 0)ans = b*t[n]; else ans = b*t[1]; } else{ if (a > 0)ans = vec[vec.size() - 1] * a; else ans = vec[0] * a; } printf("Case #%d: %lld\n", kase++, ans); } return 0; }