【POJ 3265】Problem Solving

Problem Solving

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 1645   Accepted: 675

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 AiBi 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

Hint

+------+-------+--------+---------+---------+--------+
|      | Avail | Probs  | Before  | After   | Candy  |
|Month | Money | Solved | Payment | Payment | Money  |
+------+-------+--------+---------+---------+--------+
| 1    | 0     | -none- | 0       | 0       | 0      |
| 2    | 100   | 1, 2   | 40+60   | 0       | 0      |
| 3    | 100   | 3, 4   | 30+30   | 20+20   | 0      |
| 4    | 100   | -none- | 0       | 50+50   | 0      |
| 5    | 100   | 5      | 40      | 0       | 60     |
| 6    | 100   | -none- | 0       | 40      | 60     | 
+------+-------+--------+---------+---------+--------+

Source

USACO 2007 January Gold

一道简单的动态规划题目。

设f[n]为答案。

枚举j,表示j~i这几个问题在同一个月内完成(如果可以),那么很明显有两种情况,做完j-1个问题的最小月数是f[j-1],下个月要付before[j-1]的钱。

  (1)before[j-1] + sigma{a[k] | j <= k <= i} <= M,那么这样的最小值就是,f[j - 1]+1了;

  (2)before[j-1] + sigma{a[k] | j <= k <= i} > M,那么很明显不能和上面的问题直接接上去了,f[j-1]+2;

时间复杂度O(N2)。

#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

int m, p, a[305], b[305];
int f[305], before[305];

int main()
{
    scanf("%d%d", &m, &p);
    for (int i = 1; i <= p; ++i)
        scanf("%d%d", &a[i], &b[i]);
    before[0] = a[0] = 300005;
    f[0] = 1;
    for (int i = 1; i <= p; ++i)
    {
        int suma = 0, sumb = 0;
        f[i] = f[i - 1] + 2;
        before[i] = b[i];
        for (int j = i; j >= 1; --j)
        {
            suma += a[j];
            sumb += b[j];
            if (suma > m || sumb > m) break;
            if (suma + before[j - 1] <= m && f[j - 1] + 1 <= f[i])
            {
                if (f[j - 1] + 1 == f[i]) before[i] = min(before[i], sumb);
                else before[i] = sumb;
                f[i] = f[j - 1] + 1;
            }
            if (suma + before[j - 1] > m && f[j - 1] + 2 <= f[i])
            {
                if (f[j - 1] + 2 == f[i]) before[i] = min(before[i], sumb);
                else before[i] = sumb;
                f[i] = f[j - 1] + 2;
            }
        }
    }
    printf("%d\n", f[p]);
    return 0;
}
时间: 2024-10-21 02:07:05

【POJ 3265】Problem Solving的相关文章

【POJ 2480】Longge&#39;s problem(欧拉函数)

题意 求$ \sum_{i=1}^n gcd(i,n) $ 给定 $n(1\le n\le 2^{32}) $. 链接 分析 用欧拉函数$φ(x)$求1到x-1有几个和x互质的数. gcd(i,n)必定是n的一个约数.若p是n的约数,那么gcd(i,n)==p的有$φ(n/p)$个数,因为要使gcd(i,n)==p,i/p和n/p必须是互质的.那么就是求i/p和n/p互质的i在[1,n]里有几个,就等价于,1/p,2/p,...,n/p里面有几个和n/p互质,即φ(n/p). 求和的话,约数为p

【POJ 2777】Count Color

[POJ 2777]Count Color Description Chosen Problem Solving and Program design as an optional course, you are required to solve all kinds of problems. Here, we get a new problem. There is a very long board with length L centimeter, L is a positive integ

2292: 【POJ Challenge 】永远挑战

2292: [POJ Challenge ]永远挑战 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 553  Solved: 230[Submit][Status][Discuss] Description lqp18_31和1tthinking经常出题来虐ftiasch.有一天, lqp18_31搞了一个有向图,每条边的长度都是1. 他想让ftiasch求出点1到点 N 的最短路."水题啊.", ftiasch这么说道. 所以1tth

【POJ 2942】Knights of the Round Table(双联通分量+染色判奇环)

[POJ 2942]Knights of the Round Table(双联通分量+染色判奇环) Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 11661   Accepted: 3824 Description Being a knight is a very attractive career: searching for the Holy Grail, saving damsels in distress, an

【POJ 2482】 Stars in Your Window(线段树+离散化+扫描线)

[POJ 2482] Stars in Your Window(线段树+离散化+扫描线) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11294   Accepted: 3091 Description Fleeting time does not blur my memory of you. Can it really be 4 years since I first saw you? I still remembe

【POJ 3440】 Coin Toss(概率公式)

[POJ 3440] Coin Toss(概率公式) Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3591   Accepted: 957 Description In a popular carnival game, a coin is tossed onto a table with an area that is covered with square tiles in a grid. The prizes ar

【POJ 3071】 Football(DP)

[POJ 3071] Football(DP) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4350   Accepted: 2222 Description Consider a single-elimination football tournament involving 2n teams, denoted 1, 2, -, 2n. In each round of the tournament, all tea

【POJ 1698】Alice&#39;s Chance(二分图多重匹配)

http://poj.org/problem?id=1698 电影和日子匹配,电影可以匹配多个日子. 最多有maxw*7个日子. 二分图多重匹配完,检查一下是否每个电影都匹配了要求的日子那么多. #include<cstdio> #include<cstring> #include<algorithm> #define N 360 #define M 30 using namespace std; int t,n,m; int g[N][M]; int linker[M

BZOJ 2287 【POJ Challenge】消失之物

2287: [POJ Challenge]消失之物 Description ftiasch 有 N 个物品, 体积分别是 W1, W2, ..., WN. 由于她的疏忽, 第 i 个物品丢失了. “要使用剩下的 N - 1 物品装满容积为 x 的背包,有几种方法呢?” -- 这是经典的问题了.她把答案记为 Count(i, x) ,想要得到所有1 <= i <= N, 1 <= x <= M的 Count(i, x) 表格. Input 第1行:两个整数 N (1 ≤ N ≤ 2