URAL 1200. Horns and Hoofs 枚举+数学
2013-08-05 13:38
309 查看
接触的东西越来越来反而变得不够灵活了,以前碰到的这样的题都要暴力,枚举,两个for循环试试的,今天看到时脑子里接着想暴力会超时的,但是好几个人都200+ms过了,我一直在推导,证明,最后越推越麻烦,一个题也没做出来,其实这个题很简单的,O(A的范围的算法),一个for循环,枚举A的个数,然后根据profit=A*a+B*b-a*a-b*b,是y的二次函数,根据图像b=B/2时最大,然后以此为轴,两边递减。
纯暴力枚举的算法。
#include<cstdio> #include<iostream> #include<cstring> #include<cstdlib> using namespace std; int main() { //freopen("in.txt","r",stdin); double A,B,maxx; int k,a,b; scanf("%lf%lf%d",&A,&B,&k); maxx=-100000; a=0; b=0; for(int i=0; i<=k; i++) { int y=B/2; double temp; if(y+i>k) y=k-i; if(y<0) y=0; temp=A*i+B*y-i*i-y*y; if(temp>maxx) { maxx=temp; a=i; b=y; } } printf("%.2lf\n%d %d\n",maxx,a,b); return 0; }
纯暴力枚举的算法。
#include<cstdio> #include<iostream> #include<cstring> #include<cstdlib> using namespace std; int main() { //freopen("in.txt","r",stdin); double A,B,maxx; int a=0,b=0,k; maxx=-100000; scanf("%lf%lf%d",&A,&B,&k); for(int i=0;i<=k;i++) { for(int j=0;j+i<=k;j++) { double temp=A*i+B*j-i*i-j*j; if(temp>maxx) { maxx=temp; a=i; b=j; } } } printf("%.2lf\n%d %d\n",maxx,a,b); return 0; }
相关文章推荐
- URAL 1502. Domino Dots(数学)
- Codeforces Round #332 (Div. 2) D. Spongebob and Squares 数学题枚举
- URAL 2025. Line Fighting 简单数学推理
- UVA 11889-Benefit(数学_快速枚举因子)
- ural 2032 Conspiracy Theory and Rebranding (数学水题)
- bzoj 4921: 互质序列 数学+枚举
- URAL 1984. Dummy Guy(数学啊)
- hpuoj-1215-毛毛的数学问题【数学】【枚举】
- URAL 1500. Pass Licenses 枚举+位运算
- UVa 143 Orchard Trees (数学&计算几何&枚举)
- UVA 10976 Fractions Again?(枚举+数学)
- BestCoder Round #35(第一题解题报告(组合数学(期望的可加性)或暴力枚举)
- 【枚举】URAL - 2081 - Faulty dial
- URAL 2070 Interesting Numbers(数学)
- cf#AIM Tech D. Array GCD (数学+枚举)
- URAL 1792. Hamming Code (枚举)
- poj2245(枚举||组合数学||dfs)
- URAL 2003. Simple Magic(数学啊 )
- URAL 1987. Nested Segments(数学 & 线段树)
- URAL 1823. Ideal Gas(数学啊 )