Brush (III)
LightOJ - 1017Samir 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.
OutputFor each case print the case number and the maximum number of dusts Samir can clean using at most k moves.
Sample Input2
3 2 1
0 0
20 2
30 2
3 1 1
0 0
20 2
30 2
Sample OutputCase 1: 3
Case 2: 2
原博客地址题意
墙上有N个污点,知道它们的坐标(xi,yi)。现有一把宽度为w的刷子,将刷子固定在一个高度就可以沿着平行于x轴的方向刷除污点。总操作次数最多为k,求最多能够刷除掉多少污渍
分析
我们以刷子底部的y坐标来刻画刷子的位置。首先既然刷子会沿着平行x轴的方向刷出这个高度所有的污点,那么可以不管污点的x坐标。
先预处理一下,把污点高度从大到小排个序(因为我们是以刷子的下部作为刷子的位置,那么刷高处的点是不会影响到下面的点),求出以某个污点的高度作为刷子放置的位置能够刷掉上面多少个点,记录为wipe[i]
接下来DP,设状态:
状态转移方程:
dp[i][j]={dp[i−1][j](不刷i这个高度)dp[i−wipe[i]][j−1]+wipe[i](刷i这个高度,那么能被它刷到的高度都没必要
dp[i][j] = max(dp[i-1][j],dp[i-wipe[i]][j-1]+wipe[i]);
不刷 刷(从刷之前的上一个位置转移过来)
code:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int T,n,w,k;
int Y[200],wipe[200];
int dp[200][200];
int main(){
scanf("%d",&T);
int cas;
for(cas = 1; cas <= T; cas++){
scanf("%d%d%d",&n,&w,&k);
for(int i = 1; i <= n; i++){
int x;
scanf("%d%d",&x,&Y[i]);
}
sort(Y+1,Y+1+n,greater<int>());
memset(wipe,0,sizeof(wipe));
for(int i = 1; i <= n; i++){
for(int j = i; j >= 1 && Y[j] - Y[i] <= w; j--){
wipe[i]++;
}
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= k; j++){
dp[i][j] = max(dp[i-1][j],dp[i-wipe[i]][j-1]+wipe[i]);
}
}
printf("Case %d: %d\n",cas,dp[n][k]);
}
return 0;
}