See the Code of the Great God. Understand for a long time ... It's really a gap.
Test instructions: Given m, p, a, B, then XI satisfies the two formula, and the maximum value of xp1 + xp2 +...+ xPM is obtained.
1, -1/sqrt (a) <= XI <= sqrt (a); (a>0)
2, X1+X2+...+XM = B*sqrt (a);
Note: P is even.
Problem solving: Because P is even, the P-sqrt (a) and -1/sqrt (a) are positive and sqrt (a) > 1/sqrt (a). So greedy thought when try to take sqrt (a); When the sum of the XI is already taken and greater than B*SQRT (a), then fetch ( -1/ sqrt (a)) offset the extra sqrt (a).
To simplify the calculation and reduce the error caused by sqrt (). The 2 can now be multiplied by sqrt (a).
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5 using namespacestd;6 intMain () {7 intm, p, a, B;8 while(~SCANF ("%d%d%d%d", &m, &p, &a, &b)) {9 intL =0, r =0;//L, R represents the number of -1/sqrt (a) and sqrt (a), respectivelyTen intsum = A *b; One for(inti =1; I < m; i++) {//Note that the last number is not considered A if(Sum >=a) { -r++; -Sum-= A;//after Xi has taken sqrt (a), sum remembers to update the } - Else { -l++; -sum++; + } - } + DoubleAns = L/pow (sqrt (a), p) + R *Pow (sqrt (a), p); AAns + = POW (sum/sqrt (a), p);//Remember to remove the sqrt (a); atprintf"%d\n", (int) (ans +0.5)); - } - return 0; -}
"Reasoning, greedy" UVa 1319-maximum