UVALive 2322 Wooden Sticks sorting + greedy

Source: Internet
Author: User

Give the first and tail coordinates of n wooden bars, and find the minimum number of non-descending sequence of wooden bars. After sorting, simulate it. Code:

 /*  *   Author:        illuz <iilluzen[at]gmail.com>  *   Blog:          http://blog.csdn.net/hcbbt  *   File:          uvalive2322.cpp  *   Lauguage:      C/C++  *   Create Date:   2013-09-06 20:50:20  *   Descripton:    greedy, sort   */  #include <cstdio>  #include <cstring>  #include <algorithm>  using namespace std;  #define rep(i, n) for (int i = 0; i < (n); i++)  #define mc(a) memset(a, 0, sizeof(a))    const int MAXN = 5001;  struct P {      int lhs, rhs;  } p[MAXN];  int t, n;  bool vis[MAXN];    bool cmp(P a, P b) {      if (a.lhs != b.lhs) return a.lhs < b.lhs;      else return a.rhs < b.rhs;  }    int main() {      scanf("%d", &t);      while (t--) {          // input          scanf("%d", &n);          rep(i, n) scanf("%d%d", &p[i].lhs, &p[i].rhs);          // sort          sort(p, p + n, cmp);          // solve          mc(vis);          int cnt = 0;          while (1) {              int i;              for (i = 0; i < n; i++)                  if (!vis[i]) break;              if (i == n) break;              vis[i] = true;              int l = p[i].lhs, r = p[i].rhs;              for (i++; i < n; i++)                  if (!vis[i] && p[i].lhs >= l && p[i].rhs >= r) {                      vis[i]++;                      l = p[i].lhs;                      r = p[i].rhs;                  }              cnt++;          }          printf("%d\n", cnt);      }      return 0;  }  

 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.