zoukankan      html  css  js  c++  java
  • poj1065 Wooden Sticks

    思路:

    贪心,最长下降子序列 二分。

    实现:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 #include <windows.h>
     5 const int INF = 0x3f3f3f3f;
     6 using namespace std;
     7 struct node
     8 {
     9     int x, y;
    10 };
    11 node a[5005];
    12 int d[5005];
    13 bool cmp(const node & a, const node & b)
    14 {
    15     if (a.x == b.x)
    16         return a.y < b.y;
    17     return a.x < b.x;
    18 }
    19 void work(int x, int end)
    20 {
    21     int l = 1, r = end, m, res = INF;
    22     while (l <= r)
    23     {
    24         m = (l + r) >> 1;
    25         if (d[m] <= x)
    26         {
    27             r = m - 1;
    28         }
    29         else
    30         {
    31             res = m;
    32             l = m + 1;
    33         }
    34     }
    35     if (res != INF)
    36         d[res + 1] = x;
    37     else
    38         d[1] = x;
    39 }
    40 int main()
    41 {
    42     int t, n;
    43     cin >> t;
    44     while (t--)
    45     {
    46         cin >> n;
    47         for (int j = 0; j < n; j++)
    48             scanf("%d %d", &a[j].x, &a[j].y);
    49         sort(a, a + n, cmp);
    50         int ans = 1;
    51         d[1] = a[0].y;
    52         for (int i = 1; i < n; i++)
    53         {
    54             if (a[i].y < d[ans])
    55                 d[++ans] = a[i].y;
    56             else
    57                 work(a[i].y, ans-1);    
    58         }
    59         cout << ans << endl;    
    60     }
    61     return 0;
    62 }
  • 相关阅读:
    周总结13
    周总结11
    《程序员的自我修养》阅读笔记四
    周总结10
    数据导入hive仓库
    周总结9
    《程序员的自我修养》阅读笔记三
    《软件需求》读书笔记四
    《软件需求》读书笔记三
    《软件需求》读书笔记二
  • 原文地址:https://www.cnblogs.com/wangyiming/p/6576195.html
Copyright © 2011-2022 走看看