传送门

•题意

  二维平面上有 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. PLAY2.6-SCALA(十一) 模板常用场景

    1.布局 声明一个views/main.scala.html模板作为主布局模板 @(title: String)(content: Html) <!DOCTYPE html> <ht ...

  2. qt获取本机用户名

    //获取用户名 QString getUserName() { #if 1 QStringList envVariables; envVariables << "USERNAME ...

  3. iPhone开发之深入浅出 (7) — ARC总结

    原文链接:http://www.yifeiyang.net/development-of-the-iphone-simply-7/ 通过前面几篇文章的介绍,我想大家应该对ARC有了一个比较完整的理解. ...

  4. 一个基于Asterisk构建的VOIP应用软件:Elastix介绍

    Elastix 是一种应用软件,它整合了适用于那些基于 Asterisk 的 PBX 的最好工具,并将它们集成为单一的.易用的接口.同时,它增加了自己的工具集,以及允许创建第三方模块来使 Elasti ...

  5. No.4 Verilog 表达式

    4-1 操作数 常数.参数.线网.变量.位选.存储器.数组. *部分位选: integer mark; :] inst; :] gpio; inst[mark+ : ] //选择 mark,mark+ ...

  6. iOS9 CASpringAnimation 弹簧动画详解

    http://blog.csdn.net/zhao18933/article/details/47110469 1. CASpringAnimation iOS9才引入的动画类,它继承于CABaseA ...

  7. 观察者模式(Java实现)

    import java.util.ArrayList; import java.util.Iterator; /* 抽象观察者类 */ abstract class Observer { public ...

  8. Audio Session Programming Guide

    http://www.cocoachina.com/ios/20150615/12119.html

  9. python 空值(NoneType)

  10. oralcle函数 AVG([distinct|all]x)

    [功能]统计数据表选中行x列的平均值. [参数]all表示对所有的值求平均值,distinct只对不同的值求平均值,默认为all 如果有参数distinct或all,需有空格与x(列)隔开. [参数] ...