zoukankan      html  css  js  c++  java
  • poj 1836 Alignment 排队

    poj   1836   Alignment

    http://poj.org/problem?id=1836

    题意:有士兵n个,根据编号排为一列,但是身高不一,现在要求去掉几个人,使得剩下的每一个人可以向左或向右看到队头。问:至少去掉的士兵数。

    dp动态规划双向LIS问题

     1 /*
     2 Problem: 1836        User: bibier
     3 Memory: 712K        Time: 63MS
     4 Language: G++        Result: Accepted
     5 */
     6 //动态规划 之 双向LIS问题
     7 #include <stdio.h>
     8 #include <string.h>
     9 #include <iostream>
    10 #include <algorithm>
    11 #include <cstdio>
    12 #include <cstring>
    13 #include <cmath>
    14 #include <stack>
    15 #include <queue>
    16 #include <vector>
    17 using namespace std;
    18 #define M 0x0f0f0f0f
    19 #define min(a,b) (a>b?b:a)
    20 #define max(a,b) (a>b?a:b)
    21 
    22 int qdp[1003];
    23 int hdp[1003];
    24 float high[1003];
    25 
    26 int main()
    27 {
    28     int n;
    29     int i,j;
    30     scanf("%d",&n);
    31     for(i=0; i<n; i++)
    32     {
    33         scanf("%f",&high[i]);
    34         qdp[i]=1;
    35         hdp[i]=1;
    36     }
    37 
    38     for(i=1; i<n; i++)
    39         for(j=0; j<i; j++)
    40         {
    41             if(high[j]<high[i]&&qdp[j]==qdp[i])
    42                 qdp[i]++;
    43         }
    44 
    45     for(i=n-2; i>=0; i--)
    46         for(j=n-1; j>i; j--)
    47         {
    48             if(high[j]<high[i]&&hdp[j]==hdp[i])
    49                 hdp[i]++;
    50         }
    51 
    52     int max_=0;
    53     
    54     /*
    55     若没有身高相同的一层循环就足够
    56     for(i=0; i<n; i++)
    57         max_=max(max_,qdp[i]+hdp[i]);
    58     */
    59     
    60     for(i=0; i<n-1; i++)   //用两层循环是为了处理1 2 5 5 1 3 2或1 2 5 5 5 5 1 3 2
    61         for(j=i+1; j<n; j++)
    62             max_=max(max_,qdp[i]+hdp[j]);
    63     printf("%d\n",n-max_);
    64     return 0;
    65 }
    View Code
  • 相关阅读:
    [Canvas]RPG游戏雏形 (地图加载,英雄出现并移动)
    [Canvas]计时表/秒表
    [Canvas]新版箴言钟表
    [Canvas]用透明PNG图在背景上画前景能不遮挡背景
    [Canvas]动态背景
    [Canvas]更多的球
    [Canvas]碰撞球 增加小球间碰撞检测
    [Canvas]碰撞球
    [Canvas]越来越近的女孩
    [Canvas]走近的女孩
  • 原文地址:https://www.cnblogs.com/bibier/p/3898050.html
Copyright © 2011-2022 走看看