传送门

•题意

  二维平面上有 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. thinkphp---display与fetch区别

    区别: ① display方法直接输出模板文件渲染后的内容,fetch方法是返回模板文件渲染后的内容 ② 有时候我们不想直接输出模板内容,而是希望对内容再进行一些处理后输出, 就可以使用fetch方法 ...

  2. java读取项目路径下的中文文件乱码问题

    出现乱码错误: 处理方案: 对文件路径中存在中文的,都要进行URLDecoder.decode(path,"UTF-8")编码转换 wordContent = URLEncoder ...

  3. nodeJs学习-10 模板引擎 ejs语法案例

    ejs语法案例 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <t ...

  4. iOS:学习runtime的理解和心得

    http://www.cocoachina.com/ios/20150901/13173.html 作者:兴宇是谁 授权本站转载. Runtime是想要做好iOS开发,或者说是真正的深刻的掌握OC这门 ...

  5. jq向元素附加数据

    --------data() 方法向被选元素附加数据,或者从被选元素获取数据.--------- --------removeData() 方法删除之前通过 data() 方法设置的数据.------ ...

  6. LeetCode109 Convert Sorted List to Binary Search Tree

    Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...

  7. 利用IDEA构建springboot应用-Controller的使用

    Controller的使用 @Controller 处理http请求   @RestController  Spring4之后新加的注解,原来返回json需要@ResponseBody配合@Contr ...

  8. Python中的生产者消费者模型

    ---恢复内容开始--- 了解知识点: 1.守护进程: ·什么是守护进程: 守护进程其实就是一个‘子进程’,守护即伴随,守护进程会伴随主进程的代码运行完毕后而死掉 ·为何用守护进程: 当该子进程内的代 ...

  9. nodeJs学习-06 模块化、系统模块、自定义模块、express框架

    系统模块:http://nodejs.cn/api/events.html 自定义模块: require   请求:引入模块 module    模块:批量输出 exports   输出:单独输出   ...

  10. LightOJ 1341 Aladdin and the Flying Carpet【整数分解】

    题目链接: http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1341 题意: 给定一个数,将其拆分成两个数的乘 ...