zoukankan      html  css  js  c++  java
  • LA 4490

    Bubu's bookshelf is in a mess! Help him!

    There are n books on his bookshelf. We define the mess value to be the number of segments of consecutive equal-height books. For example, if the book heights are 30, 30, 31, 31, 32, the mess value is 3, that of 30, 32, 32, 31 is also 3, but the mess value of 31, 32, 31, 32, 31 is 5 -- it's indeed in a mess!

    Bubu wants to reduce the mess value as much as possible, but he's a little bit tired, so he decided to take out at most k books, then put them back somewhere in the shelf. Could you help him?

    Input 

    There will be at most 20 test cases. Each case begins with two positive integers n and k (1$ le$k$ le$n$ le$100), the total number of books, and the maximum number of books to take out. The next line contains n integers, the heights of each book, from left to right. Each height is an integer between 25 and 32, inclusive. The last test case is followed by n = k = 0, which should not be processed.

    Output 

    For each test case, print the case number and the minimal final mess value. Print a blank line after the output of each test case.

    Sample Input 

    5 2 
    25 25 32 32 25 
    5 1 
    25 26 25 26 25 
    0 0
    

    Sample Output 

    Case 1: 2 
    
    Case 2: 3

    dp[s][k][r] 代表当前取出K本书剩余s的集合以编号为R的书本作为尾的最小散乱度
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <bitset>
     6 
     7 using namespace std;
     8 
     9 #define read() freopen("sw.in", "r", stdin)
    10 
    11 const int MAX = 105;
    12 const int INF = 1e6;
    13 int N, K;
    14 int a[105];
    15 int dp[2][1 << 8][100][8];
    16 int ALL;
    17 
    18 void solve() {
    19         int last = 0, now = 1;
    20         memset(dp[0], -1, sizeof(dp[0]));
    21         memset(dp[1], -1, sizeof(dp[1]));
    22         //printf("%d
    ", dp[0][3][4]);
    23         dp[0][0][0][0] = 0;
    24 
    25         for (int i = 0; i < N; ++i) {
    26                 for (int s = 0; s < (1 << 8); ++s) {
    27                         for (int k = 0; k <= min(i + 1, K); ++k)
    28                             for (int r = 0; r <= 8; ++r) {
    29                                     if (r && !(s >> (r - 1) & 1)) continue;
    30                                 if (k - 1 >= 0 && dp[last][s][k - 1][r] != -1 && ( dp[now][s][k][r] == -1 || dp[now][s][k][r] >
    31                                                             dp[last][s][k - 1][r] ))
    32                                 dp[now][s][k][r] = dp[last][s][k - 1][r];
    33                                 if (dp[last][s][k][r] != -1 && (dp[now][s | (1 << (a[i] - 25))][k][a[i] - 25 + 1] == -1 ||
    34                                                              dp[now][s | (1 << (a[i] - 25))][k][a[i] - 25 + 1] > dp[last][s][k][r] + ((a[i] - 25 + 1) != r))) {
    35                                         dp[now][s | (1 << (a[i] - 25))][k][a[i] - 25 + 1] = dp[last][s][k][r] + ((a[i] - 25 + 1) != r);
    36                                 }
    37                                 //printf("i = %d s = %d k = %d r = %d %d
    ", i, s, k, r, dp[now][s][k][r]);
    38                         }
    39 
    40                 }
    41                 swap(last, now);
    42                 memset(dp[now], -1, sizeof(dp[now]));
    43         }
    44 
    45         int ans = INF;
    46         for (int i = 0; i < (1 << 8); ++i) {
    47                 for (int k = 0; k <= K; ++k)
    48                     for (int r = 1; r <= 8; ++r) {
    49                         //printf("%d 
    ", dp[last][i][k]);
    50                         if (dp[last][i][k][r] == -1) continue;
    51                         //printf("%d
    ", dp[last][i][k]);
    52                         int t = ALL ^ i;
    53                         bitset <12> v(t);
    54                         if (ans > v.count() + dp[last][i][k][r])
    55                         ans = v.count() + dp[last][i][k][r];
    56 
    57                 }
    58         }
    59 
    60         printf("%d
    
    ", ans);
    61 }
    62 int main()
    63 {
    64    // read();
    65     int ca = 1;
    66     while (scanf("%d%d", &N, &K) != EOF && (N || K)) {
    67             ALL = 0;
    68             for (int i = 0; i < N; ++i) {
    69                     scanf("%d", &a[i]);
    70                     ALL |= 1 << (a[i] - 25);
    71             }
    72             //printf("ALL = %d
    ", ALL);
    73             printf("Case %d: ", ca++);
    74             solve();
    75 
    76     }
    77     //cout << "Hello world!" << endl;
    78     return 0;
    79 }
    View Code
  • 相关阅读:
    asp.net后台导出excel的方法:使用response导出excel
    asp.net后台导出excel的方法一:使用response导出excel
    infragistics--web网站升级注意点
    infragistics--网站部署时webtab的tab前出现textbox
    Jewels and Stones
    To Lower Case
    Unique Email Addresses
    unique-morse-code-words
    很久没来博客园了。。。。
    Centos7 基础知识---------root文件夹下没有.ssh文件
  • 原文地址:https://www.cnblogs.com/hyxsolitude/p/3849567.html
Copyright © 2011-2022 走看看