Y2K Accounting Bug
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8678   Accepted: 4288

Description

Accounting for Computer Machinists (ACM) has sufferred from the Y2K bug and lost some vital data for preparing annual report for MS Inc.
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

Input is a sequence of lines, each containing two positive integers s and d.

Output

For each line of input, output one line containing either a single integer giving the amount of surplus for the entire year, or output Deficit if it is impossible.

Sample Input

59 237
375 743
200000 849694
2500000 8000000

Sample Output

116
28
300612
Deficit

Source

Waterloo local 2000.01.29

题意:

对于每一个月来说,如果是盈利则盈利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

fukan

2586

Accepted

164K

0MS

C++

1515B

2013-08-05 22:04:06

poj 2586 Y2K Accounting Bug (贪心)的更多相关文章

  1. POJ 2586 Y2K Accounting Bug 贪心 难度:2

    Y2K Accounting Bug Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10537   Accepted: 52 ...

  2. poj 2586 Y2K Accounting Bug(贪心算法,水题一枚)

    #include <iostream> using namespace std; /*248K 32MS*/ int main() { int s,d; while(cin>> ...

  3. 贪心 POJ 2586 Y2K Accounting Bug

    题目地址:http://poj.org/problem?id=2586 /* 题意:某公司要统计全年盈利状况,对于每一个月来说,如果盈利则盈利S,如果亏空则亏空D. 公司每五个月进行一次统计,全年共统 ...

  4. poj 2586 Y2K Accounting Bug

    http://poj.org/problem?id=2586 大意是一个公司在12个月中,或固定盈余s,或固定亏损d. 但记不得哪些月盈余,哪些月亏损,只能记得连续5个月的代数和总是亏损(<0为 ...

  5. POJ 2586 Y2K Accounting Bug(枚举洪水问题)

    Y2K Accounting Bug Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10674   Accepted: 53 ...

  6. POJ 2586 Y2K Accounting Bug(枚举大水题)

    Y2K Accounting Bug Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10674   Accepted: 53 ...

  7. [POJ 2586] Y2K Accounting Bug (贪心)

    题目链接:http://poj.org/problem?id=2586 题目大意:(真难读懂啊)给你两个数,s,d,意思是MS公司每个月可能赚钱,也可能赔钱,如果赚钱的话,就是赚s元,如果赔钱的话,就 ...

  8. 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) ...

  9. POJ - 2586 Y2K Accounting Bug (找规律)

    Accounting for Computer Machinists (ACM) has sufferred from the Y2K bug and lost some vital data for ...

随机推荐

  1. A*寻路算法的实现

    原理:http://www.cppblog.com/christanxw/archive/2006/04/07/5126.html 算法理论请到原理这个传送门,代码中的注释,已经比较详细,所以我不会讲 ...

  2. [面经] 南京SAP面试(上)

    背景 博主乃985弱校的小硕一枚,在南京某外企工作了两年,如今的公司还不错,待遇还行,做的东西也比較有意思.在南京这个地方,给力的公司不太多,仅仅要是跟亲戚朋友聊到我在南京做IT,无一例外都会问&qu ...

  3. Java学习之道:jdk环境变量配置方法

    JDK(Java Development Kit)是整个Java的核心,包含了Java执行环境.Java工具和Java基础类库.JDK作为JAVA开发的环境,无论是做JAVA开发还是做安卓开发,都必须 ...

  4. POJ 3189 Steady Cow Assignment【网络流】

    题意:每个奶牛对所有的牛棚有个排名(根据喜欢程度排的),每个牛棚能够入住的牛的数量有个上限,重新给牛分配牛棚,使牛棚在牛心中的排名差(所有牛中最大排名和最小排名之差)最小. 牛棚个数最多为20,那么直 ...

  5. 记录break和continue的区别

    我对break 和 continue 还是有点搞不清除,今天在看<Thinking in Java>,看到这个,学习了一下.       break的作用是跳出这个循环(如果这个break ...

  6. OpenGL--第一个OpenGL程序

    环境:VS2012 + OpenGL所需文件(其他IDE也可以,不一定要VS2012,VS2010或其他也可以) 步骤: 1.下载Vs2012 2.下载OpenGL所需文件 3.解压缩OpenGL包并 ...

  7. 【Struts2学习笔记(11)】对action的输入校验和XML配置方式实现对action的全部方法进行输入校验

    在struts2中,我们能够实现对action的全部方法进行校验或者对action的指定方法进行校验. 对于输入校验struts2提供了两种实现方法: 1. 採用手工编写代码实现. 2. 基于XML配 ...

  8. ios 6 横竖屏转换

    xcode 版本4.5     模拟器:6.0 项目需求:刚进去界面横屏,从这个界面进去的界面全是竖屏. 程序的根控制器用了UINavigationController.下面是代码: 1.在appde ...

  9. linux内核笔记-内核同步

    linux内核就相当于不断对请求进行响应的服务器,这些请求可能来自CPU,可能来自发出中断的外部设备.我们将内核看作两种请求的侍者. (1)老板提出请求,侍者如果空闲,为老板服务.(系统调用或异常) ...

  10. TopCoder SRM 625 Incrementing Sequence 题解

    本题就是给出一个数k和一个数组,包含N个元素,通过每次添加�数组中的一个数的操作,最后须要得到1 - N的一个序列,不用排序. 能够从暴力法入手,然后优化. 这里利用hash表进行优化,终于得到时间效 ...