ZOJ - 3953 Intervals 【贪心】
题目链接
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 【贪心】的更多相关文章
- 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 ...
- 扫描线(线段树)+贪心 ZOJ 3953
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5572 Intervals Time Limit: 1 Second ...
- ZOJ 3953:Intervals(优先队列+思维)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5572 题意:给出n个线段,问最少删除几个线段可以使得任意一个点不会被三个以上的 ...
- 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 ...
- zoj 1025Wooden Sticks(贪心)
递增子序列的最小组数.可以直接贪心,扫一遍 #include<iostream> #include<cstring> #include<cstdio> #inclu ...
- POJ 1716 Integer Intervals#贪心
(- ̄▽ ̄)-* //求一个集合,这个集合与任意一个区间的交集,需至少有两个数字 //贪心过程:按n个区间的最右值从小到大对区间进行排列, //集合首先取第一个区间的最右两个数字, //到第二个区间, ...
- E - Intervals 贪心
Chiaki has n intervals and the i-th of them is [li, ri]. She wants to delete some intervals so that ...
- zoj 1508 Intervals (差分约束)
Intervals Time Limit: 10 Seconds Memory Limit: 32768 KB You are given n closed, integer interva ...
随机推荐
- LeetCode OJ--Binary Tree Preorder Traversal
http://oj.leetcode.com/problems/binary-tree-preorder-traversal/ 二叉树的先跟遍历,写的是递归版本,也可以使用stack来进行,替代了递归 ...
- Java原来如此-几种常见的排序--冒泡排序(Bubble Sort)
冒泡排序的原理:假设要求的数组是正序,两两进行比较,如果前一个数比后一个数小,位置不变.如果前一个数比后一个数大,位置互换,再跟后一个数进行比较,直到最后.就是逐步把大数送到最后. 举个例子:int[ ...
- 洛谷—— P3807 【模板】卢卡斯定理
https://www.luogu.org/problemnew/show/3807 题目背景 这是一道模板题. 题目描述 给定n,m,p(1\le n,m,p\le 10^51≤n,m,p≤105) ...
- T2821 天使之城 codevs
http://codevs.cn/problem/2821/ 题目描述 Description 天使城有一个火车站,每辆火车都从A方向驶入车站,再从B方向驶出车站. 为了调度火车,火车站设有停放轨道, ...
- SnakeYaml使用
新的项目中需要将yaml文件解析为对象,调研了决定使用snakeYaml,下面看一看怎么使用. 一.引入依赖 因为项目是使用maven构建的,所以我们在pom文件中引入snakeYaml的依赖,如下: ...
- kafka技术分享01--------why we study kafka?
kafka技术分享01--------why we study kafka? 作为一名大数据工程师,我们所面对的大多数是数据密集型的应用,而非计算密集型的应用.对于数据密集型的应用,如何解决数据激 ...
- Generate C and C++ Header File
1. 2. 其中bootclasspath 后面的参数就是自己android.jar具体位置 location: ${system_path:javah} working Directoy: ${pr ...
- openfire Android学习(五)------连接断开重连
首先要创建连接监听器,用来监听连接状态,这里我写了一个类 继承了ConnectionListener,重写了里面5个方法,最重要的两个方法connectionClosed()和connectionCl ...
- openfire Android学习---android客户端聊天开发之登录 和 注销登录
一切就绪,新建一个android测试工程: 上网权限配置,界面绘制啥的,这里就不说了. 首先 导入一个smark包.这个是用来维护长连接的,也可以是asmark.我用的是asmark 先普及一些基本知 ...
- CSRF攻击 & XSS攻击
之前有几篇文章写了 SQL注入类问题: http://www.cnblogs.com/charlesblc/p/5987951.html (介绍) http://www.cnblogs.com/cha ...