zoukankan      html  css  js  c++  java
  • 1017

    1017 - Brush (III)

         PDF (English)StatisticsForum

    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一下, 技巧: dp[k][p], p 代表的是第几个点,而不是y轴的位置。

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm> 
    using namespace std;
    
    int y[50010];
    int dp[110][110];
    int main(){
        
        int T,n,w,K,temp,ans,counter,j;
        scanf("%d",&T);
        for(int t=1;t<=T;t++){
            
            memset(dp,0,sizeof(dp));
            scanf("%d%d%d",&n,&w,&K);
            for(int i=0;i<n;i++) scanf("%d%d",&temp,&y[i]);
            sort(y,y+n);
            ans = 0;
            for(int k=1;k<=K;k++){
                for(int i=0;i<n;i++){
                    counter = 0;
                    for(j=i;j>=0&&y[i]-y[j]<=w;j--){ // for之后的j 便是k-1 能够达到的最大下标 
                        counter++;
                    }
                    dp[k][i] = counter;
                    
                    for(int p=0;p<=j;p++) dp[k][i] = max(dp[k][i],counter+dp[k-1][p]);
                    ans = max(dp[k][i],ans);
                }
            }
            
            printf("Case %d: %d
    ",t,ans);
        }
        
        
        return 0;
    }
    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

  • 相关阅读:
    面试题目小结
    面试题目3
    C#中new和override区别
    [转]:存储过程与函数的区别
    [转载]:C# 面试题大全
    [转]:C++虚函数表解析
    【修订版】C#/ASP.Net 面试题及答案(1)
    [转载]:C# 中结构与类的区别
    [转载]:C#笔试题面试题锦集
    [转载]:SQL Server性能调优之执行计划深度剖析 第一节 浅析SQL执行的过程
  • 原文地址:https://www.cnblogs.com/yuanshixingdan/p/5538767.html
Copyright © 2011-2022 走看看