Alice’s Stamps_dp_2018_3_8

时间:2023-01-30 16:23:35
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.
1T100
1KM
1N,M2000
1LiRiN
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.

        
 


#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N(2010);
int dp[N][N];
struct AA{
	int l,r;
}a[N];

bool cmp(AA a,AA b){
	return a.l<b.l;
}

int main(){
	int T;
	scanf("%d",&T);
	for(int z=1;z<=T;z++){
		int n,m,k;
		scanf("%d%d%d",&n,&m,&k);
		memset(dp,0,sizeof(dp));
		for(int i=0;i<m;i++)
		scanf("%d%d",&a[i].l,&a[i].r);
		sort(a,a+m,cmp);
		int num=0,pos=0;
		for(int i=0;i<n;i++){
			while(pos<m&&a[pos].l==i+1){
				num=max(num,a[pos].r-a[pos].l+1);
				pos++;
			}
			for(int j=0;j<=k;j++){
				dp[i+1][j]=max(dp[i+1][j],dp[i][j]);
				dp[i][j+1]=max(dp[i][j+1],dp[i][j]);
				dp[i+num][j+1]=max(dp[i][j]+num,dp[i+num][j+1]);
			}
			if(num)num--;
		}
		printf("Case #%d: %d\n",z,dp[n][k]);
	}
}