ZOJ 3953:Intervals(优先队列+思维)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5572
题意:给出n个线段,问最少删除几个线段可以使得任意一个点不会被三个以上的线段覆盖。
思路:首先离散化坐标。
然后想着按右端点从小到大排序后直接O(n)扫的贪心,但是后面发现错误了。
应该按左端点从小到大排序,然后用一个优先队列(按右端点从大到小排序)。
开始扫坐标,当有与该坐标相同的点的左端点就进队,用ed[]记录右端点的位置。
用cnt记录当前这个点有多少个区间覆盖,当cnt>=3的时候就出队更新。
#include <bits/stdc++.h>
using namespace std;
#define N 50010
struct node {
int l, r, id;
node () {}
node (int l, int r, int id) : l(l), r(r), id(id) {}
bool operator < (const node &rhs) const {
if(r != rhs.r) return r < rhs.r;
return l < rhs.l;
}
} seg[N];
vector<int> ans;
priority_queue<node> que;
int ed[N*], p[N*];
bool cmp(const node &a, const node &b) {
if(a.l != b.l) return a.l < b.l;
return a.r < b.r;
}
int main() {
int t; scanf("%d", &t);
while(t--) {
int n, u, v; scanf("%d", &n);
ans.clear(); memset(ed, , sizeof(ed));
while(!que.empty()) que.pop();
int cnt = , now = , num = ;
for(int i = ; i < n; i++) scanf("%d%d", &u, &v), seg[i] = node(u, v, i + ), p[cnt++] = u, p[cnt++] = v;
sort(seg, seg + n, cmp); sort(p, p + cnt);
cnt = unique(p, p + cnt) - p;
for(int i = ; i < n; i++)
seg[i].l = lower_bound(p, p + cnt, seg[i].l) - p,
seg[i].r = lower_bound(p, p + cnt, seg[i].r) - p; for(int i = ; i < cnt && now < n; i++) {
while(seg[now].l == i && now < n) {
num++;
ed[seg[now].r + ]++; // 线段的终点
que.push(seg[now++]);
}
num -= ed[i]; // 离开了某线段的范围
while(num >= ) {
node tmp = que.top(); que.pop();
num--; ed[tmp.r + ]--; // 这个线段被删除
ans.push_back(tmp.id);
}
}
printf("%d\n", ans.size()); sort(ans.begin(), ans.end());
for(int i = ; i < ans.size(); i++) printf("%d%c", ans[i], i + == ans.size() ? '\n' : ' ');
}
return ;
}
ZOJ 3953:Intervals(优先队列+思维)的更多相关文章
- ZOJ - 3953 Intervals 【贪心】
题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3953 题意 给出N个区间,求去掉某些区间,使得剩下的区间中,任何 ...
- ZOJ 3953 Intervals
线段树,排序. 按照$R$从小到大排序之后逐个检查,如果$L$,$R$最大值不超过$2$,那么就把这个区间放进去,区间$+1$,否则不能放进去. #include<bits/stdc++.h&g ...
- Intervals ZOJ - 3953 (区间贪心)
Chiaki has n intervals and the i-th of them is [li, ri]. She wants to delete some intervals so that ...
- POJ 1201 & HDU1384 & ZOJ 1508 Intervals(差分约束+spfa 求最长路径)
题目链接: POJ:http://poj.org/problem?id=1201 HDU:http://acm.hdu.edu.cn/showproblem.php? pid=1384 ZOJ:htt ...
- POJ 1511 Invitation Cards (ZOJ 2008) 使用优先队列的dijkstra
传送门: http://poj.org/problem?id=1511 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1008 ...
- ZOJ 4124 拓扑排序+思维dfs
ZOJ - 4124Median 题目大意:有n个元素,给出m对a>b的关系,问哪个元素可能是第(n+1)/2个元素,可能的元素位置相应输出1,反之输出0 省赛都过去两周了,现在才补这题,这题感 ...
- ZOJ 3865 Superbot(优先队列--模板)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5477 主要思路:1.从一个点(cur)到它相邻的点(next),所需 ...
- zoj 3778 Talented Chef(思维题)
题目 题意:一个人可以在一分钟同时进行m道菜的一个步骤,共有n道菜,每道菜各有xi个步骤,求做完的最短时间. 思路:一道很水的思维题, 根本不需要去 考虑模拟过程 以及先做那道菜(比赛的时候就是这么考 ...
- ZOJ 649 Rescue(优先队列+bfs)
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
随机推荐
- [转]完美解决)Tomcat启动提示At least one JAR was scanned for TLDs yet contained no TLDs
一.文章前言 本文是亲测有效解决At least one JAR was scanned for TLDs yet contained no TLDs问题,绝对不是为了积分随便粘贴复制然后压根都 ...
- 用MVVM模式开发中遇到的零散问题总结(5)——将动态加载的可视元素保存为图片的控件,Binding刷新的时机
原文:用MVVM模式开发中遇到的零散问题总结(5)--将动态加载的可视元素保存为图片的控件,Binding刷新的时机 在项目开发中经常会遇到这样一种情况,就是需要将用户填写的信息排版到一张表单中,供打 ...
- Hermite曲线插值
原文 Hermite Curve Interpolation Hermite Curve Interpolation Hamburg (Germany), the 30th March 1998. W ...
- C# System.Threading.Timer的使用
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- windows下,Qt Creator 中javascript调试器安装并使用
最开始使用Qt Creator时,想使用断点来调试javascript代码.但在按下debug键后,却提示调试器未配置,让我比较郁闷. 好了,郁闷的是说了,咱们来说说高兴的.要Qt Creator调试 ...
- 【shell】递归函数----调用自身的函数
什么是递归函数? 一句话,调用自己的函数称为递归函数! #!/bin/bash declare -i count checkoutCount(){ read -p "Enter an cou ...
- TLD单目标跟踪算法程序详解--OpenTLD Code 详解
TLD算法原理介绍:http://www.cnblogs.com/liuyihai/p/8306419.html OpenTLD源代码页: https://github.com/zk00006/Ope ...
- Python爬虫: "追新番"网站资源链接爬取
“追新番”网站 追新番网站提供最新的日剧和日影下载地址,更新比较快. 个人比较喜欢看日剧,因此想着通过爬取该网站,做一个资源地图 可以查看网站到底有哪些日剧,并且随时可以下载. 资源地图 爬取的资源地 ...
- Android零基础入门第65节:RecyclerView分割线开发技巧
在上一期通过简单学习,已经领略到了RecyclerView的灵活性,当然都是一些最基础的用法,那么本期一起来学习RecyclerView的分割线使用. 相信有的比较细心的同学已经发现了,使用Recyc ...
- UWP 发送短信公用倒计时按钮
1.要求: 发送验证码按钮,点击后,会倒计时60s,之后才能再次点击.不同界面的多个验证码按钮共享这个倒计时时间. 2.操作步骤 1) 从登录界面-->忘记密码输入手机号- ...