Light OJ 1017 Brush (III)

Samir returned home from the contest and got angry after seeing his room dusty. Who likes to see a dusty room after a brain storming programming contest? After checking a bit he found a brush in his room which has width w. Dusts are defined as 2D points. And since they are scattered everywhere, Samir is a bit confused what to do. He asked Samee and found his idea. So, he attached a rope with the brush such that it can be moved horizontally (in X axis) with the help of the rope but in straight line. He places it anywhere and moves it. For example, the y co-ordinate of the bottom part of the brush is 2 and its width is 3, so the y coordinate of the upper side of the brush will be 5. And if the brush is moved, all dusts whose y co-ordinates are between 2 and 5 (inclusive) will be cleaned. After cleaning all the dusts in that part, Samir places the brush in another place and uses the same procedure. He defined a move as placing the brush in a place and cleaning all the dusts in the horizontal zone of the brush.

You can assume that the rope is sufficiently large. Since Samir is too lazy, he doesn't want to clean all the room. Instead of doing it he thought that he would use at most k moves. Now he wants to find the maximum number of dust units he can clean using at most k moves. Please help him.

Input

Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case starts with a blank line. The next line contains three integers N (1 ≤ N ≤ 100), w (1 ≤ w ≤ 10000) and k (1 ≤ k ≤ 100). N means that there are N dust points. Each of the next N lines contains two integers: xi yi denoting the coordinates of the dusts. You can assume that (-109 ≤ xi, yi ≤ 109) and all points are distinct.

Output

For each case print the case number and the maximum number of dusts Samir can clean using at most k moves.

Sample Input

2

3 2 1

0 0

20 2

30 2

3 1 1

0 0

20 2

30 2

Sample Output

Case 1: 3

Case 2: 2

题解:状态好想,就是不太好写,总想着标记,其实更换一下循环的顺序就行了。dp[ i ][ j ]表示在 j 位置时共用了 i 次刷子所消除的点的个数的最大值。

dp[ i ][ j ]=max { dp[ i - 1 ][ p ] | 1<=p<=这次刷的起点 }。注意 j 是刷子终止的位置,而不是起始位置。 

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 using namespace std;
 6 struct node{
 7     int x,y;
 8     bool operator<(const node& i)const{
 9         if(y==i.y) return x<i.x;
10         return y<i.y;
11     }
12 }p[105];
13 
14 int n,w,k;
15 int dp[105][105];
16 
17 int solve(){
18     int ans=0;
19     memset(dp,0,sizeof(dp));
20     for(int i=1;i<=k;i++){
21         for(int j=1;j<=n;j++){
22             int cnt=0,t,l;
23             for(t=j;t>=1&&p[j].y-p[t].y<=w;t--) cnt++;
24             dp[i][j]=cnt;
25             for(l=1;l<=t;l++) dp[i][j]=max(dp[i][j],dp[i-1][l]+cnt);
26             ans=max(ans,dp[i][j]);
27         }
28     }
29     return ans;
30 }
31 
32 int main()
33 {   int kase;
34     cin>>kase;
35     for(int t=1;t<=kase;t++){
36         cin>>n>>w>>k;
37         for(int i=1;i<=n;i++) scanf("%d%d",&p[i].x,&p[i].y);
38         sort(p+1,p+n+1);
39         printf("Case %d: %d
",t,solve());
40     }
41     return 0;
42 }
原文地址:https://www.cnblogs.com/zgglj-com/p/7307960.html