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. 多校第五场 归并排序+暴力矩阵乘+模拟+java大数&amp;记忆化递归

    HDU 4911 Inversion 考点:归并排序 思路:这题呀比赛的时候忘了知道能够用归并排序算出逆序数,可是忘了归并排序的实质了.然后不会做-- 由于看到题上说是相邻的两个数才干交换的时候.感觉 ...

  2. XML和对象属性互转的基类

    本人做了一个XML和对象属性互转的基类,现在放上来有兴趣拿去看一下,用法很简单,声明一个BaseConversion的子类,该子类与你想转换的对象相对应,然后覆盖基类的两个虚方法,然后在里面写元素与对 ...

  3. KMP 知识点总结

    KMP算法是BF算法的改进,主要是消除了主串指针的回溯,提高算法效率. 先简单介绍一下BF算法: 基本思路: 从目标串s的第一个字符开始和模式串的第一个字符比较,相等逐个比较后续字符,否则从目标串的第 ...

  4. ThinkPHP页面跳转、Ajax技巧详细介绍(十八)

    原文:ThinkPHP页面跳转.Ajax技巧详细介绍(十八) ThinkPHP页面跳转.Ajax技巧详细介绍 一.页面跳转 $this->success('查询成功',U('User/test' ...

  5. [Word使用笔记]分类简介

    什么Vistual Studio , Eclipse , Xcode , 都弱爆了,Word比他们难多了 - -! 此分类用于记录Word的一些使用

  6. 【矩阵乘】【NOI 2012】【cogs963】随机数生成器

    963. [NOI2012] 随机数生成器 ★★ 输入文件:randoma.in 输出文件:randoma.out 简单对照 时间限制:1 s 内存限制:128 MB **[问题描写叙述] 栋栋近期迷 ...

  7. POJ 3267-The Cow Lexicon(DP)

    The Cow Lexicon Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8252   Accepted: 3888 D ...

  8. acdream 1222 Quantization Problem [dp]

    称号:acdream 1222 Quantization Problem 题意:给出一个序列 a ,然后给出一个 n * m 的矩阵,让你从这个矩阵中选出一个序列k,使得sum(abs(ki - ai ...

  9. leetcode 之 Permutation Sequence

    Permutation Sequence The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and ...

  10. 单片机实验: 三轴磁场模块 GY-271

    最近买了一块三轴磁场模块进行实验 名称:HMC5883L模块(三轴磁场模块) 型号:GY-271 使用芯片:HMC5883L 供电电源:3-5v 通信方式:IIC通信协议 测量范围:±1.3-8 高斯 ...