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


分析:

    已知一年中,所有的连续的5个月是亏损的(如1-5月,2-6月。。。8-12月共8个),每个月要么收益s,要么亏损d,
求一年中最大收益。这数据量什么都不想了直接暴力搜索加个树状数组优化下算是用了点心了
#include <iostream>
#include <cstring>
using namespace std;
int f[13],ans,s,d;
void update(int x,int k){
for(;x<=12;x+=(-x)&x) f[x]+=k;
}
int sum(int x){
int s=0;
for(;x>0;x-=(-x)&x) s+=f[x];
return s;
}
void make(int k){
if(k==13) {
ans=max(sum(12),ans);
return;
}
update(k,s+d);
int i;
for(i=5;i<=12;i++){
if(sum(i)-sum(i-5)>0) break;
}
if(i<13) {
update(k,-s-d);
make(k+1);
}
else{
make(k+1);
update(k,-s-d);
make(k+1);
}
}
int main(){
while(cin>>s>>d) {
ans=-1e8;
memset(f,0,sizeof f);
for(int i=1;i<=12;i++) update(i,-d);
make(1);
if(ans<0) cout<<"Deficit"<<endl;
else
cout<<ans<<endl;
}
return 0;
}
http://www.cnblogs.com/keam37/ keam所有 转载请注明出处

POJ2586 Y2K Accounting Bug 解题报告的更多相关文章

  1. [POJ2586]Y2K Accounting Bug

    [POJ2586]Y2K Accounting Bug 试题描述 Accounting for Computer Machinists (ACM) has sufferred from the Y2K ...

  2. POJ2586——Y2K Accounting Bug

    Y2K Accounting Bug   Description Accounting for Computer Machinists (ACM) has sufferred from the Y2K ...

  3. poj2586 Y2K Accounting Bug(贪心)

    转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=2586 ------ ...

  4. POJ-2586 Y2K Accounting Bug贪心,区间盈利

    题目链接: https://vjudge.net/problem/POJ-2586 题目大意: MS公司(我猜是微软)遇到了千年虫的问题,导致数据大量数据丢失.比如财务报表.现在知道这个奇特的公司每个 ...

  5. POJ2586 Y2K Accounting Bug(贪心)

    题目链接. 题目大意: 题目相当晦涩难懂啊. 一年的12个月,每5个月统计一次,如从1~5,2~6,3~7,...,8~12共统计了8次,已知每次都deficit,问这一年有没有盈利的可能. 一个月s ...

  6. poj2586 Y2K Accounting Bug —— 枚举

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

  7. Y2K Accounting Bug(贪心)

    Y2K Accounting Bug Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10945   Accepted: 54 ...

  8. 贪心 POJ 2586 Y2K Accounting Bug

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

  9. Y2K Accounting Bug 分类: POJ 2015-06-16 16:55 14人阅读 评论(0) 收藏

    Y2K Accounting Bug Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11222   Accepted: 56 ...

随机推荐

  1. call、apply/bind的区别和用法(简单粗暴的解释)

    var obj1={ name:"bob", age:20 } var obj2={ name:"coco", age:22 } function getAge ...

  2. Beta测试团队

    ---恢复内容开始--- Beta版本测试 这个作业属于哪个课程 https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass1/?page ...

  3. python常见问题一(安装报错)

    常见问题一:我在安装python2.7时,提示错误:'An error occurred during the installation of assembly 'Microsoft.VC90.CRT ...

  4. 【转】MFC 自定义edit 限制输入十六进制内容 响应复制粘贴全选剪切的功能

    参考地址:MFC 自定义edit 限制输入内容 响应复制粘贴全选剪切的功能   Ctrl组合键ASCII码 ^Z代表Ctrl+z                     ASCII值 控制字符  AS ...

  5. CocoaPods 命令和使用

    CocoaPods 命令 pod init 在新建的项目根目录下运行该命令,为当前项目新建podfile文件. pod install 下载和配置 podfile里定义的项目依赖(不包括已经下载和配置 ...

  6. 字符串匹配算法之BM算法

    BM算法,全称是Boyer-Moore算法,1977年,德克萨斯大学的Robert S. Boyer教授和J Strother Moore教授发明了一种新的字符串匹配算法. BM算法定义了两个规则: ...

  7. react-native Android WARNING: API 'variant.getMergeAssets()' is obsolete and has been replaced with 'variant.getMergeAssetsProvider()'.

    android Studio 中打开react-native项目的android文件夹 在sync的过程中 发生warning: WARNING: API 'variant.getMergeAsset ...

  8. 【牛客小白月赛6】 J 洋灰三角 - 快速幂&逆元&数学

    题目地址:https://www.nowcoder.com/acm/contest/136/J 解法一: 推数学公式求前n项和: 当k=1时,即为等差数列,Sn = n+pn(n−1)/2 当k≠1时 ...

  9. 51nod 1057 n的阶乘 (压位优化)

    题目地址:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1057&judgeId=605203 使用压位进行优化, ...

  10. Try, throw和catch用法

    PHP 5 提供了一种新的面向对象的错误处理方法. 使用思路如下: 1.Try - 使用异常的函数应该位于 "try" 代码块内.如果没有触发异常,则代码将照常继续执行.但是如果异 ...