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
Output for Sample Input
2
3 2 1
0 0
20 2
30 2
3 1 1
0 0
20 2
30 2
Case 1: 3
Case 2: 2
这个题首先x坐标根本就没有用,而且因为最多一百个点
直接用n^2的方法存数据也完全没问题…
存完数据以后开始状态转移
这一类状态转移的关键在于一个步数….和位置上的转移
首先在x个地方动n次是被允许的
然后再x+1个地方动x+1次也是被允许的
那么不如设dp[a][b] 这个东西为在a地点动第b次的状态
只要每个都这么设那么所有的情况都能被覆盖掉
然后就转移状态,最后一步留给自己,其他的步数在前边找一个最大的。
三重循环稍微有点low…..
不过初学先这样吧….后边会想出好的优化方式吧
#include<iostream>
#include<memory.h>
#include<cstdio>
#include<algorithm>
#include<map>
using namespace std;
int n, w, k;
struct p
{
int san, quan;
bool operator< (const p &a)const {
return san < a.san;
}
};
p tu[120];
long long dp[101][101];
int main()
{
int T;
cin >> T;
int u = 0;
while (T--)
{
memset(tu, 0, sizeof(tu));
memset(dp, 0, sizeof(dp));
cin >> n >> w >> k;
int l, q;
int ww = 0;
for (int a = 1;a <= n;a++)
{
cin >> q >> l;
int b;
for (b = 1;b <= ww;b++)
{
if (l == tu[b].san)
{
tu[b].quan++;
break;
}
}
if (b > ww)tu[++ww] = { l,1 };
}
sort(tu + 1, tu + ww + 1);
for (int a = 1;a <= n;a++)
{
for (int b = 1;b <= k;b++)
{
long long sum = 0;
int c;
for (c = 0;c < a&&tu[a].san-tu[c].san>w;c++)
{
sum = max(sum, dp[c][b - 1]);
}
for (int d = c;d <= a;d++)
{
sum += tu[d].quan;
}
dp[a][b] = sum;
}
}
long long ss = 0;
for (int a = 1;a <= ww;a++)
{
ss = max(ss, dp[a][k]);
}
printf("Case %d: %lld\n", ++u, ss);
}
return 0;
}