lightoj 1017 - Brush (III) 【离散化 + dp】

题目链接:lightoj 1017 - Brush (III)

1017 - Brush (III)
PDF (English) Statistics Forum
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

题意:给你n个点,每次可以消去[y, y+w]的一段x区间上的所有点,问你k次最多可以消去多少个点。

思路:先离散化,将n个点变成N个数,求出相应的权值。设置dp[i][j]为前j个数用i次得到的最大权值,那么有
dp[i][j]=max(dp[i][j1],dp[i1][k]+sum(k+1,j))dis(k+1,j)<=w,sum(i,j)

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#define ll o<<1
#define rr o<<1|1
#define fi first
#define se second
#define CLR(a, b) memset(a, (b), sizeof(a))
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int INF = 0x3f3f3f3f;
const int MAXN = 3*1e5 + 10;
int dp[110][110];
int x[110], y[110], rec[110], num[110], sum[110], match[110];
int main()
{
    int t, kcase = 1;
    scanf("%d", &t);
    while(t--) {
        int n, w, k; scanf("%d%d%d", &n, &w, &k);
        int top = 1;
        for(int i = 1; i <= n; i++) {
            scanf("%d%d", &x[i], &y[i]);
            rec[top++] = y[i];
        }
        sort(rec+1, rec+top);
        int N = unique(rec+1, rec+top) - rec; CLR(num, 0);
        for(int i = 1; i <= n; i++) {
            int pos = lower_bound(rec+1, rec+N, y[i]) - rec;
            num[pos]++;
        }
        CLR(dp, 0); N--; rec[0] = rec[1]; sum[0] = 0;
        for(int i = 1; i <= N; i++) {
            int pos = lower_bound(rec+1, rec+i+1, rec[i] - w) - rec;
            match[i] = pos;
        }
        for(int i = 1; i <= N; i++) {
            sum[i] = sum[i-1] + num[i];
        }
        for(int i = 1; i <= k; i++) {
            for(int j = 1; j <= N; j++) {
                dp[i][j] = dp[i][j-1];
                for(int p = match[j]-1; p < j; p++) {
                    dp[i][j] = max(dp[i][j], dp[i-1][p] + sum[j] - sum[p]);
                }
            }
        }
        int ans = 0;
        for(int i = 1; i <= N; i++) {
            //cout << dp[k][i] << endl;
            ans = max(ans, dp[k][i]);
        }
        printf("Case %d: %d\n", kcase++, ans);
    }
    return 0;
}

你可能感兴趣的:(lightoj 1017 - Brush (III) 【离散化 + dp】)