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 即可。
1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cmath> 5 #include <algorithm> 6 7 const double eps = 1e-3; 8 const double INF = 1e10; 9 10 using namespace std; 11 12 double A, B; 13 int N; 14 15 int dcmp( double a ) 16 { 17 if ( fabs(a) < eps ) return 0; 18 return a < 0 ? -1 : 1; 19 } 20 21 double cal( int x, int y ) 22 { 23 return A*x + B*y -x*x - y*y; 24 } 25 26 int main() 27 { 28 while ( ~scanf( "%lf%lf", &A, &B ) ) 29 { 30 scanf( "%d", &N ); 31 double ans = -INF; 32 int ansX, ansY; 33 34 for ( int i = 0; i <= N; ++i ) 35 { 36 int tmp = floor(( 2 * i + A - B ) / 4.0) ; 37 if ( tmp < 0 ) tmp = 0; 38 else if ( tmp > i ) tmp = i; 39 40 double tmpcal = cal( tmp, i - tmp ); 41 if ( dcmp( ans - tmpcal ) < 0 ) 42 { 43 ans = tmpcal; 44 ansX = tmp; 45 ansY = i - tmp; 46 } 47 else if ( dcmp( ans - tmpcal ) == 0 ) 48 { 49 if ( tmp < ansX ) 50 { 51 ansX = tmp; 52 ansY = i - tmp; 53 } 54 else if ( tmp == ansX ) 55 { 56 if ( ansY > i - tmp ) 57 { 58 ansY = i - tmp; 59 } 60 } 61 } 62 63 tmp = ceil( ( 2 * i + A - B ) / 4.0 ) ; 64 65 if ( tmp < 0 ) tmp = 0; 66 else if ( tmp > i ) tmp = i; 67 68 tmpcal = cal( tmp, i - tmp ); 69 if ( dcmp( ans - tmpcal ) < 0 ) 70 { 71 ans = tmpcal; 72 ansX = tmp; 73 ansY = i - ansX; 74 } 75 else if ( dcmp( ans - tmpcal ) == 0 ) 76 { 77 if ( tmp < ansX ) 78 { 79 ansX = tmp; 80 ansY = i - tmp; 81 } 82 else if ( tmp == ansX ) 83 { 84 if ( ansY > i - tmp ) 85 { 86 ansY = i - tmp; 87 } 88 } 89 } 90 } 91 92 if ( dcmp(ans) <= 0 || ( ansX <= 0 && ansY <= 0 ) ) 93 { 94 puts("0.00"); 95 puts("0 0"); 96 continue; 97 } 98 99 printf( "%.2f\n", ans ); 100 printf( "%d %d\n", ansX, ansY ); 101 } 102 return 0; 103 }
转载于:https://www.cnblogs.com/GBRgbr/p/3237955.html
URAL 1200 Horns and Hoofs 枚举相关推荐
- URAL 1200. Horns and Hoofs 枚举+数学
接触的东西越来越来反而变得不够灵活了,以前碰到的这样的题都要暴力,枚举,两个for循环试试的,今天看到时脑子里接着想暴力会超时的,但是好几个人都200+ms过了,我一直在推导,证明,最后越推越麻烦,一 ...
- URAL:1200 Horns and Hoofs
虽然原题给了250MS,看似很短,其实暴力也是跑能过去的... 应该坑了不少人. #include <iostream> #include <cstdio> #include ...
- URAL 1200 - Horns and Hoofs(暴力+剪枝)
比赛时水过的,现在贴一下正规做法. #include <cstdio> #include <cstring> #include <cstdlib> #include ...
- 让我想到微观经济学的一个题 1200. Horns and Hoofs
原文链接: http://acm.timus.ru/problem.aspx?space=1&num=1200 背景: 最优化问题: 一个人养a, b两种东西, 单只的收益分别为 A, ...
- 字符串类型、结构体、共用体、枚举、container宏、内存来源
一.C语言的字符串类型 1.C语言没有原生字符串类型 很多高级语言像java.C#等就有字符串类型,有个String来表示字符串,用法和int这些很像,可以String s1 = "linu ...
- 递推DP URAL 1586 Threeprime Numbers
题目传送门 1 /* 2 题意:n位数字,任意连续的三位数字组成的数字是素数,这样的n位数有多少个 3 最优子结构:考虑3位数的数字,可以枚举出来,第4位是和第3位,第2位组成的数字判断是否是素数 4 ...
- PCIe学习笔记之pcie初始化枚举和资源分配流程代码分析
本文主要是对PCIe的初始化枚举.资源分配流程进行分析.代码对应的是linux 4.19, 平台是arm64. 文章首发于这里 1. PCIe architecture 1.1 pcie的拓扑结构 在 ...
- ural 2032 Conspiracy Theory and Rebranding (数学水题)
ural 2032 Conspiracy Theory and Rebranding 链接:http://acm.timus.ru/problem.aspx?space=1&num=2032 ...
- ural 2032 Conspiracy Theory and Rebranding 整点三角形
ural 2032 Conspiracy Theory and Rebranding 链接:https://vjudge.net/contest/175269#problem/I 题意:给定一个三角形 ...
最新文章
- Why is OFDMA a Magical Feature in the 802.11ax Standard?
- 从C语言的角度重构数据结构系列(一)-数据结构入门之逻辑结构与物理结构
- Lombok pojo类小神器
- k8s:资源类型及yaml语法
- 剪刀,石头,布,小游戏脚本
- C++ 序列化 serialization 如何将类持久化?
- POJ 1651 Multiplication Puzzle(类似矩阵连乘 区间dp)
- java约瑟夫环pta上_cdoj525-猴子选大王 (约瑟夫环)
- Linux驱动(6)--关于uboot
- 4-1 :input表单选择器 jQuery第四章 很关键 好像 刚好可以解决 微信自动回复...
- python类型错误:can only concatenate list (not str) to list
- jave 逻辑运算 vs 位运算 + Python 逻辑运算 vs 位运算
- 问答| 为何会采用倒车入库(侧方位停车)方式?
- 关闭windows 2008的自动播放
- Chapter3 Language Basics
- curve BLS12-377/381 BN256 SageMath脚本
- 西门子 Prodave通讯
- 今日头条新闻采集爬虫分享
- 关于ONVIF协议你不得不知的6个常见问题
- python 拆分excel工作表_Python合并拆分excel