传送门

•题意

  二维平面上有 n 条线段,每条线段坐标为 $(l_i,i),(r_i,i)$;

  平面上的每个整点坐标上都可以放置一枚硬币,但是要求任意两枚硬币的横坐标不相同;

  问最多有多少条线段可以放置硬币。

•题解1

  考虑到当 $X=x$ 时,最多有一条线段可以放置一枚硬币;

  那么,我们可以从左到右查找最多有多少条线段可以放置硬币;

  定义变量 $X$ 表示 $[0,X]$ 位置已放置好硬币;

  既然是按照 $x$ 来的,那么我们就需要将所有可能可以放置硬币的线段按照 $l$ 升序排列,如果 $l$ 相同,按照 $r$ 升序排列;

  考虑用优先级队列,首先将所有线段放入优先级队列 $q$ 中,并定义 $X=0$;

  每次选择从 $q$ 的队头取出 $l$ 小的线段,判断这条线段的 $l'$ 与 $X$ 的位置关系:

    ①如果 $l' \leq X$,说明当前这条线段的 $[l',X]$ 位置 不能放置硬币,只能考虑 $[X+1,r']$ 位置是否还可以放置硬币;

       那么,此时,我们就将 $[X+1,r_i]$ 丢到 $q$ 中,代表可能从 $[X+1,r']$ 中选择某位置放置硬币;

    ②如果 $l' > X$,说明 $[X,l')$ 间无可放置硬币的线段,那么我们要选择一枚硬币放置在 $l'$ 处,即当前这条线段上,并更新 $X=l'$;

•Code

 #include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+; int n;
struct Heap
{
int l,r;
bool operator < (const Heap &obj)const
{
if(l != obj.l)
return l > obj.l;
return r > obj.r;
}
};
priority_queue<Heap >q; int Solve()
{
int X=;
int ans=;
while(!q.empty())
{
Heap tmp=q.top();
q.pop(); /**
如果 tmp.l <= X,那么[tmp.l,X]是已求出最解的位置
但是[X+1,tmp.r] 还是没有放置硬币的
所以当前线段还是有可能在[X+1,tmp.rr]区间放置一枚硬币的
所以将其加入到q中
*/
if(tmp.l <= X && X+ <= tmp.r)
q.push({X+,tmp.r});
else if(tmp.l > X)///如果tmp.l > X,更新ans,X
{
ans++;
X=tmp.l;
}
}
return ans;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
while(!q.empty())
q.pop(); scanf("%d",&n);
for(int i=;i <= n;++i)
{
int l,r;
scanf("%d%d",&l,&r);
q.push({l,r});
}
printf("%d\n",Solve());
}
return ;
}

  

•题解2

  贪心+暴力

  贪心策略:按 $r$ 从小到大排,$r$ 相同按 $l$ 从小到大排;

  从 1~n 遍历每个线段,对于第 i 条线段,暴力查找 $[l,r]$ 最左的为放置硬币的空位置;

•Code

 #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
const int maxn=1e5+; int n;
set<int >_set;
struct Date
{
int l,r;
int len;
bool operator < (const Date &obj) const
{
return r < obj.r;
}
}_date[maxn]; int Solve()
{
sort(_date+,_date+n+);
_set.clear(); int ans=;
for(int i=;i <= n;++i)
{
int l=_date[i].l;
int r=_date[i].r;
for(int j=l;j <= r;++j)
{
if(_set.find(j) == _set.end())///查找第i条线段可以放置硬币的最左的位置
{
_set.insert(j);
ans++;
break;
}
}
}
return ans;
}
int main()
{
int test;
scanf("%d",&test);
while(test--)
{
scanf("%d",&n);
for(int i=;i <= n;++i)
{
scanf("%d%d",&_date[i].l,&_date[i].r);
_date[i].len=_date[i].r-_date[i].l+;
}
printf("%d\n",Solve());
}
return ;
}

•题解2分析

  如果输入 1e5 个线段,所有线段的左右端点全部为 [1,1e9];

  那么,这个算法的时间复杂度为 O(n2logn);

  这个时间复杂度在打比赛的时候是不敢想的啊;

  虽然不能说是正解,但可以借鉴一下其贪心的思路(tql);

•疑惑

  这道题在离散化后跑一边方法①的代码wa了???

  感觉,离散化后不影响结果啊??

The 10th Shandong Provincial Collegiate Programming Contest H.Tokens on the Segments(贪心+优先级队列 or 贪心+暴力)的更多相关文章

  1. The 10th Shandong Provincial Collegiate Programming Contest(11/13)

    $$The\ 10th\ Shandong\ Provincial\ Collegiate\ Programming\ Contest$$ \(A.Calandar\) 签到 //#pragma co ...

  2. The 10th Shandong Provincial Collegiate Programming Contest

    目录 Contest Info Solutions A. Calandar B. Flipping Game C. Wandering Robot D. Game on a Graph E. BaoB ...

  3. The 10th Shandong Provincial Collegiate Programming Contest 2019山东省赛游记+解题报告

    比赛结束了几天...这篇博客其实比完就想写了...但是想等补完可做题顺便po上题解... 5.10晚的动车到了济南,没带外套有点凉.酒店还不错. 5.11早上去报道,济南大学好大啊...感觉走了一个世 ...

  4. The 10th Zhejiang Provincial Collegiate Programming Contest

    Applications http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5008 string set 专场 #include& ...

  5. zoj The 12th Zhejiang Provincial Collegiate Programming Contest May Day Holiday

    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5500 The 12th Zhejiang Provincial ...

  6. zoj The 12th Zhejiang Provincial Collegiate Programming Contest Capture the Flag

    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5503 The 12th Zhejiang Provincial ...

  7. zoj The 12th Zhejiang Provincial Collegiate Programming Contest Team Formation

    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5494 The 12th Zhejiang Provincial ...

  8. zoj The 12th Zhejiang Provincial Collegiate Programming Contest Beauty of Array

    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5496 The 12th Zhejiang Provincial ...

  9. zoj The 12th Zhejiang Provincial Collegiate Programming Contest Lunch Time

    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5499 The 12th Zhejiang Provincial ...

随机推荐

  1. utf8mb4 使用注意

    数据库的表的定义如果是utf8mb4的富文本时,关联的字段必须指定为非utf8,否则 跟其他的表关联的时候,会非常慢,以至于索引都不能使用. 也就是必须的字段才可以使用这个 utf8mb4 ,否则检索 ...

  2. Docker for windows pull镜像文件的安装位置改变方法

    发生现象: 在windows10下安装docker for windows,随着用docker pull image文件后,C盘的容量越来越小了,你可能也有一种跟我一样的想法,想改变默认的安装路径,本 ...

  3. KiCad EDA 如何修改 Pcbnew 线路板的背景色?

    KiCad EDA 如何修改 Pcbnew 线路板的背景色? 关于背景色,传统的原理图是白色,线路板是黑色. EDA 软件 类型 颜色 Protel 原理图 浅黄色 Protel PCB 黑色 Orc ...

  4. 2019-8-31-asp-dotnet-core-支持客户端上传文件

    title author date CreateTime categories asp dotnet core 支持客户端上传文件 lindexi 2019-08-31 16:55:58 +0800 ...

  5. vue-cnodejs

    感谢那些无私开源的程序员,你们是最可爱的人儿~~~~ //根app app.js <template> <div id="app"> <v-heade ...

  6. LeetCode225 Implement Stack using Queues

    Implement the following operations of a stack using queues. (Easy) push(x) -- Push element x onto st ...

  7. hdu4325 线段树 成段更新

    #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; #def ...

  8. C/S和B/S交互 2016-03-19 11:27 1275人阅读 评论(30) 收藏

    最近一直在做C/S的项目,每天都超忙,抽个时间写篇博客,之前一直做C/S项目就是各种窗体,各种控件,拖来拖去,然后点进去写方法,做BS的时候呢,因为一直使用的是mvc,所以就是经常手写代码,或者拖引用 ...

  9. 发布SaaS加速器:我们不做SaaS,我们只做SaaS生态的推进者和守护者

    摘要: 此次阿里云推出的SaaS加速器,涵盖商业中心.能力中心.技术中心三大板块,是阿里巴巴商业.能力和技术的一次合力输出:技术能力在这里沉淀为一个个模块,ISV和开发者只要通过简单的操作,写很少的代 ...

  10. docker查看运行容器详细信息

    使用docker ps命令可以查看所有正在运行中的容器列表, 使用docker inspect命令我们可以查看更详细的关于某一个容器的信息. $ docker inspect 容器id/image