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_i\le 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\n", re);
46     }
47     return 0;
48 }
复制代码

 

posted @   Xxiaoyu  阅读(347)  评论(1编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示