【lightoj1017】DP


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


 

题意 :
给出污点数, 刷子宽度, 刷的次数, 求最多能刷掉几个污点。


思路:

跟01背包非常类似, 如果比较熟悉01背包的话应该会很快做出来(只可惜我不熟悉...orz)

背包中的dp[i][j]代表前i件物品最多装j单位重的最大价值,两个状态是选不选第i个物品。

类比一下这里dp[i][j]代表前i个污点刷j次的最多污点数(这里前i个污点是从下往上),也分刷不刷第i个污点。

dp[i][j] = max(dp[i - 1][j], dp[i - wipe[i]][j-1] + wipe[i]),当i < wipe[i] 时,dp[i][j] = dp[i - 1][j]

这里wipe[i]可以有两种理解,1. 刷第i个污点一共可以刷掉几个污点  2.刷第i个污点可以刷掉它前边的几个污点 。

区别就在于第i个污点和第i+1个污点如果在同一高度,就会导致wipe[i]的不同。但是状态方程都一样,两个都会AC

我这里按第一种写。


代码:

#include <bits/stdc++.h>
using namespace std;
const int N = 105;
int a[N], dp[N][N], wipe[N]; //a代表高度,wipe刷第i个污点一共可以刷掉几个污点
int main()
{
    int t, n, i, k, w, x, y;
    int cas = 0;
    cin>>t;
    while(t--)
    {
        memset(a, 0, sizeof a);
        memset(wipe, 0, sizeof wipe);
        scanf("%d%d%d", &n, &w, &k);
        {
            for(i = 1; i <= n; i++)
            {
                scanf("%d%d", &x, &y);
                a[i] = y;
            }
        }
        sort(a+1, a+1+n);
        int hign, low, j;
        for(i = 1; i <= n; i++)
        {
            for(j = i; j<=n+1; j++) //找到与i在同一高度的最后那个顶点,因为要算第i个污点一共可以刷掉几个污点
                if(a[j]!=a[i])
                    break;
            j--;
            for(int u = j; u>=1; u--)
                if(a[j] - a[u] <= w) wipe[i]++;
        }
        for(i = 1; i<=n; i++)
            for(j = 1; j<=k; j++)
            {
                if(i < wipe[i]) dp[i][j] = dp[i - 1][j];
                else 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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值