题目链接

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3953

题意

给出N个区间,求去掉某些区间,使得剩下的区间中,任何的三个区间都不两两相交。

思路

将所有区间 以左端点为键值从小到大排序

然后三个三个一组 进行判断

如果 这三个中有两两相交的 那么就删去右端点最大的 因为这个区间对答案的贡献最小

然后三个区间当中没有两两相交的,那么下一次进来的区间就替换掉右端点最小的。

AC代码

#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits> #define CLR(a) memset(a, 0, sizeof(a))
#define pb push_back using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair<string, int> psi;
typedef pair<string, string> pss; const double PI = 3.14159265358979323846264338327;
const double E = exp(1);
const double eps = 1e-30; const int INF = 0x3f3f3f3f;
const int maxn = 5e4 + 5;
const int MOD = 1e9 + 7; int ans[maxn]; int pos; struct Node
{
int id;
int l, r;
void read()
{
scanf("%d%d", &l, &r);
}
}q[maxn]; Node v[5]; bool comp(Node x, Node y)
{
return x.l < y.l;
} bool comp2(Node x, Node y)
{
return x.r < y.r;
} bool comp3(Node x, Node y)
{
return x.r > y.r;
} void solve()
{
sort(v, v + 3, comp);
bool flag = ((v[1].l <= v[0].r) && (v[2].l <= v[0].r) && (v[2].l <= v[1].r));
if (flag)
{
sort(v, v + 3, comp2);
ans[pos++] = v[2].id;
}
else
sort(v, v + 3, comp3);
} int main()
{
int t;
scanf("%d", &t);
while (t--)
{
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
q[i].read();
q[i].id = i;
}
sort(q + 1, q + 1 + n, comp);
v[0] = q[1];
v[1] = q[2];
pos = 0;
for (int i = 3; i <= n; i++)
{
v[2] = q[i];
solve();
}
sort(ans, ans + pos);
printf("%d\n", pos);
for (int i = 0; i < pos; i++)
{
if (i)
printf(" ");
printf("%d", ans[i]);
}
printf("\n");
}
}

ZOJ - 3953 Intervals 【贪心】的更多相关文章

  1. ZOJ 3953 Intervals

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

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

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

  3. 扫描线(线段树)+贪心 ZOJ 3953

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5572 Intervals Time Limit: 1 Second       ...

  4. ZOJ 3953:Intervals(优先队列+思维)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5572 题意:给出n个线段,问最少删除几个线段可以使得任意一个点不会被三个以上的 ...

  5. 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 ...

  6. zoj 1025Wooden Sticks(贪心)

    递增子序列的最小组数.可以直接贪心,扫一遍 #include<iostream> #include<cstring> #include<cstdio> #inclu ...

  7. POJ 1716 Integer Intervals#贪心

    (- ̄▽ ̄)-* //求一个集合,这个集合与任意一个区间的交集,需至少有两个数字 //贪心过程:按n个区间的最右值从小到大对区间进行排列, //集合首先取第一个区间的最右两个数字, //到第二个区间, ...

  8. E - Intervals 贪心

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

  9. zoj 1508 Intervals (差分约束)

    Intervals Time Limit: 10 Seconds      Memory Limit: 32768 KB You are given n closed, integer interva ...

随机推荐

  1. AC日记——Super Mario hdu 4417

    Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  2. linux下kill某个应用

    linux命令行与桌面切换快捷键Ctr+Alt+F1,Ctr+Alt+F7 ps -e | grep abc sudo kill xyz

  3. Android-Service基本用法、AIDL、Binder连接池详解

    本文介绍Service与Activity之间的通信,文章包含以下内容: 一.Service基本用法 二.通过AIDL实现Service与Activity跨进程通信 三.Binder连接池 四.使用Me ...

  4. ios- nil NULL 和 NSNull

    因为objective-c的集合对象,比如nsarray, nsdictionary, nsset等,都有可能包含nsnull对象,所以,如果以下代码中的item为nsnull,则会引起程序崩溃. N ...

  5. 【css】设置div位于浏览器的最底层,离用户最远

    有时候切换发现某块div一直悬浮在最上层,怎么设置div位于浏览器的最底层.离用户最远? <style> .in{ z-index: -1; } </style> 然后引用in ...

  6. Linux中的热键[Tab] [Ctrl]-c [Ctrl]-d

    Tab键:命令或者文件补全.可以避免很多的输入错误 1. 按一次,文件或命令补全 2. 按两次,会列举出以按键前的字母为首的所有命令或者文件 Ctrl+C:中断目前程序 Ctrl+D:键盘输入结束.可 ...

  7. install nfs and share file

    介绍一下NFS的安装,以及共享文件 NFS(Net File System),通过使用NFS,可以像使用本地文件一样访问远程文件. 它主要解决了数据共享的问题,可以备份容灾. 安装配置 1.以linu ...

  8. DVBS/S2在数字电视系统中的应用 三 (LNB介绍)

    DVBS/S2在数字电视系统中的应用 三 (LNB介绍) 老谢在前面两篇文章中(例如以下).都有提到LNB这一概念. DVBS/S2在数字电视系统中的应用 一 (DVBS/S2接收系统Block Di ...

  9. iOS加急审核之2015年总结

    就在今天到公司的一会,查看了一下邮件,收到Apple的回复,今年的第六次加急审核通过了. 然后,想想明天就是西方的圣诞节假期了,从22日到29日的这段时间,Apple会暂时关闭iTunesconnec ...

  10. C# 解析迅雷链接成正常的Http链接

    目前热门剧司马懿第一集的迅雷下载地址是: thunder://QUFodHRwOi8vZGwxMjIuODBzLmltOjkyMC8xNzA2L1vlpKflhptTU+mprOaHv+S5i+WGm ...