zoukankan      html  css  js  c++  java
  • CodeForces 946D Timetable (DP)

    题意:给定 n,m,K,表示某个人一个周有 n 天,每天有 m 节课,但是他可以跳过 K 节课,然后下面每行一个长度为 m 个01字符串,0 表示该人在这一小时没有课,1 表示该人在这一个小时有课,每天的在学校时间是从开始上的第一节课,到上完最后一节课,问你他在校时间最短是多少。

    析:首先要预处理出来他第 i 天跳过 j 节课在校的最短时间dp[i][j],因为每一天都是独立的,然后就可以使用动态规划来求解,f[i][j] 表示前 i 天跳过 j 节课的最短在校时间,f[i][j] = min{f[i-1][k] + dp[i][j-k]]},很容易就得到了,但是这个题我主要被卡在预处理上了,我开始使用贪心来解以为每次都从左边或者右边去掉 1,每次去掉间隔最大的,如果相等就继续向前比较,直到不相等为止,但这个思想是错的,有一个错误数据就是 K = 2  0110001101  这个串,对于K = 2 来说,应该是 4 ,但是用信心做却是 6,是不正确的,所以我们反向考虑,每次肯定是从左边或者右边去掉 1,最后剩下的肯定是连续的一部分,我们只要枚举所有剩下的一部分长度最短的就是答案。

    代码如下:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <cmath>
    #include <stack>
    #include <sstream>
    #include <list>
    #include <assert.h>
    #include <bitset>
    #include <numeric>
    #define debug() puts("++++")
    #define gcd(a, b) __gcd(a, b)
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define fi first
    #define se second
    #define pb push_back
    #define sqr(x) ((x)*(x))
    #define ms(a,b) memset(a, b, sizeof a)
    #define sz size()
    #define be begin()
    #define en end()
    #define pu push_up
    #define pd push_down
    #define cl clear()
    #define lowbit(x) -x&x
    //#define all 1,n,1
    #define FOR(i,n,x)  for(int i = (x); i < (n); ++i)
    #define freopenr freopen("in.in", "r", stdin)
    #define freopenw freopen("out.out", "w", stdout)
    using namespace std;
    
    typedef long long LL;
    typedef unsigned long long ULL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const LL LNF = 1e17;
    const double inf = 1e20;
    const double PI = acos(-1.0);
    const double eps = 1e-10;
    const int maxn = 500 + 50;
    const int maxm = 1000 + 5;
    const LL mod = 1000000007;
    const int dr[] = {-1, 1, 0, 0, 1, 1, -1, -1};
    const int dc[] = {0, 0, 1, -1, 1, -1, 1, -1};
    const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
    int n, m;
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    inline bool is_in(int r, int c) {
      return r >= 0 && r < n && c >= 0 && c < m;
    }
    
    int g[maxn][maxn];
    int f[maxn][maxn];
    int dp[maxn][maxn];
    int a[maxn];
    string s;
    
    int main(){
      int K;
      cin >> n >> m >> K;
      ms(dp, INF);
      for(int i = 1; i <= n; ++i){
        cin >> s;
        int cnt = 0;
        for(int j = 0; j < m; ++j)  if(s[j] == '1')  a[cnt++] = j;
        if(cnt)  dp[i][0] = a[cnt-1] - a[0] + 1;
        else dp[i][0] = 0;
        dp[i][cnt] = 0;
        int idx = cnt;
        for(int j = 1; j < cnt; ++j) {
          dp[i][j] = a[cnt-j-1] - a[0] + 1;
          for(int k = 1; k + cnt - j - 1 < cnt; ++k){
            dp[i][j] = min(dp[i][j], a[cnt+k-j-1] - a[k] + 1);
          }
        }
      }
      
      for(int i = 1; i <= n; ++i){
        for(int j = 0; j <= K; ++j){
          f[i][j] = INF;
          for(int k = 0; k <= j; ++k)
            f[i][j] = min(f[i][j], f[i-1][k] + dp[i][j-k]);
        }
      }
      cout << f[n][K] << endl;
      return 0;
    }
    

      

  • 相关阅读:
    jquery获取元素各种宽高及页面宽高
    Mysql 多表联合查询效率分析及优化
    mysql连表操作是先连表还是先查询条件
    CSS实现垂直居中
    laravel中orderBy使用
    Spring官网改版后下载
    java.lang.NoClassDefFoundError: org/jaxen/JaxenException
    flex 简单跑马灯效果(竖着显示)
    Flex “跑马灯”效果(自定义“跑马灯”控件)
    当LinkButton无效时,光标不显示为手型
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/8637709.html
Copyright © 2011-2022 走看看