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. nginx相关关键词

    nginx在unix系统中会以daemon(守护进程)方式在后台运行; nginx默认模式是多线程的; nginx会有一个master进程和多个worker进程,master进程主要用来管理worke ...

  2. laravel查看sql语句

    我自己是用第一种方法来调试的,第三种不行 不知道为啥 laravel查看sql语句 方法一: 我们有时候想测试一段代码生产的 SQL 语句,比如: 我们想看 App\User::all(); 产生的 ...

  3. 【转】学习使用Jmeter做压力测试(一)--压力测试基本概念

    一.性能测试的概念 性能测试是通过自动化的测试工具模拟多种正常峰值及异常负载条件来对系统的各项性能指标进行测试.负载测试和压力测试都属于性能测试,两者可以结合进行. 通过负载测试,确定在各种工作负载下 ...

  4. java输入月份,年份,显示对应月份的天数,

    总结:1,输入月份,年份,这需要用Scanner   2.我们已知道12个月份的天数,有30天,31天   3.判断闰年 用switch -case-break语句  4.注意不要忘了写break;判 ...

  5. FTP,FTPS,FTPS与防火墙

    昨天搭建了一台FTPS服务器,过程中学习了很多不清楚的知识点,还有遇到的问题,记录一下. (大部分内容汇集.整理自网络) 一. 关于FTP传输模式 众所周知,FTP传输有两种工作模式,Active M ...

  6. AngularJS:Http

    ylbtech-AngularJS:Http 1.返回顶部 1. AngularJS XMLHttpRequest $http 是 AngularJS 中的一个核心服务,用于读取远程服务器的数据. 使 ...

  7. 第三方引擎应用场景分析--Tokudb,infobright

    TokuDBTokuDB的特色:• Fractal Tree而不是B-Tree• 内部结点不仅有指向父子的指针还有Buffer区,数据写入先写buffer区,FIFO结构,写入只需要顺序添加到Buff ...

  8. Linux内核中常见内存分配函数

    1.      原理说明 Linux内核中采用了一种同时适用于32位和64位系统的内存分页模型,对于32位系统来说,两级页表足够用了,而在x86_64系统中,用到了四级页表,如图2-1所示.四级页表分 ...

  9. whoosh----索引|搜索文本类库

    先了解基本概念和步骤: Quick Start Whoosh是一个索引文本和搜索文本的类库,他可以为你提供搜索文本的服务,比如如果你在创建一个博客的软件,你可以用whoosh为它添加添加一个搜索功能以 ...

  10. Project2--Lucene的Ranking算法修改:BM25算法

    原文出自:http://blog.csdn.net/wbia2010lkl/article/details/6046661 1.       BM25算法 BM25是二元独立模型的扩展,其得分函数有很 ...