ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Tokens on the Segments 题解(nlogn题解+贪心+优先队列)

2021-12-01 01:03:46  阅读:242  来源: 互联网

标签:pq int 题解 Segments lsh Tokens now id


题目链接

题目思路

网上我还没看到正解,感觉大家好像都是\(n^2logn\)甚至是更加高的复杂度,所以我决定水一篇题解

题意就是给你\(n\)条线段,要从每条线段选一个点放入一个集合中,求集合的最大\(size\)

我们设选点是从左往右

假设我们现在选的点\(pos\)为\(now\),那么显然下次选的点就是所有没有被选过的线段中\(l\leq now+1\)中\(r\)最小的点

我们把\(r\)放在一个优先队列里面,每次选了\(now\),我们就删除那个\(r\)最小的,然后加入所有左端点为\(now+1\)的

所有右端点的值

代码

#include<bits/stdc++.h>
#define fi first
#define se second
#define debug cout<<"I AM HERE"<<endl;
using namespace std;
typedef long long ll;
const int maxn=2e5+5,inf=0x3f3f3f3f,mod=1e9+7;
const double eps=1e-6;
int n;
int lsh[maxn];
struct node{
    int l,r;
}e[maxn];
int a[maxn];
vector<int> vec[maxn];
signed main(){
    int _;scanf("%d",&_);
    while(_--){
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%d%d",&e[i].l,&e[i].r);
            lsh[i] = e[i].l;
            a[i]=e[i].l;
            vec[i].clear();
        }
        sort(a+1,a+1+n);
        a[n+1]=inf;
        sort(lsh+1 , lsh+n+1);
        int cnt = unique(lsh+1 , lsh+n+1) - lsh - 1;
        for(int i=1; i<=n; i++)
            vec[lower_bound(lsh+1 , lsh+cnt+1 , e[i].l) - lsh].push_back(e[i].r);
        int now=-1;
        int ans=0;
        lsh[cnt+1]=0;
        while(1){
            now=*lower_bound(a+1,a+1+n+1,now);
            if(now==inf) break;
            priority_queue<int,vector<int>,greater<int> > pq;
            int id=lower_bound(lsh+1 , lsh+cnt+1 , now) - lsh;
            for(auto x:vec[id]){
                pq.push(x);
            }
            while(!pq.empty()){
                if(pq.top()>=now){
                    now++;
                    ans++;
                    pq.pop();
                }else{
                    pq.pop();
                    continue;
                }
                if(now==*lower_bound(lsh+1 , lsh+cnt+1 , now)){
                    int id=lower_bound(lsh+1 , lsh+cnt+1 , now) - lsh;
                    for(auto x:vec[id]){
                        pq.push(x);
                    }
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

标签:pq,int,题解,Segments,lsh,Tokens,now,id
来源: https://www.cnblogs.com/hunxuewangzi/p/15627164.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有