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 题意:有一个公司,每个月或盈利或亏损,盈利都为s,亏损都为d,已每五个月(1-5月,2-6月,3-7月 etc.)中每个月的盈或亏加起来均小于0
求该公司能否盈利,如果能,输出盈利最大值,不能则输出"Deficit"
题解:如果五个月中有x个月赢,(5-x)个月亏,则应该满足约束x*s<d*(5-x)
期望求出盈利最大值,所以x要尽量大
然后就是求使每五个月中都有(5-x)个月亏所需要月份的最小值
自然可以手模,然而易错,所以可以打个程序暴搜一发
程序如下
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; int sta,ans=0x3f3f3f3f,target; void check(int x)
{
int flag=,tmp=,bit=,a[],cnt[],sum=;
memset(a,,sizeof(a));
memset(cnt,,sizeof(cnt));
while(x)
{
if(x%==)
{
tmp++;
a[bit]=;
}
bit++;
x>>=;
}
sum=a[]+a[]+a[]+a[]+a[];
for(int i=;i<=;i++)
{
if(sum<target)
{
flag=;
}
sum-=a[i];
sum+=a[i+];
}
if(sum<target)
{
flag=;
}
if(flag)
{
ans=min(ans,tmp);
}
} int main()
{
scanf("%d",&target);
for(int sta=;sta<=(<<)-;sta++)
{
check(sta);
}
printf("%d\n",ans);
}
接着,只需要根据上述结论打模拟即可.
代码如下:
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; int ans,tmp,d,s; int main()
{
int x=;
while(scanf("%d%d",&s,&d)==)
{
for(int i=; i>=; i--)
{
if(s*i<d*(-i))
{
x=i;
break;
}
}
x=-x;
if(x==)
{
ans=s*;
}
if(x==)
{
ans=s*-d*;
}
if(x==)
{
ans=s*-d*;
}
if(x==)
{
ans=s*-d*;
}
if(x==)
{
ans=s*-d*;
}
if(x==)
{
ans=-d*;
}
if(ans>=)
{
printf("%d\n",ans);
}
else
{
puts("Deficit");
}
}
} //0 0
//1 2
//2 4
//3 6
//4 9
//5 12
 


POJ - 2586 Y2K Accounting Bug (找规律)的更多相关文章

  1. 贪心 POJ 2586 Y2K Accounting Bug

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

  2. poj 2586 Y2K Accounting Bug

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

  3. poj 2586 Y2K Accounting Bug (贪心)

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

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

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

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

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

  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(贪心算法,水题一枚)

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

随机推荐

  1. 字符串(PHP学习)

    1.什么是字符串 答:一串字符组成(参考羊肉串) 2.字符串定义 答:单引号,双引号,包含单引号或双引号的字符串(1.双引号里面有单引号2.单引号里面有双引号3.转义4.字符拼接) 3.单双引号定义字 ...

  2. UE4事件相关总结

    转自:http://blog.ch-wind.com/ue4-event-overview/ 事件机制是实现游戏内逻辑的重要部分,在开始进行游戏逻辑的设计和实现之前,对UE4的事件机制进行理解是非常必 ...

  3. netty实现远程调用RPC功能

    netty实现远程调用RPC功能 依赖 服务端功能模块编写 客户端功能模块编写 netty实现远程调用RPC功能 PRC的功能一句话说白了,就是远程调用其他电脑的api 依赖 <dependen ...

  4. Python Twisted系列教程10:增强defer功能的客户端

    作者:dave@http://krondo.com/an-introduction-to-asynchronous-programming-and-twisted/ 译者:杨晓伟(采用意译) 可以从这 ...

  5. day3心得

    1. 集合 主要作用: 去重 关系测试, 交集\差集\并集\反向(对称)差集 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 >>&g ...

  6. 恒创科技 基于openStack云主机

    https://www.henghost.com/cloud-vps.shtml?s=gg&gclid=CKaXuOyr79UCFY-TvQodMJ8BCw

  7. leetcode216

    public class Solution { public IList<IList<int>> CombinationSum3(int k, int n) { , , , , ...

  8. Mycat实战之数据迁移(oracle -- mysql)

    1.案例场景: Mycat 后面接一个 Oracle 实例与一个 MySQL 实例,假设用户表,订单表,转账记录表, Oracle 字符集为 GBK 的,MySQL 字符集则要求 UTF8的 完成用户 ...

  9. 关于windows的jdk

    扯淡工作从web开发转向build automation,不知不觉已经一个半月了,各种学习熟悉build相关的流程和知识,比如ant, maven, jenkins 等CI技能.因为这一个半月学的东西 ...

  10. IP地址分类和子网划分

    IP地址: 地址范围                                  网络地址规律 子网掩码             私有地址       保留地址 A类地址:从1.0.0.0 到1 ...