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 ...
 
随机推荐
- 【Longest Palindromic Substring】cpp
			
题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...
 - Dedication(转)
			
To all developers who strive for continuous self-improvement... Who are not satisfied with good enou ...
 - cygwin chmod 失效
			
问题背景 为了在 Cygwin 下使用之前最喜爱的 screen 命令, 安装 Cygwin 时就选上了 screen 来运行一把 ganiks.liu@MAMIS-Gaiks-Liu /tmp $ ...
 - Matlab中颜色线形和形状
			
1.颜色字符串有'c', 'm', 'y', 'r', 'g', 'b', 'w',和'k'.分别表示青,红紫,黄,红,绿,白和黑. 2.线型字符串有:'-' 为实线, '--' 为虚线, ':' 为 ...
 - FormCreate & FormActivate & FormShow执行顺序演示
			
procedure TForm1.FormCreate(Sender: TObject);begin form1.Caption:=form1.Caption +'+Create'; end; pr ...
 - UML基本表示法(转载)
			
UML是流行的图解符号.我们都知道,UML是可视化,说明,构建和记录软件和非软件系统的组成部分.这里的可视化是最重要的部分,需要被理解和记忆. UML符号是最重要的建模元素.适当有效地使用符号是非常重 ...
 - 利用传入的Type类型来调用范型方法的解决方案
			
起因:自定义一个GridView控件,其数据源来源于一个通用方法Get<T>(),根据你传入的T到数据库中得到相应的数据,问题是定义GridView控件时没法在界面端设置使用泛型,只能在每 ...
 - Sqlite中使用rowid来表示行号,用于分页。
			
在SQLite的查询结果中显示行号,可以使用select rowid as RowNumber ,* from WSCLanguage: select rowid as RowNumber ,* fr ...
 - 关于SOAP
			
http://www.tutorialspoint.com/soap/index.htm http://www.w3.org/TR/2000/NOTE-SOAP-20000508/ SOAP协议规范介 ...
 - sourcemap的使用
			
minify.bat @echo off if ""%1""=="""" goto end :loop if not e ...