UVALive 2326 Moving Tables greedy

Source: Internet
Author: User

You have several things to move from one classroom to another, which takes 10 minutes. However, during this period, the corridor you need to go through cannot be occupied by other tasks. The classroom distribution is as follows: Obviously greedy. In fact, it is enough to find out at least a few non-Intersecting intervals and find them cyclically. The complexity is O (n ^ 2). This is feasible, because the data volume is not very large, it can only reach 200. However, if the data volume is large, it will time out. So I thought of an o (nlogn + n) method and sorted it and traversed it again, take an array to record the end value of each group of non-Intersecting intervals. If each query can be placed in a group of intervals, it can be put in, and no longer opens up an array. The difference is that the two rooms entered are not necessarily greater than the right on the left, and the wa is performed here. Code:

 /*  *   Author:        illuz <iilluzen[at]gmail.com>  *   Blog:          http://blog.csdn.net/hcbbt  *   File:          uvalive2326.cpp  *   Lauguage:      C/C++  *   Create Date:   2013-09-07 14:19:24  *   Descripton:    UVALive 2326 Moving Tables, greedy   */  #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 = 210;  struct P {      int lhs, rhs;  } p[MAXN];  int t, n, vis[MAXN], rec[MAXN], a, b;    bool cmp(P a, P b) {      return a.lhs < b.lhs;  }    int main() {      scanf("%d", &t);      while (t--) {          scanf("%d", &n);          if (n == 0) {              printf("0\n");              continue;          }          rep(i, n) {              scanf("%d%d", &a, &b);              a = (a + 1) / 2;              b = (b + 1) / 2;              if (a == b) i--, n--;              else {                  p[i].lhs = min(a, b);                  p[i].rhs = max(a, b);              }          }          sort(p, p + n, cmp);          int ans = 0;          mc(vis); mc(rec);          for (int i = 0; i < n; i++) {              if (vis[i] == 0) {                  vis[i]++;                  int ok = false;                  for (int j = 1; j <= ans && !ok; j++)                      if (rec[j] < p[i].lhs)                          rec[j] = p[i].rhs, ok = true;                  if (!ok)                      rec[++ans] = p[i].rhs;              }          }          if (ans == 0) printf("10\n");          else printf("%d\n", ans * 10);      }      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.