Chinaunix首页 | 论坛 | 博客
  • 博客访问: 406632
  • 博文数量: 101
  • 博客积分: 2207
  • 博客等级: 大尉
  • 技术积分: 2508
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-19 20:45
文章分类

全部博文(101)

文章存档

2013年(15)

2012年(86)

我的朋友

分类: C/C++

2012-09-28 14:38:36


点击(此处)折叠或打开

  1. #include <iostream>
  2. #include <algorithm>

  3. struct Entry
  4. {
  5.     int x;
  6.     int y;
  7. };

  8. bool cmp(const Entry &a, const Entry &b)
  9. {
  10.     return a.x < b.x || (a.x == b.x && a.y <= b.y);
  11. }

  12. int binsearch(Entry* a,int low,int high, const int value)
  13. {
  14.     int mid;
  15.     while (low <= high)
  16.     {
  17.         mid = low + (high - low)/2;
  18.         Entry& tmp = a[mid];
  19.         if (tmp.x <= value && value <= tmp.y) return mid;
  20.         else if (value < tmp.x) high = mid -1;
  21.         else low = high + 1;
  22.     }
  23.     return -1;
  24. }
  25. const int MAXN = 256;

  26. int main()
  27. {
  28.     Entry arr[MAXN] = {0},brr[MAXN] = {0};
  29.     int n;
  30.     scanf("%d",&n);
  31.     for (int i = 0; i < n; ++i)
  32.         scanf("%d %d",&(arr[i].x),&(arr[i].y));
  33.     std::sort(arr,arr+n,cmp);
  34.     int x,y;
  35.     scanf("%d %d",&x,&y);
  36.     //[a,b],[c,d],a <= c
  37.     //(1) [a,b] U [c,d] = [a,b] ,a <= c && d <= b
  38.     //(2) [a,b][c,d] = 0 , b < c
  39.     //(3) [a,b] U [c,d] = [a,d]
  40.     int cnt = 0;
  41.     brr[cnt++] = arr[0];
  42.     for (int i = 1; i < n; ++i)
  43.     {
  44.        if (brr[cnt-1].y < arr[i].x) // [a,b] N [c,d] = 0 ,b < c
  45.        {
  46.          brr[cnt++] = arr[i];
  47.        }
  48.        else if (arr[i].x <= brr[cnt-1].y && brr[cnt-1].y <= arr[i].y)
  49.        {
  50.             brr[cnt-1].y = arr[i].y;
  51.        }
  52.        else if(brr[cnt-1].y >= arr[i].y)
  53.        {
  54.        }
  55.     }
  56.     /*for (int i = 0; i < cnt; ++i)
  57.     {
  58.         printf("(%d,%d),",brr[i].x,brr[i].y);
  59.     }
  60.     */
  61.     int xpos = binsearch(brr,0,cnt-1,x);
  62.     int ypos = binsearch(brr,0,cnt-1,y);
  63.     if (xpos != -1 && ypos != -1 && xpos == ypos)
  64.     {
  65.         printf("suc\n");
  66.     }
  67.     else
  68.     {
  69.         printf("fail\n");
  70.     }
  71. }

阅读(775) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~