UVAlive 2911 Maximum(贪心)
Let x1, x2,..., 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: m, p, a, b ( m2000, p
12, p is even). Input is correct, i.e. for each input numbers there exists x1, x2,..., 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)去抵消是最好的。这样就能满足最大了。不过要注意。抵消到最后剩下那部分也要考虑进去。
代码:
UVAlive 2911 Maximum(贪心)的更多相关文章
- uvalive 2911 Maximum(贪心)
题目连接:2911 - Maximum 题目大意:给出m, p, a, b,然后xi满足题目中的两个公式, 要求求的 xp1 + xp2 +...+ xpm 的最大值. 解题思路:可以将x1 + x2 ...
- UVALive 4867 Maximum Square 贪心
E - Maximum Square Time Limit:4500MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit ...
- Maximum 贪心
Maximum Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Des ...
- UVALive - 4225(贪心)
题目链接:https://vjudge.net/contest/244167#problem/F 题目: Given any integer base b ≥ 2, it is well known ...
- UVALive 4850 Installations 贪心
题目链接 题意 工程师要安装n个服务,其中服务Ji需要si单位的安装时间,截止时间为di.超时会有惩罚值,若实际完成时间为ci,则惩罚值为max{0,ci-di}.从0时刻开始执行任务,问惩罚值最大 ...
- UVALive 4863 Balloons 贪心/费用流
There will be several test cases in the input. Each test case will begin with a line with three inte ...
- UVALive - 6268 Cycling 贪心
UVALive - 6268 Cycling 题意:从一端走到另一端,有T个红绿灯,告诉你红绿灯的持续时间,求最短的到达终点的时间.x 思路:
- UVALive 4731 dp+贪心
这个题首先要利用题目的特性,先贪心,否则无法进行DP 因为求期望的话,越后面的乘的越大,所以为了得到最小值,应该把概率值降序排序,把大的数跟小的系数相乘 然后这种dp的特性就是转移的时候,由 i推到i ...
- UVALive 3835:Highway(贪心 Grade D)
VJ题目链接 题意:平面上有n个点,在x轴上放一些点,使得平面上所有点都能找到某个x轴上的点,使得他们的距离小于d.求最少放几个点. 思路:以点为中心作半径为d的圆,交x轴为一个线段.问题转换成用最少 ...
随机推荐
- 安装ngix
第一步:解压源码包 第二步:./configure -->这个时候会提示缺少PCRE 这个时候要安装yum -y install pcre-devel 第三步:./configure --> ...
- Delphi 服务操作
unit Service; interface uses Windows,Messages,SysUtils,Winsvc,Dialogs; function StartServices(Const ...
- Activitys, Threads, & Memory Leaks
Activitys, Threads, & Memory Leaks 在Android编程中,一个公认的难题是在Activity的生命周期如何协调长期运行的任务和避免有可能出现的内存泄漏问题. ...
- xxx.java: Recompile with -Xlint:unchecked for details.
一.遇到问题:用ant执行jasperreport的samples/charts示例的build.xml时,无法编译,提示错误如下:javac: [javac] Compiling 2 sour ...
- CC++初学者编程教程(13) 基于Oracle linux 的Oracle12c环境搭建
1设置虚拟机选项 2 设置文件夹共享 3启动文件夹共享向导 4 设置共享文件夹 5 启用共享 6 关闭虚拟机设置 7 开启虚拟机 8 登陆帐户 9 看见虚拟机桌面 10 安装vmwaretools 1 ...
- ubuntu14.04 qt4开发环境搭建(vnc use gnome)
1,安装qt开发环境软件包:apt-get install qt4-dev-tools qt4-doc qt4-qtconfig qt4-demos qt4-designer qtcreator; 2 ...
- LINQ 图解 LINQ学习第三篇 [转]
LINQ,语言集成查询(Language INtegrated Query)是一组用于c#和Visual Basic语言的扩展.它允许编写C#或者Visual Basic代码以查询数据库相同的方式操作 ...
- Error occured processing XML 'Cannot find class [springmvc.extention.BeanArgumentResolver]'.
<Description Resource Path Location Type Error occured processing XML 'Cannot find class [springm ...
- Repeater获取某一行TextBox值
TextBox tb = (TextBox)e.Item.FindControl("TextBoxID");
- Quartz.NET 2.0 作业调度框架使用
Quartz.NET是一个开源的作业调度框架,是 OpenSymphony 的 Quartz API 的.NET移植,它用C#写成,可用于winform和asp.net应用中.它提供了巨大的灵活性而不 ...