UVAlive 2911 Maximum(贪心)

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

a)
-$ {\frac{​{1}}{​{\sqrt{a}}}}$$ \le$xi$ \le$$ \sqrt{​{5}}$ ;
b)
x1 + x2 +...+ xm = b * $ \sqrt{​{a}}$ 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 ( m$ \le$2000, p$ \le$12p 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)去抵消是最好的。这样就能满足最大了。不过要注意。抵消到最后剩下那部分也要考虑进去。

代码:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值