题目大意:
- 已知一个公司在某一年中,【每个月要么固定盈利s、要么固定亏损d】。
- 但是具体哪个月盈利、那个月亏损却不得而知。
- 不过可以肯定的是,这一年中,【任意的连续5个月盈亏和必定是亏损(< 0)】。
- 问这年是否存在盈利的可能,若可能盈利,【最大的盈利额】是多少
题解:运用贪心的思想,将每连续5月的亏损最小,可以设每5个月,x个月盈利,y个月亏损;
列方程:s*x-y*s<0 == y>5*s/(d+s)
y最小等于5*s/(d+s)+1
代码如下:
#include<iostream> #include<algorithm> #include<cstdio> #include<queue> using namespace std; int main() { int s, d; int a[13]; while (~scanf("%d%d", &s, &d)) { int sum = 0; int M = 5 * s / (d + s); if (s >= 4*d) { cout << "Deficit" << endl; continue; } int y = M + 1; int x = 5 - y; for (int i = 1; i <= 5; i++) { if (i <= x) { a[i] = s; } else { a[i] = -d; } sum += a[i]; } for (int i = 6; i <= 12; i++) { if (a[i - 5] == s) a[i] = s; else a[i] = -d; sum += a[i]; } if (sum >= 0) cout << sum << endl; else cout << "Deficit" << endl; } return 0; }