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 ...
随机推荐
- 项目中用到的js日期函数
<script type="text/javascript"> //替换字符串 function Replace(str, from, to) { ...
- 使用Java Service Wrapper在Linux下配置Tomcat应用
前言 Java Service Wrapper是Tanuki Software的一个产品,可以将Java应用注册成Windows或Linux服务,使其可以随系统开机启动,同时可以监控Java应用的状态 ...
- C++ Template之函数模版
函数模版的定义: template <typename T> T const& max(const T& a,const T b) { return a > b ? ...
- 【BZOJ】【2178】圆的面积并
自适应辛普森积分 Orz Hzwer 辛普森真是个强大的东西……很多东西都能积= = 这题的正解看上去很鬼畜,至少我这种不会计算几何的渣渣是写不出来……(对圆的交点求图包,ans=凸包的面积+一堆弓形 ...
- Dynamic Programming - Part1
public static void main(String[] args) { //give N, find the number of different ways to write N as t ...
- win8 修改msconfig 里面的"引导高级选项" 最大内存后 BSOD的解决方案
最近由于本人的电脑一直非常卡,于11.4日通过win8任务管理器分析发现 Peer Name Resolution Protocol Peer Networking Grouping Peer Net ...
- thinkphp中SQLSTATE[42S02]: Base table or view not found: 1146 Table错误解决方法
随手记录下今天在thinkphp3.2.3中遇到的错误SQLSTATE[42S02]: Base table or view not found: 1146 Table 'test.file_info ...
- asp.net中实现群发邮件功能
前段时间在帮老师开发的网站中需要用到一个群发邮件的功能,而自己之前学习cms系统的时候用的we7的群发邮件功能也有一些问题,于是乎便自己去网上查了一下资料,自己总结了一下,并且封装成了一个类,亲测有用 ...
- .NET设计模式(4):建造者模式(Builder Pattern)(转)
概述 在软件系统中,有时候面临着“一个复杂对象”的创建工作,其通常由各个部分的子对象用一定的算法构成:由于需求的变化,这个复杂对象的各个部分经常面临着剧烈的变化,但是将它们组合在一起的算法确相对稳定. ...
- 如何理解JS项目
JS API(DOM/PhoneGap/Cordova/NodeJS/Library/Android/MongoDB....)最基础,可以看懂一行代码. -------> JS OOP, JS语 ...