zoukankan      html  css  js  c++  java
  • [LightOJ1017]Brush (III)(dp)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1017

    题意:给你一个无限大的平面(2D)上面有N个 点,一个宽w的刷子,最多使用k次,每次使用只能横着水平刷一次。问刷k次后刷过的地方最多可以覆盖多少个点。

    思路:其实读完题就知道这个题和x坐标没关系,我们只需要关心每一个点的y坐标就行了。可以动态规划,输入完后给每一个点的y坐标从小到大排序,定义dp(i,k)为前i个点刷了k次后最大能刷到的点数。转移的时候有两种情况,一种是之前刷了k次了,这里不刷,即dp(i-1,k);另一种是从大到小枚举j从(i~1),找到这个刷子能覆盖到的最大宽度,更新dp(i,k)=max(dp(i,k), dp(j-1,k-1)+cnt)即可。

      1 /*
      2 ━━━━━┒ギリギリ♂ eye!
      3 ┓┏┓┏┓┃キリキリ♂ mind!
      4 ┛┗┛┗┛┃\○/
      5 ┓┏┓┏┓┃ /
      6 ┛┗┛┗┛┃ノ)
      7 ┓┏┓┏┓┃
      8 ┛┗┛┗┛┃
      9 ┓┏┓┏┓┃
     10 ┛┗┛┗┛┃
     11 ┓┏┓┏┓┃
     12 ┛┗┛┗┛┃
     13 ┓┏┓┏┓┃
     14 ┃┃┃┃┃┃
     15 ┻┻┻┻┻┻
     16 */
     17 #include <algorithm>
     18 #include <iostream>
     19 #include <iomanip>
     20 #include <cstring>
     21 #include <climits>
     22 #include <complex>
     23 #include <fstream>
     24 #include <cassert>
     25 #include <cstdio>
     26 #include <bitset>
     27 #include <vector>
     28 #include <deque>
     29 #include <queue>
     30 #include <stack>
     31 #include <ctime>
     32 #include <set>
     33 #include <map>
     34 #include <cmath>
     35 using namespace std;
     36 #define fr first
     37 #define sc second
     38 #define cl clear
     39 #define BUG puts("here!!!")
     40 #define W(a) while(a--)
     41 #define pb(a) push_back(a)
     42 #define Rint(a) scanf("%d", &a)
     43 #define Rll(a) scanf("%lld", &a)
     44 #define Rs(a) scanf("%s", a)
     45 #define Cin(a) cin >> a
     46 #define FRead() freopen("in", "r", stdin)
     47 #define FWrite() freopen("out", "w", stdout)
     48 #define Rep(i, len) for(int i = 0; i < (len); i++)
     49 #define For(i, a, len) for(int i = (a); i < (len); i++)
     50 #define Cls(a) memset((a), 0, sizeof(a))
     51 #define Clr(a, x) memset((a), (x), sizeof(a))
     52 #define Full(a) memset((a), 0x7f7f, sizeof(a))
     53 #define lrt rt << 1
     54 #define rrt rt << 1 | 1
     55 #define pi 3.14159265359
     56 #define RT return
     57 #define lowbit(x) x & (-x)
     58 #define onenum(x) __builtin_popcount(x)
     59 typedef long long LL;
     60 typedef long double LD;
     61 typedef unsigned long long ULL;
     62 typedef pair<int, int> pii;
     63 typedef pair<string, int> psi;
     64 typedef pair<LL, LL> pll;
     65 typedef map<string, int> msi;
     66 typedef vector<int> vi;
     67 typedef vector<LL> vl;
     68 typedef vector<vl> vvl;
     69 typedef vector<bool> vb;
     70 
     71 const int maxn = 110;
     72 
     73 int n, K, w;
     74 int dp[maxn][maxn];
     75 int y[maxn];
     76 
     77 int main() {
     78     // FRead();
     79     int T, _ = 1, x;
     80     Rint(T);
     81     W(T) {
     82         Rint(n); Rint(w); Rint(K);
     83         For(i, 1, n+1) {
     84             Rint(x); Rint(y[i]);
     85         }
     86         sort(y+1, y+n+1);
     87         Cls(dp);
     88         For(i, 1, n+1) {
     89             For(k, 1, K+1) {
     90                 dp[i][k] = dp[i-1][k];
     91                 int cnt = 0;
     92                 for(int j = i; j >= 1; j--) {
     93                     if(y[i] - y[j] > w) break;
     94                     cnt++;
     95                     dp[i][k] = max(dp[i][k], dp[j-1][k-1]+cnt);
     96                 }
     97             }
     98         }
     99         int ret = -1;
    100         For(i, 1, K+1) ret = max(dp[n][i], ret);
    101         printf("Case %d: %d
    ", _++, ret);
    102     }    
    103     RT 0;
    104 }
  • 相关阅读:
    Linq聚合操作之Aggregate,Count,Sum,Distinct源码分析
    Linq分区操作之Skip,SkipWhile,Take,TakeWhile源码分析
    Linq生成操作之DefautIfEmpty,Empty,Range,Repeat源码分析
    Linq基础操作之Select,Where,OrderBy,ThenBy源码分析
    PAT 1152 Google Recruitment
    PAT 1092 To Buy or Not to Buy
    PAT 1081 Rational Sum
    PAT 1084 Broken Keyboard
    PAT 1077 Kuchiguse
    PAT 1073 Scientific Notation
  • 原文地址:https://www.cnblogs.com/kirai/p/5579252.html
Copyright © 2011-2022 走看看