light oj 1017 - Brush (III)(背包->直线覆盖点的最大数量)

1017 - Brush (III)
Time Limit: 2 second(s)Memory Limit: 32 MB

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


思路:动态规划,设dp[i][j]表示在前j个dusts中用了i刷子刷掉dusts的个数:状态转移方程就是: dp[i][j] = max(dp[i][j-1], dp[i-1][j-len[j]] + len[j]);    len[j]表示刷第j个dust时需要覆盖其前面dusts的个数,可以在O(n)或O(n^2)的时间复杂度内预处理出来。

首先只要处理Y坐标就可以了,但是数据太大需要离散化,,,如果能想到用最长上升子序列把坐标封装起来的话(就是一种贪心思维)就可以用背包处理了。。。很难想


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int N = 110;
int dp[N][N], len[N], y[N];


int main()
{
    int t, ncase=1, n, w, x, k;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d %d %d", &n, &w, &k);
        for(int i=1;i<=n;i++)
        {
            scanf("%d %d", &x, &y[i]);
        }
        sort(y+1,y+n+1);
        memset(dp,0,sizeof(dp));
        memset(len,0,sizeof(len));
        for(int i=1;i<=n;i++)
        {
            for(int j=i;j>=1;j--)
            {
                if(y[i]-y[j]<=w)
                {
                    len[i]++;
                }
            }
        }
        for(int i=1;i<=k;i++)
        {
            for(int j=1;j<=n;j++)
            {
                dp[i][j]=max(dp[i][j-1],dp[i-1][j-len[j]]+len[j]);
            }
        }
        printf("Case %d: %d\n",ncase++,dp[k][n]);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值