Problem Description

In easier times, Farmer John's cows had no problems. These days, though, they have problems, lots of problems; they have P (1 ≤ P ≤ 300) problems, to be exact. They have quit providing milk and have taken regular jobs like all other good citizens. In fact, on a normal month they make M (1 ≤ M ≤ 1000) money.

Their problems, however, are so complex they must hire consultants to solve them. Consultants are not free, but they are competent: consultants can solve any problem in a single month. Each consultant demands two payments: one in advance (1 ≤ payment ≤ M) to be paid at the start of the month problem-solving is commenced and one more payment at the start of the month after the problem is solved (1 ≤ payment ≤ M). Thus, each month the cows can spend the money earned during the previous month to pay for consultants. Cows are spendthrifts: they can never save any money from month-to-month; money not used is wasted on cow candy.
Since the problems to be solved depend on each other, they must be solved mostly in order. For example, problem 3 must be solved before problem 4 or during the same month as problem 4.
Determine the number of months it takes to solve all of the cows' problems and pay for the solutions.

Input

Line 1: Two space-separated integers: M and P. 
Lines 2..P+1: Line i+1 describes problem i with two space-separated integers: Bi and Ai. Bi is the payment to the consult BEFORE the problem is solved; Ai is the payment to the consult AFTER the problem is solved.

Output

Line 1: The number of months it takes to solve and pay for all the cows' problems.

Sample Input

100 5

40 20
60 20
30 50
30 50
40 40

Sample Output

6

题意:有p个问题,每人每月解决一个问题,解决问题有两个代价,当月给a[i],第二月给b[i],然后每个月钱有m,求最快多久能解决所有问题。

思路:

假设j之前为上个月及之前完成的,j到i为在本月完成的,那么:

当 before[j-1]+a[j...i]<=m 成立时,最小值为 f[j-1]+1

当 before[j-1]+a[j...i]>m 成立时,最小值为 f[j-1]+2

Source Program

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstdlib>
#include<queue>
#include<set>
#include<vector>
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define N 1001
#define MOD 123
#define E 1e-6
using namespace std;
int a[N],b[N];
int before[N],f[N];
int main()
{int m,p;scanf("%d%d",&m,&p);for(int i=1;i<=p;i++)scanf("%d%d",&a[i],&b[i]);before[0]=INF;f[0]=1;for(int i=1;i<=p;i++){int cost_a=0,cost_b=0;f[i]=f[i-1]+2;before[i]=b[i];for (int j=i;j>=1;j--)//j之前为上个月及之前完成的,j到i为在本月完成的{cost_a+=a[j];cost_b+=b[j];if (cost_a > m||cost_b>m)//超出当月钱数,退出break;if (cost_a+before[j-1]<=m&&f[j-1]+1<=f[i]){if (f[j-1]+1==f[i])before[i]=min(before[i],cost_b);elsebefore[i]=cost_b;f[i]=f[j-1]+1;}if (cost_a+before[j-1]>m&&f[j-1]+2<=f[i]){if(f[j-1]+2==f[i])before[i]=min(before[i],cost_b);elsebefore[i]=cost_b;f[i]=f[j-1]+2;}}}printf("%d\n",f[p]);return 0;
}

Problem Solving(POJ-3265)相关推荐

  1. Bailian2734 十进制到八进制【入门】(POJ NOI0113-45)

    问题链接:POJ NOI0113-45十进制到八进制 2734:十进制到八进制 总时间限制: 1000ms 内存限制: 65536kB 描述 把一个十进制正整数转化成八进制. 输入 一行,仅含一个十进 ...

  2. Bailian2676 整数的个数【入门】(POJ NOI0105-11)

    问题链接:POJ NOI0105-11 整数的个数 2676:整数的个数 总时间限制: 1000ms 内存限制: 65536kB 描述 给定k(1 < k < 100)个正整数,其中每个数 ...

  3. Bailian4029 数字反转【进制】(POJ NOI0105-29)

    问题链接:POJ NOI0105-29 数字反转 4029:数字反转 总时间限制: 1000ms 内存限制: 65535kB 描述 给定一个整数,请将该数各个位上数字反转得到一个新数.新数也应满足整数 ...

  4. Bailian2735 八进制到十进制【入门】(POJ NOI0113-46)

    问题链接:POJ NOI0113-46 八进制到十进制 2735:八进制到十进制 总时间限制: 1000ms 内存限制: 65536kB 描述 把一个八进制正整数转化成十进制. 输入 一行,仅含一个八 ...

  5. 洛谷 P1919 【模板】A*B Problem升级版(FFT快速傅里叶)

    题目链接:P1919 [模板]A*B Problem升级版(FFT快速傅里叶) 题意 给定两个 \(n\) 位 \(10\) 进制整数 \(x\) 和 \(y\),求 \(x*y\). 思路 FFT ...

  6. [P1919 【模板】A*B Problem升级版(FFT快速傅里叶)(高精乘板子,FFT板子)

    P1919 [模板]A*B Problem升级版(FFT快速傅里叶) 分析: ​ 为什么高精乘可以用 FFT 优化??? ​ 众所周知 FFT 是解决多项式之间的乘法运算,将乘数的每一位看成多项式的系 ...

  7. Silver Cow Party (POJ - 3268 )

    Silver Cow Party (POJ - 3268 ) 这道题是我做的最短路专题里的一道题,但我还没做这个,结果比赛就出了,真是.......... 题目: One cow from each ...

  8. 吴昊品游戏核心算法 Round 7 —— 熄灯游戏AI(有人性的Brute Force)(POJ 2811)

    暴力分为两种,一种属于毫无人性的暴力,一种属于有人性 的暴力.前面一种就不说了,对于后面一种情况,我们可以只对其中的部分问题进行枚举,而通过这些子问题而推导到整个的问题中.我称之为有人性的Brute ...

  9. 【二分】Best Cow Fences(poj 2018)

    Best Cow Fences poj 2018 题目大意: 给出一个正整数数列,要你求平均数最大,长度不小于M的字串,结果乘1000取整 输入样例 10 6 6 4 2 10 3 8 5 9 4 1 ...

  10. 主席树学习小结(POJ 2104)

    在高中的时候就听到过主席树了,感觉非常高端,在寒假的时候 winter homework中有一题是查找区间第K大的树,当时就开始百度这种网上的博客,发现主席树看不懂,因为那个root[i],还有tx[ ...

最新文章

  1. 高手的习惯:pythonic风格代码
  2. pcre库文件的安装
  3. 免费Java高级工程师学习资源,使用指南
  4. python第10天(上)
  5. localStorage的过期时间设置的方法?
  6. Java 在 CMD 环境下编译
  7. 用Way.EntityDB进行Entity Framework Core数据库建模
  8. linux文件系统的管理方法,Linux学习笔记:2.文件系统的管理命令(2)
  9. 通过configSource提高web.config配置灵活性
  10. C#中设置webBrowser为可编辑模式
  11. windows 安装apex_Nvidia Apex安装
  12. c语言b20等于多少,C语言程序:温度DS19B20显示
  13. 如何跟成功的男人谈恋爱?
  14. Kubernetes-2018干货盘点
  15. 人脸生成:Beyond Face Rotation: Global and Local Perception GAN
  16. 181220每日一句
  17. 【转】【Python】Python网络编程
  18. 深度学习下的电商商品推荐
  19. 【STM32CubeMX安装】
  20. 写专门针对ios设备的css

热门文章

  1. Flink的设计与实现:集群资源管理
  2. 包教包会,7段代码带你玩转Python条件语句
  3. Python实操:手把手教你用Matplotlib把数据画出来
  4. oracle 嵌套游标慢,oracle 嵌套游标以及java,oracle的时间处理
  5. 漫话:如何给女朋友解释什么是删库跑路?
  6. 最牛逼的微服务架构方案,没有之一!
  7. 从3000米高空,一跃而下…
  8. 腾讯总监周颢:亿级用户微信采用的架构宝典
  9. mysql添加新用户 开放外网访问
  10. 架构设计基础:单服务.集群.分布式,基本区别和联系