URAL 1200 Horns and Hoofs 枚举
设horns和hoofs的数量分别为 x 和 y ,题目要求:
满足 x+y <= K,使得A*x + B*y - x*x - y*y 最大。
枚举 i 从0~K,直接解方程得对称轴 x = ( 2*i + A - B ) / 4,判断对称轴是否在 [ 0, i ] 区间内。
注意:
1.精度
2.x需要上下个取整一次
3.如果最大值都<=0,那么最大收益直接为 0 即可。
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm> const double eps = 1e-;
const double INF = 1e10; using namespace std; double A, B;
int N; int dcmp( double a )
{
if ( fabs(a) < eps ) return ;
return a < ? - : ;
} double cal( int x, int y )
{
return A*x + B*y -x*x - y*y;
} int main()
{
while ( ~scanf( "%lf%lf", &A, &B ) )
{
scanf( "%d", &N );
double ans = -INF;
int ansX, ansY; for ( int i = ; i <= N; ++i )
{
int tmp = floor(( * i + A - B ) / 4.0) ;
if ( tmp < ) tmp = ;
else if ( tmp > i ) tmp = i; double tmpcal = cal( tmp, i - tmp );
if ( dcmp( ans - tmpcal ) < )
{
ans = tmpcal;
ansX = tmp;
ansY = i - tmp;
}
else if ( dcmp( ans - tmpcal ) == )
{
if ( tmp < ansX )
{
ansX = tmp;
ansY = i - tmp;
}
else if ( tmp == ansX )
{
if ( ansY > i - tmp )
{
ansY = i - tmp;
}
}
} tmp = ceil( ( * i + A - B ) / 4.0 ) ; if ( tmp < ) tmp = ;
else if ( tmp > i ) tmp = i; tmpcal = cal( tmp, i - tmp );
if ( dcmp( ans - tmpcal ) < )
{
ans = tmpcal;
ansX = tmp;
ansY = i - ansX;
}
else if ( dcmp( ans - tmpcal ) == )
{
if ( tmp < ansX )
{
ansX = tmp;
ansY = i - tmp;
}
else if ( tmp == ansX )
{
if ( ansY > i - tmp )
{
ansY = i - tmp;
}
}
}
} if ( dcmp(ans) <= || ( ansX <= && ansY <= ) )
{
puts("0.00");
puts("0 0");
continue;
} printf( "%.2f\n", ans );
printf( "%d %d\n", ansX, ansY );
}
return ;
}
URAL 1200 Horns and Hoofs 枚举的更多相关文章
- URAL 2031. Overturned Numbers (枚举)
2031. Overturned Numbers Time limit: 1.0 second Memory limit: 64 MB Little Pierre was surfing the In ...
- ural 1200
推出公式 然后特判两端 代码其实挺烂 但是有人竟然可以直接暴过去的 ...... #include <cstdio> #include <cstring> #in ...
- ural1701 Ostap and Partners
Ostap and Partners Time limit: 2.0 secondMemory limit: 64 MB Workman Ivan lost his job. Not because ...
- URAL 1792. Hamming Code (枚举)
1792. Hamming Code Time limit: 1.0 second Memory limit: 64 MB Let us consider four disks intersectin ...
- 【折半枚举】Ural Championship April 30, 2017 Problem G. Glasses with solutions
题意:有n杯盐溶液,给定每杯里面盐的质量以及盐溶液的质量.问你有多少种方案选择一个子集,使得集合里面的盐溶液倒到一个被子里面以后,浓度为A/B. 折半枚举,暴力搜索分界线一侧的答案数,跨越分界线的答案 ...
- 【枚举】URAL - 2081 - Faulty dial
//._. ... ._. ._. ... ._. ._. ._. ._. ._. //|.| ..| ._| ._| |_| |_. |_. ..| |_| |_| //|_| ..| |_. ._ ...
- POJ 1200 字符串HASH
题目链接:http://poj.org/problem?id=1200 题意:给定一个字符串,字符串只有NC个不同的字符,问这个字符串所有长度为N的子串有多少个不相同. 思路:字符串HASH,因为只有 ...
- 后缀数组 POJ 3974 Palindrome && URAL 1297 Palindrome
题目链接 题意:求给定的字符串的最长回文子串 分析:做法是构造一个新的字符串是原字符串+反转后的原字符串(这样方便求两边回文的后缀的最长前缀),即newS = S + '$' + revS,枚举回文串 ...
- ural 2071. Juice Cocktails
2071. Juice Cocktails Time limit: 1.0 secondMemory limit: 64 MB Once n Denchiks come to the bar and ...
随机推荐
- Visual Studio 2013
1.How to hide reference counts in VS2013? Tools--> Options --> Text Editor --> All Language ...
- SqlServer 临时表、表变量、函数 替代游标
http://www.cnblogs.com/chongzi/archive/2011/01/19/1939106.html 临时表 存放在tempdb中 --存储过程中将多表连接结果写入到临时表中, ...
- 设计模式之适配器模式(Adapter)
适配器模式原理:适配器模式属于结构型模式,主要作用是完成功能的转换, 1.通过一个类继承目标类. 2.需要适配的类 3.适配器 代码如下: #include <iostream> usin ...
- Facebook 和 Google 如何激发工程师的创造力
原文链接:http://kb.cnblogs.com/page/193450/ 今天终于“朝圣”了两个伟大的公司——Facebook和Google,对创造力和驱动力的来源有了更多的理解,尤其是对于典型 ...
- 【BZOJ】【3156】防御准备
DP/斜率优化 斜率优化的裸题…… sigh……又把$10^6$当成10W了……RE了N发 这题还是很水的 当然逆序也能做……不过还是整个反过来比较顺手 反转后的a[0]=反转前的a[n],以此类推直 ...
- 【BZOJ】【3004】吊灯
思路题 要将整棵树分成大小相等的连通块,那么首先我们可以肯定的是每块大小x一定是n的约数,且恰好分成$\frac{n}{x}$块,所以我有了这样一个思路:向下深搜,如果一个节点的size=x,就把这个 ...
- CoreText 使用教程
[iOS开发] CoreText 使用教程:以创建一个简单的杂志应用为例抢沙发 分类:iPhone开发 标签:CoreText.iOS.iOS开发.iOS开发教程.杂志应用 BBS.CHINAAPP. ...
- JS中函数的基础知识
函数 一. 函数定义 函数又叫方法,在程序里面函数是用来执行某些特定功能的代码.为了减少重复使用代码,可以把特定功能的代码做成函数,需要使用时拿出来调用.alert();就是一个很常见的.简单的函数 ...
- 2014 ACM/ICPC Asia Regional Xi'an Online Paint Pearls
传说的SB DP: 题目 Problem Description Lee has a string of n pearls. In the beginning, all the pearls have ...
- 5种你未必知道的JavaScript和CSS交互的方法
随着浏览器不断的升级改进,CSS和JavaScript之间的界限越来越模糊.本来它们是负责着完全不同的功能,但最终,它们都属于网页前端技术,它们需要相互密切的合作.我们的网页中都有.js文件和.css ...