1. 程式人生 > >hdu6249 Alice’s Stamps(dp好題!)

hdu6249 Alice’s Stamps(dp好題!)

Alice’s Stamps

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1165    Accepted Submission(s): 415  

Problem Description

Alice likes to collect stamps. She is now at the post office buying some new stamps. There are N different kinds of stamps that exist in the world; they are numbered 1 through N

. However, stamps are not sold individually; they must be purchased in sets. There are M different stamp sets available; the ith set contains the stamps numbered Li through Ri. The same stamp might appear in more than one set, and it is possible that one or more stamps are not available in any of the sets. All of the sets cost the same amount; because Alice has a limited budget, she can buy at most K
different sets. What is the maximum number of different kinds of stamps that Alice can get?

Input

The input starts with one line containing one integer T, the number of test cases.T test cases follow. Each test case begins with a line containing three integers: N, M, and K: the number of different kinds of stamps available, the number of stamp sets available, and the maximum number of stamp sets that Alice can buy.M

lines follow; the ithoftheselinesrepresentsthei^{th} stamp set and contains two integers, Li and Ri, which represent the inclusive range of the numbers of the stamps available in that set. 1≤T≤100 1≤KM 1≤N,M≤2000 1≤LiRiN

Output

For each test case, output one line containing “Case #x: y”, where x is the test case number (starting from 1) and y is the maximum number of different kinds of stamp that Alice could get.

Sample Input

2 5 3 2 3 4 1 1 1 3 100 2 1 1 50 90 100

Sample Output

Case #1: 4 Case #2: 50

Hint

In sample case #1, Alice could buy the first and the third stamp sets, which contain the first four kinds of stamp. Note that she gets two copies of stamp 3, but only the number of different kinds of stamps matters, not the number of stamps of each kind. In sample case #2, Alice could buy the first stamp set, which contains 50 different kinds of stamps.

Source

Recommend

liuyiding   |   We have carefully selected several similar problems for you:  6447 6446 6445 6444 6443 

我又一次對肥宅產生了深深的崇拜。有的人呢,沒有見過的型別是想不到的。絞盡腦汁可能也找不到正確方向。找到了正確方向可能也想不出來。譬如我。有的人呢,使勁想想或許就能想出來。邊玩邊想,譬如紀大佬。

偶爾誇一下隊友(因為不是很經誇)。如果隊友有我一半勤奮,我有隊友一半的睿智,就完美了。

題意:

題意很簡單,就是給了m段區間,問從裡面選出來k段,使得覆蓋的面積最廣,輸出覆蓋的最大面積。

思路:

用ri[i]代表從i開始最長的那一段能夠覆蓋的最靠右的值。

dp[i][j]表示,到了座標i,此時選了j個所能覆蓋的最大值。

狀態轉移方程詳見程式碼。

看了題解覺得很簡單。重點是想不到。

程式碼:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define ll long long
#define mod 1000000007
#define inf 1e9
#define mem(a,x) memset(a,x,sizeof(a))
using namespace std;


int dp[2020][2020];
int ri[2020];
int main()
{
    ios::sync_with_stdio(false);
    int t,cas=1;
    cin>>t;
    while(t--)
    {
        int n,m,k,i,j;
        mem(dp,0);
        mem(ri,0);
        cin>>n>>m>>k;
        while(m--)
        {
            int l,r;
            cin>>l>>r;
            while(l<=r)
            {
                ri[l]=max(ri[l],r);
                l++;
            }
        }
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=k;j++)
            {
                dp[i][j]=max(dp[i][j],dp[i-1][j]);
                dp[ri[i]][j]=max(dp[i-1][j-1]+ri[i]-i+1,dp[ri[i]][j]);
            }
        }
        int res=0;
        for(i=1;i<=n;i++)
        {
            res=max(dp[i][k],res);
        }
        cout<<"Case #"<<cas++<<": "<<res<<endl;
    }
    return 0;
}