贪心 poj2586

Y2K Accounting Bug

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10537   Accepted: 5264

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题目意思:MS公司在1999年每个月有盈余有亏损,现在只知道盈余一定为s,亏损一定为d,并且该公司每连续五个月的总利润都是亏损(0-4,1-5,...7-11)(编号0-11),问如何取值(s,-d)在该条件下使得总利润最大,并且询问总利润是否可能为正数(盈利)思路:贪心,一开始是统计需要次数最多的那个点,但是那样容易造成断层,(比如第二组数据375 473,5 6 7 8都是被需要次数相同的,这个算法就不知道该先选择哪个,可能会先选择5,6,而4是一定要选的,选了4就不用选6了)改变贪心思路,初始化全部选s,从头往尾扫,每个sum都必须变成-的为止,那么选择最右边的大于0,因为左边的要不就已经变成-的,或者使用次数没有右边多,直到sum变成负数为止

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int s,d;
int a[12];
long long sum[8];
int main()
{
    while(scanf("%d%d",&s,&d)==2){
        for(int i=0;i<11;i++)a[i]=s;
        for(int i=0;i<8;i++)sum[i]=5*s;
        for(int i=0;i<8;i++){
                int t=4;
                while(sum[i]>=0){
                    a[i+t]=-d;
                    for(int j=max(i+t-4,0);j<8&&j<=i+t;j++){
                        sum[j]-=d+s;
                    }
                    t--;
                }
        }
        long long ans=sum[0]+sum[7]+a[5]+a[6];
        if(ans>=0)printf("%I64d\n",ans);
        else printf("Deficit\n");
    }
    return 0;
}

时间: 2024-08-26 01:26:29

贪心 poj2586的相关文章

poj2586 Y2K Accounting Bug(贪心)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=2586 Language: Default Y2K Accounting Bug Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9979   Accepted: 4970 Description Accounting for Computer

POJ3069 POJ2586 解题报告(异曲同工的贪心算法)

[POJ 3069](2586见下) 原题在此:http://poj.org/problem?id=3069 题目大意: 一个直线上有N个点.点i的距离是Xi.从这些点中选取若干个加上标记.要求:对于每个点,与其距离为R的范围内必有做标记的点(包括自身).求至少标记多少点才能满足要求. 输入:N, R,以及N个点各自距原点的距离(①不一定按照顺序,故需要排序 ②可以重叠). 输出:被标记的点的最少个数. 解题思路: 从最左边开始看,在距离为R的范围内,被标记的点一定在第一个点的右侧(或是自身).

阶段性总结-贪心算法

### 贪心算法总结 ##poj1328> 贪心算法使用点:> 雷达覆盖距离最大为d 的岛屿,也就是以岛屿为圆心,d为半径与海岸线的相交的区间为该雷达的可在范围> 尽可能少的雷达:每个岛屿都有一个上述的圆和一个雷达可在的区间范围,区间范围重叠的岛屿可共用一个雷达 ##poj1700>贪心算法使用点:A:船从right side 返回left side时速度是最快的/次快 > B:船从left side 到right side,再返回,来回的总时间最短这道题需注意 贪心策略有两

【uva 1615】Highway(算法效率--贪心 区间选点问题)

题意:给定平面上N个点和一个值D,要求在x轴上选出尽量少的点,使得对于给定的每个店,都有一个选出的点离它的欧几里德距离不超过D. 解法:先把问题转换成模型,把对平面的点满足条件的点在x轴的直线上可得到一个个区间,这样就是选最小的点覆盖所有的区间的问题了.我之前的一篇博文有较详细的解释:关于贪心算法的经典问题(算法效率 or 动态规划).代码实现我先空着.挖坑~

【贪心+Treap】BZOJ1691-[Usaco2007 Dec]挑剔的美食家

[题目大意] 有n头奶牛m种牧草,每种牧草有它的价格和鲜嫩度.每头奶牛要求它的牧草的鲜嫩度要不低于一个值,价格也不低于一个值.每种牧草只会被一头牛选择.问最少要多少钱? [思路] 显然的贪心,把奶牛和牧草都按照鲜嫩度由大到小排序,对于每奶牛把鲜嫩度大于它的都扔进treap,然后找出后继. 不过注意后继的概念是大于它且最小的,然而我们这里是可以等于的,所以应该是找cow[i].fresh-1的后继,注意一下…… 1 #include<iostream> 2 #include<cstdio&

POJ1017 Packets(贪心算法训练)

Time Limit: 1000MS          Memory Limit: 10000K          Total Submissions: 51306          Accepted: 17391 Description A factory produces products packed in square packets of the same height h and of the sizes 1*1, 2*2, 3*3, 4*4, 5*5, 6*6. These pro

ZOJ 3946 Highway Project 贪心+最短路

题目链接: http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=3946 题解: 用dijkstra跑单元最短路径,如果对于顶点v,存在一系列边(ui,v)使得dis[v]最小(dis[v]表示0到v的距离).这些边能且只能选一条,那么我们自然应该选cost最小的那个边了. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #inc

CoderForce 140C-New Year Snowmen(贪心)

题目大意:有n个已知半径的雪球.堆一个雪人需要三个尺寸不同的雪球,问用这些雪球最多能堆多少个雪人? 题目分析:先统计一下每种尺寸的球的个数,从三种最多的种类中各取出一个堆成雪人,这样贪心能保证的到的数目最多. 代码如下: # include<iostream> # include<map> # include<vector> # include<cstdio> # include<queue> # include<algorithm>

计蒜客 跳跃游戏(贪心)

给定一个非负整数数组,假定你的初始位置为数组第一个下标.数组中的每个元素代表你在那个位置能够跳跃的最大长度. 请确认你是否能够跳跃到数组的最后一个下标. 例如: A = [2,3,1,1,4], return ture A = [3,2,1,0,4], return false. 格式: 第一行输入一个正整数n,接下来的一行,输入数组A[n].如果能跳到最后一个下标,输出"true",否则输出"false" 样例1 ????输入:???? ????????5 ???