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>> ...
随机推荐
- selenium - xpath - 定位
前言: XPath 是一门在 XML 文档中查找信息的语言.XPath 可用来在 XML 文档中对元素和属性进行遍历. 看这里介绍:w3school 首先来看一下xpath常用的语法: 一.xpath ...
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 1) C. Bear and Drawing
题目链接:http://codeforces.com/contest/573/problem/C题目大意:在两行无限长的点列上面画n个点以及n-1条边使得构成一棵树,并且要求边都在同一平面上且除了节点 ...
- MySQL中的交并差
Mysql只提供了并集(union),没有提供差集,和交集,但是我们可以用union来实现交和差,下面即是实现方式: 首先创建两个表: ERROR 1064 (42000): mysql> cr ...
- Regex正则表达式
正则表达式 热身 正则表达式(regular expression)描述了一种字符串匹配的模式,可以用来检查一个串是否含有某种子串.将匹配的子串做替换或者从某个串中取出符合某个条件的子串等. 例如 g ...
- Redhat下 Apache, php, mysql的默认安装路径
apache: 如果采用RPM包安装,安装路径应在 /etc/httpd目录下 apache配置文件:/etc/httpd/conf/httpd.conf Apache模块路径:/usr/sbin/a ...
- java 删除文件夹 / 删除某文件夹下的所有文件
import java.io.File; /*************************删除文件夹delFolder / 删除文件夹中的所有文件delAllFile *start******** ...
- 第一章 Spring整体架构和环境搭建(待续)
········
- How to fix apt-get GPG error NO_PUBKEY Ubuntu 14
This morning when I do apt-get update on my new Ubuntu 14.04 server, I got these error messages: R ...
- SonarQube在CentOS上的安装
1 简介 SonarQube 是一个用于代码质量管理的开放平台.通过插件机制,Sonar 可以集成不同的测试工具,代码分析工具,以及持续集成工具.与持续集成工具(例如 Hudson/Jenkins 等 ...
- OSCache-缓存过滤器CacheFilter
用CashFilter实现页面级缓存. 在OSCache组件中提供了一个CacheFilter用于实现页面级的缓存,主要用于对web应用中的某些动态页面进行缓存,尤其是那些需要生成pdf格式文件/报表 ...