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 ...
随机推荐
- Infinite scroll has been called autopagerize, unpaginate, endless pages
http://www.infinite-scroll.com/ Infinite scroll has been called autopagerize, unpaginate, endless pa ...
- 你真的了解JAVA中与Webservice相关的规范和实现吗?
非常多人在项目中使用Webservice,仅仅是知道怎样公布Webservice,怎样调用Webservice,但真要论其来龙去脉,还真不一定清楚. 一切一切还要从我们伟大的sun公司规范说起. JA ...
- poj3468 A Simple Problem with Integers(线段树模板 功能:区间增减,区间求和)
转载请注明出处:http://blog.csdn.net/u012860063 Description You have N integers, A1, A2, ... , AN. You need ...
- php中实现快排与冒泡排序
快排 <?php function quicksort($str){ if(count($str)<=1) return $str;//如果个数不大于一,直接返回 $key=$str[0] ...
- Program1-1
package com.algo; import java.util.Random; import java.util.Scanner; public class P1_1 { static int ...
- 使用Java7提供Fork/Join框架
在Java7在.JDK它提供了多线程开发提供了一个非常强大的框架.这是Fork/Join框架.这是原来的Executors更多 进一步,在原来的基础上添加了并行分治计算中的一种Work-stealin ...
- 算法起步之Prim算法
原文:算法起步之Prim算法 prim算法是另一种最小生成树算法.他的安全边选择策略跟kruskal略微不同,这点我们可以通过一张图先来了解一下. prim算法的安全边是从与当前生成树相连接的边中选择 ...
- iphone开发中数据持久化之——模型对象归档(二)
在Cocoa世界中,术语“归档”是指另一种形式的序列化,它可以实现对任何对象的序列化.使用对模型对象进行归档的技术可以轻松将复杂的对象写入文件,然后再从中读取它们.只要在类中实现的每个属性都是标量(如 ...
- C++不支持Unicode,即使utf8
今天,字符串unicode我们已经不需要常理的理由,但是,一些有编程语言的悠久历史.这仍然是一个头疼. 尽管第三方库支持的假设,C++事实上没有真正有效地支持unicode.即使utf8.(注意:本文 ...
- 折扣&折让-看清实质的思考
折扣&折让 看清实质的思考 Author:zfive5(zidong) Email:zfive5@163.com 引子 有些东西,在没有人指点的情况,一时理解不了,就放一放,等到某一个契机到来 ...