zoukankan      html  css  js  c++  java
  • Tokens on the Segments(优先队列+贪心)

    Consider $n$ segments on a two-dimensional plane, where the endpoints of the $i$-th segment are $(l_i, i)$ and $(r_i, i)$. One can put as many tokens as he likes on the integer points of the plane (recall that an integer point is a point whose $x$ and $y$ coordinates are both integers), but the $x$ coordinates of the tokens must be different from each other.

    What's the maximum possible number of segments that have at least one token on each of them?

    Input

    The first line of the input contains an integer $T$ (about 100), indicating the number of test cases. For each test case:

    The first line contains one integer $n$ ($1 le n le 10^5$), indicating the number of segments.

    For the next $n$ lines, the $i$-th line contains 2 integers $l_i, r_i$ ($1 le l_i le r_ile 10^9$), indicating the $x$ coordinates of the two endpoints of the $i$-th segment.

    It's guaranteed that at most 5 test cases have $n ge 100$.

    Output

    For each test case output one line containing one integer, indicating the maximum possible number of segments that have at least one token on each of them.

    Sample Input

    2
    3
    1 2
    1 1
    2 3
    3
    1 2
    1 1
    2 2
    

    Sample Output

    3
    2
    

    Hint

    For the first sample test case, one can put three tokens separately on (1, 2), (2, 1) and (3, 3).

    For the second sample test case, one can put two tokens separately on (1, 2) and (2, 3).

    题意挺麻烦的,就是说现在有一个二维坐标,上面有一些平行于x轴的线段,若现在有一线段从1到5,那么我们可以用1到5任意一点标记该线段,每个x值只能标记一条线段。

    问最多能标记几条线段

     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 
     5 struct node{
     6     int l, r;
     7     bool operator < (const node &x) const{
     8         return l>x.l || (l==x.l&&r>x.r); ///相反
     9     }
    10 }str;
    11 
    12 priority_queue<node>que;
    13 map <int, int> mp;
    14 
    15 int main()
    16 {
    17     int t, n, i, re;
    18     scanf("%d", &t);
    19     while(t--)
    20     {
    21         scanf("%d", &n);
    22         mp.clear();
    23         for(i=0;i<n;i++)
    24         {
    25             scanf("%d %d", &str.l, &str.r);
    26             que.push(str);
    27         }
    28 
    29         re = 0;
    30         while(!que.empty())
    31         {
    32             str = que.top();
    33             que.pop();
    34             if(!mp[str.l])
    35             {
    36                 mp[str.l] = 1;
    37                 re++;
    38             }
    39             else
    40             {
    41                 str.l++;
    42                 if(str.r>=str.l) que.push(str);
    43             }
    44         }
    45         printf("%d
    ", re);
    46     }
    47     return 0;
    48 }
  • 相关阅读:
    重定向输出流实现程序日志
    为新员工分配部门
    从控制台接收输入的身份证号
    判断某一年是否为闰年
    linux重置mysql密码(root权限)
    mysql按照字符串类型的数值按数值进行排序
    android 下拉刷新
    android studio gradle 配置
    搭建自己的iOS内测分发平台
    http_range说明
  • 原文地址:https://www.cnblogs.com/0xiaoyu/p/12844329.html
Copyright © 2011-2022 走看看