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

  1. 59 237
  2. 375 743
  3. 200000 849694
  4. 2500000 8000000

Sample Output

  1. 116
  2. 28
  3. 300612
  4. Deficit
  5. 题意:有一个公司,每个月或盈利或亏损,盈利都为s,亏损都为d,已每五个月(1-5月,2-6月,3-7 etc.)中每个月的盈或亏加起来均小于0
    求该公司能否盈利,如果能,输出盈利最大值,不能则输出"Deficit"
    题解:如果五个月中有x个月赢,(5-x)个月亏,则应该满足约束x*s<d*(5-x)
    期望求出盈利最大值,所以x要尽量大
    然后就是求使每五个月中都有(5-x)个月亏所需要月份的最小值
    自然可以手模,然而易错,所以可以打个程序暴搜一发
    程序如下
  1. #include<cmath>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<iostream>
  5. #include<algorithm>
  6. using namespace std;
  7.  
  8. int sta,ans=0x3f3f3f3f,target;
  9.  
  10. void check(int x)
  11. {
  12. int flag=,tmp=,bit=,a[],cnt[],sum=;
  13. memset(a,,sizeof(a));
  14. memset(cnt,,sizeof(cnt));
  15. while(x)
  16. {
  17. if(x%==)
  18. {
  19. tmp++;
  20. a[bit]=;
  21. }
  22. bit++;
  23. x>>=;
  24. }
  25. sum=a[]+a[]+a[]+a[]+a[];
  26. for(int i=;i<=;i++)
  27. {
  28. if(sum<target)
  29. {
  30. flag=;
  31. }
  32. sum-=a[i];
  33. sum+=a[i+];
  34. }
  35. if(sum<target)
  36. {
  37. flag=;
  38. }
  39. if(flag)
  40. {
  41. ans=min(ans,tmp);
  42. }
  43. }
  44.  
  45. int main()
  46. {
  47. scanf("%d",&target);
  48. for(int sta=;sta<=(<<)-;sta++)
  49. {
  50. check(sta);
  51. }
  52. printf("%d\n",ans);
  53. }
  1. 接着,只需要根据上述结论打模拟即可.
    代码如下:
  1. #include<cmath>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<iostream>
  5. #include<algorithm>
  6. using namespace std;
  7. int ans,tmp,d,s;
  8. int main()
  9. {
  10. int x=;
  11. while(scanf("%d%d",&s,&d)==)
  12. {
  13. for(int i=; i>=; i--)
  14. {
  15. if(s*i<d*(-i))
  16. {
  17. x=i;
  18. break;
  19. }
  20. }
  21. x=-x;
  22. if(x==)
  23. {
  24. ans=s*;
  25. }
  26. if(x==)
  27. {
  28. ans=s*-d*;
  29. }
  30. if(x==)
  31. {
  32. ans=s*-d*;
  33. }
  34. if(x==)
  35. {
  36. ans=s*-d*;
  37. }
  38. if(x==)
  39. {
  40. ans=s*-d*;
  41. }
  42. if(x==)
  43. {
  44. ans=-d*;
  45. }
  46. if(ans>=)
  47. {
  48. printf("%d\n",ans);
  49. }
  50. else
  51. {
  52. puts("Deficit");
  53. }
  54. }
  55. }
  56. //0 0
  57. //1 2
  58. //2 4
  59. //3 6
  60. //4 9
  61. //5 12
  1.  

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. DLatch by Verilog

    //-----------------------------------------------------// Design Name : dlatch_reset// File Name   : ...

  2. 如何在已经安装好的Nginx上增加新模块

    学习资源: https://blog.csdn.net/dxm2025/article/details/41149865 https://blog.csdn.net/qq_36663951/artic ...

  3. php excel 设置单元格格式为文本格式

    学习源头:https://www.cnblogs.com/php-linux/p/6179442.html 解决 PHPExcel 长数字串显示为科学计数 在excel中如果在一个默认的格中输入或复制 ...

  4. 分布式缓存系统 Memcached 状态机之网络数据读取与解析

    整个状态机的基本流程如下图所示,后续分析将按该流程来进行. 接上节分解,主线程将接收的连接socket分发给了某工作线程,然后工作线程从任务队列中取出该连接socket的CQ_ITEM,开始处理该连接 ...

  5. zabbix snmp 协议监控 dell iRDAC

    转摘:http://blog.csdn.net/wanglei_storage/article/details/52789921 http://blog.csdn.net/wanglei_storag ...

  6. STL的一些技巧函数使用

    1.emplace() 函数和 emplace_back() 函数 C++11的STL中新增加了emplace() 函数和 emplace_back() 函数,用来实现insert() 函数和 pus ...

  7. babel-polyfill使用简介

    babel-polyfill介绍 简介 使用这个插件你可随心所欲的使用es6甚至更高版本的方法,这个插件自动转码 安装 这个插件必须在你的源码运行之前运行,所以必须安装成dependency npm ...

  8. temp3

  9. 优化深度神经网络(三)Batch Normalization

    Coursera吴恩达<优化深度神经网络>课程笔记(3)-- 超参数调试.Batch正则化和编程框架 1. Tuning Process 深度神经网络需要调试的超参数(Hyperparam ...

  10. STM32用有源蜂鸣器实现闹钟的声响

    有源蜂鸣器的声音是固定的,工作电压恒定,改变通断电的时间获得不同时长的音响,譬如连续音.快速短音.慢速长音(类似莫尔斯电报)来区分不同的报警信息. 简单的说,有源蜂鸣器只能发出一种声音,因为它的频率是 ...