poj 2586 Y2K Accounting Bug (贪心)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8678 | Accepted: 4288 |
Description
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
Source
题意:
对于每一个月来说,如果是盈利则盈利S,如果亏空则亏d。
每五个月进行一次统计,共统计八次(1-5月一次,2-6月一次.......)
统计的结果是这八次都是亏空。
问题:判断全年是否能盈利,如果能则求出最大的盈利。
如果不能盈利则输出Deficit
分析:
我是先在草稿纸上枚举了一下前两种情况。然后就有了思路。
贪心的思想,8次统计,每次统计报账都是亏损,但是要全年盈利最大,只要每次统计的亏损值最小就可以了。也就是说每次统计的5个月中,盈利的月要尽可能多。
故:
1、用一个数组q1向后判断,一个队列q2把每次确定的12个月各月的盈利和亏损情况存起来。
2、init()函数是用来确定前5个月每个月的盈利和亏损情况的。为了保证盈利的月尽可能多且枚举的次数小,从5个月中4个月盈利到1个月盈利依次枚举,算5个月的总和是否
小于0满足亏损。找到了就存进q1和q2并返回true.找不到返回false,全年则只有亏损的份了。
3、judge()是每次利用之前确定的后四个月的盈利亏损情况向后确定接下来的一个月的盈利亏损情况。例如:已经知道了1、2、3、4、5个月的盈利亏损情况。当要确定第6
个月的情况时,因为2—6月5个月报账,先把2、3、4、5的总账加起来,如果6月盈余加上去满足这5个月亏损,则6月盈余可以使全年的总值尽量大。依次类推。
4、compute()是来算存在q2里全年是盈利还是亏损的。
贪心思想----->子问题局部最优。
感想:
开始sum忘了清0导致第5组测试数据老过不了啊,拙计~!
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
using namespace std; int s,d;
queue<int> q2;
int q1[100000];
int head,rear; bool init()
{
int i,j,k;
head=0,rear=0;
for(i=4;i>=1;i--)
{
if(s*i-d*(5-i)<0)
{
for(j=0;j<i;j++)
{
q1[j]=s;
rear++;
q2.push(s);
}
for(k=i;k<5;k++)
{
q1[k]=-d;
rear++;
q2.push(-d);
}
return true;
}
}
return false;
} int compute()
{
int sum=0;
while(!q2.empty())
{
//printf("%d\n",q2.front());
sum+=q2.front();
q2.pop();
}
return sum;
} void judge()
{
int j;
int sum;
for(int i=0;i<7;i++)
{
head++;
sum=0;
for(j=head;j<rear;j++)
sum+=q1[j];
//printf("test: %d\n",sum+s);
if(sum+s>=0)
{
q1[rear++]=-d;
q2.push(-d);
}
else
{
q1[rear++]=s;
q2.push(s);
}
}
} int main()
{
int leaf;
while(scanf("%d%d",&s,&d)!=EOF)
{
while(!q2.empty())
q2.pop();
leaf=init();
judge();
int s=compute();
if(s<0||leaf==false)
printf("Deficit\n");
else printf("%d\n",s);
}
return 0;
}
11927011 |
Accepted |
164K |
0MS |
1515B |
2013-08-05 22:04:06 |
poj 2586 Y2K Accounting Bug (贪心)的更多相关文章
- 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(贪心算法,水题一枚)
#include <iostream> using namespace std; /*248K 32MS*/ int main() { int s,d; while(cin>> ...
- 贪心 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: 10674 Accepted: 53 ...
- 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 (找规律)
Accounting for Computer Machinists (ACM) has sufferred from the Y2K bug and lost some vital data for ...
随机推荐
- ORACLE 更改username
曾经一直常常改动oracle的用户password,但非常少改动username的. 曾经仅仅能创建一个用户1.然后将用户2数据导入到用户1.然后经用户1删掉,这样很麻烦并且耗时,今天就整理了下怎样改 ...
- VS2012配置astyle格式化代码
1.工具->扩展和更新,搜astyle插件,下载安装重启,当前是2.0版本. 2.工具->选项->AStyle Formatter->Edit,填入下面的,点击save,确定. ...
- 访问祖先类的虚方法(直接访问祖先类的VMT,但是这种方法在新版本中未必可靠)
访问祖先类的虚方法 问题提出 在子类覆盖的虚方法中,可以用inherited调用父类的实现,但有时候我们并不需要父类的实现,而是想跃过父类直接调用祖先类的方法. 举个例子,假设有三个类,实现如下: t ...
- Zeroonepack coming~^.^
今天抓的四道DP做完了==三道是用背包做的,突然想起来背包知识点总结还没做~反正时间还早..把01背包和完全背包小结了吧~~福利来啦~~噶呜~ 01背包: 基本思路: 01背包问题是最广为人知的动态规 ...
- 字符串string和内存流MemoryStream及比特数组byte[]互转
原文:字符串string和内存流MemoryStream及比特数组byte[]互转 字符串string和内存流MemoryStream及比特数组byte[]互转比较 定义string变量为str, ...
- Java Web----Java Web的数据库操作(二)
Java Web的数据库操作 三.JDBC操作数据库 上一篇介绍了JDBC API,之后就可以通过API来操作数据库,实现对数据库的CRUD操作了. http://blog.csdn.net/zhai ...
- uva11426(莫比乌斯反演)
传送门:GCD Extreme (II) 题意:给定n(n<=4000000),求G G=0 for(int i=1;i<n;i++) for(int j=i+1;j<=n;j++) ...
- CentOS Kernel Source Install
http://linuxmoz.com/centos-kernel-source-install/
- poj 2166 构造
一个看了解题报告才能想明白的题目,第一点比较容易想明白,就是每次把1交换到堆顶之后如果能够换到最后面的位置那么一定是最优的,但是怎么实现这个没有想明白. 题解的那种构建方法,及从2开始插入,是可以保证 ...
- BZOJ 1176([Balkan2007]Mokia-CDQ分治-分治询问)
1176: [Balkan2007]Mokia Time Limit: 30 Sec Memory Limit: 162 MB Submit: 185 Solved: 94 [ Submit] ...