UVAlive 2911 Maximum(贪心)

 

Let x1x2,..., xm be real numbers satisfying the following conditions:

 

a)
-xi ;
b)
x1 + x2 +...+ xm = b *  for some integers  a and  b  (a > 0).

Determine the maximum value of xp1 + xp2 +...+ xpm for some even positive integer p.

 

Input 

Each input line contains four integers: mpab ( m2000, p12p is even). Input is correct, i.e. for each input numbers there exists x1x2,..., xm satisfying the given conditions.

 

Output 

For each input line print one number - the maximum value of expression, given above. The answer must be rounded to the nearest integer.

#include <stdio.h>

#include <math.h>



double m, p, a, b, numa, anum, sb, sum;

int main() {

	while (~scanf("%lf%lf%lf%lf", &m, &p, &a, &b)) {

		sum = 0;

		sb = a * b;

		numa = anum = 0;

		for (int i = 0; i < m - 1; i ++) {//注意只进行m - 1次操作,最后一部分要单独考虑

			if (sb < a) {

				anum ++;

				sb ++;

			}

			else {

				sb -= a;

				numa ++;

			}

		}

		sum += anum / pow(sqrt(a), p);

		sum += numa * pow(sqrt(a), p);

		sum += pow((sb / sqrt(a)), p);//剩下的部分

		printf("%d\n", int(sum + 0.5));

	}

	return 0;

}

 

 

 

Sample Input 

 

1997 12 3 -318 

10 2 4 -1

 

Sample Output 

 

189548 

6

题意:给定m,p,a,b.根据题目中的两个条件.求出 xp1 + xp2 +...+ xpm 最大值..

思路:贪心.由于题目明确了p是负数,所以x^p,x绝对值越大的时候值越大。。然后我们根据条件。发现x尽可能取sqrt(a)是最好的。但是不一定能全部取得sqrt(a)。那么多出来的还要拿一部分去抵消。这时候我们就用-1/sqrt(a)去抵消是最好的。这样就能满足最大了。不过要注意。抵消到最后剩下那部分也要考虑进去。

代码:


 

你可能感兴趣的:(live)