看到了大神的代码。理解了好久。。。真是差距。

题意:给出m, p, a, b,然后xi满足已下两个公式, 求 xp1 + xp2 +...+ xp的最大值。

1、-1/sqrt(a) <= xi <= sqrt(a); (a>0)

2、x1+x2+...+xm = b*sqrt(a);

注意:p为偶数。

解题思路:因为p为偶数,所以sqrt(a)和-1/sqrt(a)的p次方都为正数且sqrt(a) > 1/sqrt(a).所以贪心思想时尽量先取sqrt(a);当已经取的xi的和大于b*sqrt(a)时,再取(-1/sqrt(a))抵消多出的sqrt(a)。

为了简便计算同时减少sqrt()带来的误差。可以现将2式左右同乘以sqrt(a)。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int main(){
int m, p, a, b;
while(~scanf("%d%d%d%d", &m, &p, &a, &b)) {
int l = , r = ; //l, r分别代表-1/sqrt(a)与sqrt(a)的数目
int sum = a * b;
for(int i = ; i < m; i++) { //注意最后一个数不要考虑
if(sum >= a) {
r++;
sum -= a; //xi已经取定sqrt(a)后,sum记得更新
}
else {
l++;
sum++;
}
}
double ans = l / pow(sqrt(a), p) + r * pow (sqrt(a), p);
ans += pow(sum/sqrt(a), p); //记得要除掉之前乘的sqrt(a);
printf("%d\n", (int)(ans + 0.5));
}
return ;
}

【推理,贪心】UVa 1319 - Maximum的更多相关文章

  1. UVa 10827 - Maximum sum on a torus

    题目大意:UVa 108 - Maximum Sum的加强版,求最大子矩阵和,不过矩阵是可以循环的,矩阵到结尾时可以循环到开头.开始听纠结的,想着难道要分情况讨论吗?!就去网上搜,看到可以通过补全进行 ...

  2. UVA.10305 Maximum Product (暴力)

    UVA.10305 Maximum Product (暴力) 题意分析 直接枚举起点和重点,然后算出来存到数组里面,sort然后取最大值即可. 代码总览 #include <iostream&g ...

  3. UVa 108 - Maximum Sum(最大连续子序列)

    题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...

  4. 贪心 UVA 11729 Commando War

    题目传送门 /* 贪心:按照执行时间长的优先来排序 */ #include <cstdio> #include <algorithm> #include <iostrea ...

  5. uva 11059 maximum product(水题)——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAB1QAAAMcCAIAAABo0QCJAAAgAElEQVR4nOydW7msuhKF2wIasIAHJK

  6. UVA 11827 Maximum GCD

    F - Maximum GCD Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Given the ...

  7. Uva 11059 Maximum Product

    注意long long  long long  longlong !!!!!!   还有 printf的时候 明明longlong型的答案 用了%d  WA了也看不出,这个细节要注意!!! #incl ...

  8. UVA 12906 Maximum Score 排列组合

    Maximum Score Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/vie ...

  9. UVa 11059 - Maximum Product 最大乘积【暴力】

    题目链接:https://vjudge.net/contest/210334#problem/B 题目大意:Given a sequence of integers S = {S1, S2, . . ...

随机推荐

  1. Spring Auto-Wiring Beans with @Autowired annotation

    In last Spring auto-wiring in XML example, it will autowired the matched property of any bean in cur ...

  2. 开发WebForm时遇到的问题

    在做一个小项目时,一个很长的页面,页面底部有一个contact us form 整个页面我没有使用MVC,而是使用ASP.NET WebForm(.aspx)来实现,实现功能后发现,当用户在页面底部输 ...

  3. Arduino Due, Maple and Teensy3.0 的 W5200性能测试

    开源平台中以太网连接方案里W5100是众所周知的,W5200正在此领域越来越受欢迎.这个测试结果是在Arduino Due(Atmel CortexM3-84Mhz), Maple(ST Cortex ...

  4. curl用法

    简介 curl是一个和服务器交互信息(发送和获取信息)的命令行工具,支持DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, L ...

  5. MVC神韵---你想在哪解脱!(十)

    增加追加数据的方法和视图 现在我们将要在数据库中追加并保存一些数据.我们将要创建一个表单以及一些表单输入控件,用来输入数据信息.当用户提交表单时将把这些用户输入的信息保存在数据库中.我们可以通过在浏览 ...

  6. JS Math 类库介绍

    下面介绍下随机生成数的常用几个API JS 随机数生成 : 在JavaScript , 提供了生成随机数的API, Math.random() 1.Math.random() : 随机生成小数 . 生 ...

  7. oracle分区表相关

    1.查询某个表各分区数据量 select count(*) from table_name partition(分区名) 可以使用sql生成所有分区查询语句: 2.分区表truncate 分区 alt ...

  8. VMM服务模板(虚机、APP)部署排错

    I won't focus this blog on how to create a service template but more on how you can track the change ...

  9. HDU 4343 D - Interval query 二分贪心

    D - Interval queryTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest ...

  10. URAL 1775 B - Space Bowling 计算几何

    B - Space BowlingTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/ ...