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(优先队列+思维)的更多相关文章

  1. ZOJ - 3953 Intervals 【贪心】

    题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3953 题意 给出N个区间,求去掉某些区间,使得剩下的区间中,任何 ...

  2. ZOJ 3953 Intervals

    线段树,排序. 按照$R$从小到大排序之后逐个检查,如果$L$,$R$最大值不超过$2$,那么就把这个区间放进去,区间$+1$,否则不能放进去. #include<bits/stdc++.h&g ...

  3. Intervals ZOJ - 3953 (区间贪心)

    Chiaki has n intervals and the i-th of them is [li, ri]. She wants to delete some intervals so that ...

  4. POJ 1201 &amp; HDU1384 &amp; ZOJ 1508 Intervals(差分约束+spfa 求最长路径)

    题目链接: POJ:http://poj.org/problem?id=1201 HDU:http://acm.hdu.edu.cn/showproblem.php? pid=1384 ZOJ:htt ...

  5. POJ 1511 Invitation Cards (ZOJ 2008) 使用优先队列的dijkstra

    传送门: http://poj.org/problem?id=1511 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1008 ...

  6. ZOJ 4124 拓扑排序+思维dfs

    ZOJ - 4124Median 题目大意:有n个元素,给出m对a>b的关系,问哪个元素可能是第(n+1)/2个元素,可能的元素位置相应输出1,反之输出0 省赛都过去两周了,现在才补这题,这题感 ...

  7. ZOJ 3865 Superbot(优先队列--模板)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5477 主要思路:1.从一个点(cur)到它相邻的点(next),所需 ...

  8. zoj 3778 Talented Chef(思维题)

    题目 题意:一个人可以在一分钟同时进行m道菜的一个步骤,共有n道菜,每道菜各有xi个步骤,求做完的最短时间. 思路:一道很水的思维题, 根本不需要去 考虑模拟过程 以及先做那道菜(比赛的时候就是这么考 ...

  9. ZOJ 649 Rescue(优先队列+bfs)

    Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

随机推荐

  1. HDU 1027 以数列

    Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ( ...

  2. C# ToString() 数据格式

    double[] numbers= {1054.32179, -195489100.8377, 1.0437E21, -1.0573e-05}; string[] specifiers = { &qu ...

  3. DotNetBar for Windows Forms 14.0.0.3_冰河之刃重打包版原创发布

    关于 DotNetBar for Windows Forms 14.0.0.3_冰河之刃重打包版 --------------------11.8.0.8_冰河之刃重打包版-------------- ...

  4. C#正则表达式的完全匹配、部分匹配及忽略大小写的问题

    原文:C#正则表达式的完全匹配.部分匹配及忽略大小写的问题 问题的提出 根据用户给定表达式,里面含有各种数学函数,如求绝对值,三角函数,平方.开方等,分别以类似ABS(表达式),Sin(表达式),AS ...

  5. Apache Cordova介绍

    原文:Apache Cordova介绍 Apache Cordova是一套设备API,允许移动应用的开发者使用JavaScript来访问本地设备的功能,比如摄像头.加速计.它可以与UI框架(如jQue ...

  6. iOS 监听控件某个属性的改变observeValueForKeyPath

    创建一个测试的UIButton #import "ViewController.h" @interface ViewController () @property(nonatomi ...

  7. sqlserver检查sql执行时间

    set statistics profile onset statistics io onset statistics time onGO select * from mytable; goset s ...

  8. C# ToolStrip在父窗体失去焦点时,点击里面的按钮无效

    C#里面ToolStrip控件,在父窗体失去焦点的情况下,第一次点击ToolStrip上面的按钮,只是让父窗体获得焦点,并不会引发按钮的Click事件.要执行按钮操作必须再点击一次.相当于说,在父窗体 ...

  9. win10 uwp 获得Slider拖动结束的值

    原文:win10 uwp 获得Slider拖动结束的值 本文讲的是如何获得Slider移动结束的值,也就是触发移动后的值.如果我们监听ValueChanged,在我们鼠标放开之前,只要拖动不放,那么就 ...

  10. Easy Compression Library(代替TFileStream, TMemoryStream and TStream)

    Easy Compression Library is a very easy-to-use replacement of TFileStream, TMemoryStream and other T ...