POJ - 2586 Y2K Accounting Bug (找规律)
All what they remember is that MS Inc. posted a surplus or a deficit each month of 1999 and each month when MS Inc. posted surplus, the amount of surplus was s and each month when MS Inc. posted deficit, the deficit was d. They do not remember which or how many months posted surplus or deficit. MS Inc., unlike other companies, posts their earnings for each consecutive 5 months during a year. ACM knows that each of these 8 postings reported a deficit but they do not know how much. The chief accountant is almost sure that MS Inc. was about to post surplus for the entire year of 1999. Almost but not quite.
Write a program, which decides whether MS Inc. suffered a deficit during 1999, or if a surplus for 1999 was possible, what is the maximum amount of surplus that they can post.
Input
Output
Sample Input
59 237
375 743
200000 849694
2500000 8000000
Sample Output
116
28
300612
Deficit 题意:有一个公司,每个月或盈利或亏损,盈利都为s,亏损都为d,已每五个月(1-5月,2-6月,3-7月 etc.)中每个月的盈或亏加起来均小于0
求该公司能否盈利,如果能,输出盈利最大值,不能则输出"Deficit"
题解:如果五个月中有x个月赢,(5-x)个月亏,则应该满足约束x*s<d*(5-x)
期望求出盈利最大值,所以x要尽量大
然后就是求使每五个月中都有(5-x)个月亏所需要月份的最小值
自然可以手模,然而易错,所以可以打个程序暴搜一发
程序如下
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; int sta,ans=0x3f3f3f3f,target; void check(int x)
{
int flag=,tmp=,bit=,a[],cnt[],sum=;
memset(a,,sizeof(a));
memset(cnt,,sizeof(cnt));
while(x)
{
if(x%==)
{
tmp++;
a[bit]=;
}
bit++;
x>>=;
}
sum=a[]+a[]+a[]+a[]+a[];
for(int i=;i<=;i++)
{
if(sum<target)
{
flag=;
}
sum-=a[i];
sum+=a[i+];
}
if(sum<target)
{
flag=;
}
if(flag)
{
ans=min(ans,tmp);
}
} int main()
{
scanf("%d",&target);
for(int sta=;sta<=(<<)-;sta++)
{
check(sta);
}
printf("%d\n",ans);
}
接着,只需要根据上述结论打模拟即可.
代码如下:
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; int ans,tmp,d,s; int main()
{
int x=;
while(scanf("%d%d",&s,&d)==)
{
for(int i=; i>=; i--)
{
if(s*i<d*(-i))
{
x=i;
break;
}
}
x=-x;
if(x==)
{
ans=s*;
}
if(x==)
{
ans=s*-d*;
}
if(x==)
{
ans=s*-d*;
}
if(x==)
{
ans=s*-d*;
}
if(x==)
{
ans=s*-d*;
}
if(x==)
{
ans=-d*;
}
if(ans>=)
{
printf("%d\n",ans);
}
else
{
puts("Deficit");
}
}
} //0 0
//1 2
//2 4
//3 6
//4 9
//5 12
POJ - 2586 Y2K Accounting Bug (找规律)的更多相关文章
- 贪心 POJ 2586 Y2K Accounting Bug
题目地址:http://poj.org/problem?id=2586 /* 题意:某公司要统计全年盈利状况,对于每一个月来说,如果盈利则盈利S,如果亏空则亏空D. 公司每五个月进行一次统计,全年共统 ...
- poj 2586 Y2K Accounting Bug
http://poj.org/problem?id=2586 大意是一个公司在12个月中,或固定盈余s,或固定亏损d. 但记不得哪些月盈余,哪些月亏损,只能记得连续5个月的代数和总是亏损(<0为 ...
- poj 2586 Y2K Accounting Bug (贪心)
Y2K Accounting Bug Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8678 Accepted: 428 ...
- POJ 2586 Y2K Accounting Bug(枚举洪水问题)
Y2K Accounting Bug Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10674 Accepted: 53 ...
- POJ 2586 Y2K Accounting Bug 贪心 难度:2
Y2K Accounting Bug Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10537 Accepted: 52 ...
- POJ 2586 Y2K Accounting Bug(枚举大水题)
Y2K Accounting Bug Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10674 Accepted: 53 ...
- [POJ 2586] Y2K Accounting Bug (贪心)
题目链接:http://poj.org/problem?id=2586 题目大意:(真难读懂啊)给你两个数,s,d,意思是MS公司每个月可能赚钱,也可能赔钱,如果赚钱的话,就是赚s元,如果赔钱的话,就 ...
- POJ 2586 Y2K Accounting Bug(贪心)
题目连接:http://poj.org/problem?id=2586 题意:次(1-5.2-6.3-7.4-8.5-9.6-10.7-11.8-12),次统计的结果全部是亏空(盈利-亏空<0) ...
- poj 2586 Y2K Accounting Bug(贪心算法,水题一枚)
#include <iostream> using namespace std; /*248K 32MS*/ int main() { int s,d; while(cin>> ...
随机推荐
- awk 内容
awk相关内容 #只要文件中的路径,不要文件名: [ ...
- AngularJS:template
ylbtech-AngularJS: 1.返回顶部 1. 2. 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 7.返回顶部 8.返回顶部 9 ...
- mysql 启动卡主,cpu 100%
[mysql@mysqlhq scripts]$ cat /etc/redhat-release Kylin Linux release 3.3.1707 (Core) mysql version S ...
- Maven+Mybatis+Spring+SpringMVC实现分页查询
转载:http://www.cnblogs.com/zhangtan/p/5846955.html 一.项目搭建 关于项目搭建,小宝鸽以前写过一篇Spirng+SpringMVC+Maven+Myba ...
- python学习——练习题(2)
""" 题目:企业发放的奖金根据利润提成.利润(I)低于或等于10万元时,奖金可提10%: 利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元 ...
- django -- url 的 name 属性
在html的form中使用给url定义的name值,可以在修改url时不用在修改form的src. urls.py from django.conf.urls import url from myte ...
- 让IE10等支持classList2.0
chrome24+, firesfox26+起支持classList2.0,即让它同时添加或删除多个类名, toggle方法支持第2个参数,用于强制添加或删除 var div = document.c ...
- 国庆前执行更新承诺SO交期 FP_SO2SAP
每年9月20日到30号执行以下程序:创建日期为昨天的订单,且承诺交期为10月1到3号,则承诺交期需加7天:创建日期为昨天的订单, 承诺交期为4号到11月1日,承诺交期需加4天 存储过程:FP_SO2S ...
- ./run.sh --indir examples/demo/ --outdir examples/results/ --vis
(AlphaPose20180911) luo@luo-ThinkPad-W540:AlphaPose$ ./run.sh --indir examples/demo/ --outdir exampl ...
- 50. Pow(x, n) 幂次方
[抄题]: mplement pow(x, n), which calculates x raised to the power n (xn). Example 1: Input: 2.00000, ...