zoukankan      html  css  js  c++  java
  • DP:Bridging Signals(POJ 1631)

              

                    不能交叉的引脚

      (这一题的难度在于读题)题目大意:有一堆引脚(signals),左边一排,右边一排,左边从上到下,对应着连接右边的引脚(所有的引脚都被接上),现在引脚之间的连线有交叉,我们要桥接这些交叉,而桥接是费事的,现在要你求不交叉引脚的最大数目

      明白题在说什么以后,是不是感觉豁然开朗?

      没错,这一题我们只用把左边的引脚从上到下排列(事实上已经排了),然后看右边对应的引脚的上升序最长有多少就可以了

      昨天我弄了一个Wooden Sticks,这一题也要用到LIS,而且还是直接用LIS,更简单

     1 #include <iostream>
     2 #include <functional>
     3 #include <algorithm>
     4 
     5 using namespace std;
     6 typedef int Position;
     7 
     8 static int ports[40000];
     9 static int stacks[40000];
    10 
    11 void Search(const int);
    12 Position Binary_Search(const int,const int);
    13 
    14 int main(void)
    15 {
    16     int case_sum, ports_sum;
    17     while (~scanf("%d", &case_sum))
    18     {
    19         for (int j = 0; j < case_sum; j++)
    20         {
    21             scanf("%d", &ports_sum);
    22             for (int i = 0; i < ports_sum; i++)
    23                 scanf("%d", &ports[i]);//左边的每一个点对应右边的拿一个点
    24             Search(ports_sum);
    25         }
    26     }
    27     return 0;
    28 }
    29 
    30 Position Binary_Search(const int len,const int item)
    31 {
    32     int left = 0, right = len, mid;
    33 
    34     while (left <= right && left != len)
    35     {
    36         mid = (left + right) / 2;
    37         if (item < stacks[mid])
    38             right = mid - 1;
    39         else
    40             left = mid + 1;
    41     }
    42     return left;
    43 }
    44 
    45 void Search(const int ports_sum)
    46 { 
    47     int pos;
    48     int length = 1; stacks[0] = ports[0];
    49 
    50     for (int i = 1; i < ports_sum; i++)
    51     {
    52         pos = Binary_Search(length,ports[i]);
    53 
    54         stacks[pos] = ports[i];
    55         if (pos == length)
    56             length++;
    57     }
    58     printf("%d
    ", length);
    59 }

  • 相关阅读:
    PAT 甲级 1115 Counting Nodes in a BST (30 分)
    PAT 甲级 1114 Family Property (25 分)
    PAT 甲级 1114 Family Property (25 分)
    Python Ethical Hacking
    Python Ethical Hacking
    Python Ethical Hacking
    Python Ethical Hacking
    Python Ethical Hacking
    Python Ethical Hacking
    Python Ethical Hacking
  • 原文地址:https://www.cnblogs.com/Philip-Tell-Truth/p/4905999.html
Copyright © 2011-2022 走看看