zoukankan      html  css  js  c++  java
  • UVA10003 Cutting Sticks

    https://odzkskevi.qnssl.com/e17d84412b7ea3a42b6503109d6dfbc1?v=1508053531

    【题解】

    裸区间dp,注意一堆细节

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 #include <cstdlib>
     5 #include <cstring>
     6 #include <cmath>
     7 #define max(a, b) ((a) > (b) ? (a) : (b))
     8 #define min(a, b) ((a) < (b) ? (a) : (b))
     9 inline void swap(int &a, int &b)
    10 {
    11     int tmp = a;a = b;b = tmp;
    12 }
    13 inline void read(int &x)
    14 {
    15     x = 0;char ch = getchar(), c = ch;
    16     while(ch < '0' || ch > '9')c = ch, ch = getchar();
    17     while(ch <= '9' && ch >= '0')x = x * 10 + ch - '0', ch = getchar();
    18     if(c == '-')x = -x;
    19 }
    20 
    21 const int INF = 0x3f3f3f3f;
    22 const int MAXN = 50 + 10;
    23 
    24 int dp[MAXN][MAXN], po[MAXN], n, L;
    25 
    26 int main()
    27 {
    28     while(scanf("%d", &L) != EOF && L)
    29     {
    30         read(n);
    31         for(register int i = 1;i <= n;++ i)
    32             read(po[i]);
    33         std::sort(po + 1, po + 1 + n);
    34         for(register int i = 0;i <= n + 1;++ i) 
    35             dp[i][i] = 0, dp[i][i + 1] = 0;
    36         po[n + 1] = L;
    37         for(register int k = 3;k <= n + 2;++ k)
    38         {
    39             for(register int i = 0;i <= n + 1;++ i)
    40             {
    41                 int j = i + k - 1;
    42                 dp[i][j] = INF;
    43                 if(j > n + 1)continue;
    44                 for(register int m = i + 1;m < j;++ m)
    45                     dp[i][j] = min(dp[i][j], dp[i][m] + dp[m][j] + po[j] - po[i]);
    46             }
    47         }
    48         printf("The minimum cutting is %d.
    ", dp[0][n + 1]);
    49     }
    50     return 0;
    51 }                 
    UVA10003
  • 相关阅读:
    P3368 【模板】树状数组 2
    P3374 【模板】树状数组 1
    P1631 序列合并
    P1387 最大正方形
    P1197 [JSOI2008]星球大战
    P2866 [USACO06NOV]糟糕的一天Bad Hair Day
    P1196 [NOI2002]银河英雄传说
    SP1805 HISTOGRA
    P1334 瑞瑞的木板
    2019信息学夏令营游记
  • 原文地址:https://www.cnblogs.com/huibixiaoxing/p/7701174.html
Copyright © 2011-2022 走看看