(有任何问题欢迎留言或私聊 && 欢迎交流讨论哦

Catalog

Problem:Portal传送门

 原题目描述在最下面。

 给你n个区间,问最少删除多少个区间,使得任意三个区间不两两相交。

Solution:

 先把所有区间按先左端点再右端点从小到大排序,每次选出前面相邻的三个区间:

 如果两两相交则一定要删除右端点最大的那个区间,因为这个区间的能影响的范围更大!

 若不两两相交就放掉右端点最小的,用下一个区间代替它,因为左端点是递增的,任选后面两个区间肯定不会和这个右端点最小的区间两两相交!重复这个过程。

 还有就是最开始区间一定要先按左端点再按右端点排序。

 我提供一组样例:

4
2 5
3 7
1 15
9 12

####AC_Code:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<vector>
#include<cmath>
#include<bitset>
#include<cassert>
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define mme(a,b) memset((a),(b),sizeof((a)))
#define fuck(x) cout<<"* "<<x<<"\n"
#define iis std::ios::sync_with_stdio(false)
using namespace std;
typedef pair<int,int> pii;
typedef long long LL;
const int MXN = 60000 + 7;
const int MXE = 1e6 + 7;
const int mod = 998244353;
const int INF = 0x3f3f3f3f; int n, m;
struct lp{
int l, r, id;
}cw[MXN],op[3];
bool cmp1(lp &a,lp &b){
if(a.r!=b.r)return a.r < b.r;
return a.l < b.l;
}
bool cmp2(lp &a,lp &b){
if(a.l!=b.l)return a.l < b.l;
return a.r < b.r;
}
int main(){
int tim;
scanf("%d", &tim);
while(tim--){
scanf("%d", &n);
for(int i = 0; i < n; ++i){
scanf("%d%d", &cw[i].l, &cw[i].r);cw[i].id = i+1;
}
if(n<2){
printf("0\n\n");
continue;
}
sort(cw,cw+n,cmp2);
op[0] = cw[0]; op[1] = cw[1];
int cnt = 0;
std::vector<int> ans;
for(int t = 2; t < n; ++t){
op[2] = cw[t];
sort(op,op+3,cmp1);
int flag = 1;
for(int i = 0; i < 3; ++i){
for(int j = i + 1; j < 3; ++j){
if(op[j].l>op[i].r)flag = 0;
}
}
if(flag == 0){
op[0] = op[1];
op[1] = op[2];
}else{
cnt++;
ans.push_back(op[2].id);
}
}
printf("%d\n", cnt);
if(cnt == 0)printf("\n");
else {
sort(ans.begin(),ans.end());
for(int i = 0; i < cnt; ++i){
printf("%d%c", ans[i], " \n"[i==cnt-1]);
}
}
}
return 0;
}

####Problem Description:

ZOJ3953-Intervals-贪心的更多相关文章

  1. 贪心:zoj3953 Intervals

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

  2. zoj3953 Intervals 最大不重叠区间加强版 zoj排名第一~

    Intervals Time Limit: 1 Second      Memory Limit:65536 KB      Special Judge Chiaki has n intervals ...

  3. POJ 1716 Integer Intervals#贪心

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

  4. E - Intervals 贪心

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

  5. ZOJ3953 Intervals

    题意 有n个区间,要求删除一些区间使得不存在三个相交的区间.找出删除的最少区间. 分析 是个比较显然的贪心吧. 先按照区间的左起点进行排序,然后从左往右扫,当有三个区间相交的时候,删除那个右端点最远的 ...

  6. ZOJ-3953 Intervals,t

    Intervals 题意:给出n个区间,求最少删除多少个区间使得任意三个区间都不相交. 思路:按区间左端点排序,每次选取r最大的两个与当前比较,如果能放更新r,否则删除r最大的.关键就在怎么删除r最大 ...

  7. {POJ}{动态规划}{题目列表}

    动态规划与贪心相关: {HDU}{4739}{Zhuge Liang's Mines}{压缩DP} 题意:给定20个点坐标,求最多有多少个不相交(点也不相交)的正方形 思路:背包问题,求出所有的正方形 ...

  8. Codeforces Gym 100231B Intervals 线段树+二分+贪心

    Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...

  9. Integer Intervals(贪心)

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12123   Accepted: 5129 Description An i ...

  10. poj 1716 Integer Intervals (差分约束 或 贪心)

    Integer Intervals Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12192   Accepted: 514 ...

随机推荐

  1. 「CTS2019 | CTSC2019」氪金手游 解题报告

    「CTS2019 | CTSC2019」氪金手游 降 智 好 题 ... 考场上签到失败了,没想容斥就只打了20分暴力... 考虑一个事情,你抽中一个度为0的点,相当于把这个点删掉了(当然你也只能抽中 ...

  2. 【LeetCode 26】删除排序数组中的重复项

    题目链接 [题解] 沙比提 [代码] class Solution { public: int removeDuplicates(vector<int>& nums) { if ( ...

  3. Luogu P2269 [HNOI2002]高质量的数据传输

    这题给大家提供一下思路~ (为不想贴代码找借口) 声明:两个思路都是正确的,并且都AC了.(逃) 总体布局 求传输失败率\(1-\prod(1-p_{i})\)最小就是求 传输成功率\(\prod(1 ...

  4. js 将字符串当作js表达式执行方法

    听同事说了一个需求.他有一个数据对象obj,接口会给他返回一个索引key,这个key长度不固定,根据这个key去修改obj对应的值. 举个例子: let obj={"level1" ...

  5. 微信小程序学习笔记(一)--创建微信小程序

    一.创建小程序 1.申请帐号.安装及创建小程序,请参照官方文档里面的操作 https://developers.weixin.qq.com/miniprogram/dev/. 小程序在创建的时候会要求 ...

  6. Python中练习题关于内置函数的getattr,setattr问题

    执行一下代码的结果是? class A: def __init__(self,a,b,c): self.x = a+b+c a = A(1,2,3) b = getattr(a,'x') setatt ...

  7. 机器学习技法笔记:Homework #5 特征变换&Soft-Margin SVM相关习题

    原文地址:https://www.jianshu.com/p/6bf801bdc644 特征变换 问题描述 程序实现 # coding: utf-8 import numpy as np from c ...

  8. POJ3630-Phone List-Trie字典树模板题

    Given a list of phone numbers, determine if it is consistent in the sense that no number is the pref ...

  9. 1022 Digital Library (30 分)

    1022 Digital Library (30 分)   A Digital Library contains millions of books, stored according to thei ...

  10. 【Java多线程系列随笔二】BlockingQueue

    前言: 在新增的Concurrent包中,BlockingQueue很好的解决了多线程中,如何高效安全“传输”数据的问题.通过这些高效并且线程安全的队列类,为我们快速搭建高质量的多线程程序带来极大的便 ...