http://codeforces.com/contest/612/problem/D

题目大意:给你n个区间,这n个区间会有相交的部分,如果一个区间相交的部分>=k,那么就把这个区间记录下来。且要求所记录的区间个数尽量少(换句话说就是区间尽量大),问有几个这样的区间,并且输出这样的区间。

思路一:

我之前的想法就是把所有的sort出来,然后用树状数组来求区间相交个数。但是这样子写出来以后可能会出现这样的区间,即[1,3][3,5],所以我们又要把这两个区间合并一下,所以合并的时候有需要使用线段树来合并区间。理论上来说,这样子是可行的,就是太麻烦了。

于是接下来是官方题解。

思路二:

我们把所有的元素预处理出来以后sort,然后我们发现题干所需要求的是>=k即可,那么我们可以发现,区间如果要是最长的话,那么必然是cnt=k的时候,所以我们只需要在sort以后,O(n)去获得这个cnt即可。在[left, right]为left的时候cnt++,right的时候cnt--,然后统计一下cnt==k的时候就能够得到最大的区间了。

//看看会不会爆int!数组会不会少了一维!
//取物问题一定要小心先手胜利的条件
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker,"/STACK:102400000,102400000")
#define LL long long
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define haha printf("haha\n")
const int maxn = 1e6 + ;
int n, k;
pair<int, int> p[maxn];
vector<pair<int, int> > ve;
int a[maxn]; int main(){
cin >> n >> k;
for (int i = ; i <= n; i++){
int l, r; scanf("%d%d", &l, &r);
ve.push_back(mk(l, ));
ve.push_back(mk(r, ));
p[i] = mk(l, r);
}
sort(ALL(ve));
vector<pair<int, int> > ans;
int cnt = , tmp;
for (int i = ; i < ve.size(); i++){
int ty = ve[i].se;
int val = ve[i].fi;
if (ty == ){
cnt++;
if (cnt == k){
tmp = val;
}
}
if (ty == ){
if (cnt == k){
ans.pb(mk(tmp, val));
}
cnt--;
}
}
printf("%d\n", ans.size());
for (int i = ; i < ans.size(); i++){
printf("%d %d\n", ans[i].fi, ans[i].se);
}
return ;
}

k个区间相交的段落数 Educational Codeforces Round 4 D的更多相关文章

  1. Educational Codeforces Round 58 (Rated for Div. 2) 题解

    Educational Codeforces Round 58 (Rated for Div. 2)  题目总链接:https://codeforces.com/contest/1101 A. Min ...

  2. Educational Codeforces Round 64(ECR64)

    Educational Codeforces Round 64 CodeForces 1156A 题意:1代表圆,2代表正三角形,3代表正方形.给一个只含1,2,3的数列a,ai+1内接在ai内,求总 ...

  3. Educational Codeforces Round 37

    Educational Codeforces Round 37 这场有点炸,题目比较水,但只做了3题QAQ.还是实力不够啊! 写下题解算了--(写的比较粗糙,细节或者bug可以私聊2333) A. W ...

  4. Educational Codeforces Round 35 (Rated for Div. 2)

    Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...

  5. Educational Codeforces Round 63 (Rated for Div. 2) 题解

    Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...

  6. Educational Codeforces Round 40 F. Runner's Problem

    Educational Codeforces Round 40 F. Runner's Problem 题意: 给一个$ 3 * m \(的矩阵,问从\)(2,1)$ 出发 走到 \((2,m)\) ...

  7. Educational Codeforces Round 65 (Rated for Div. 2)题解

    Educational Codeforces Round 65 (Rated for Div. 2)题解 题目链接 A. Telephone Number 水题,代码如下: Code #include ...

  8. Educational Codeforces Round 69 D E

    Educational Codeforces Round 69 题解 题目编号 A B C D E F 完成情况 √ √ √ ★ ★ - D. Yet Another Subarray Problem ...

  9. Educational Codeforces Round 63部分题解

    Educational Codeforces Round 63 A 题目大意就不写了. 挺简单的,若果字符本来就单调不降,那么就不需要修改 否则找到第一次下降的位置和前面的换就好了. #include ...

随机推荐

  1. Swift3GCD

    GCD的使用在Swift3中的方法 //串行队列 let q:DispatchQueue = DispatchQueue(label: "xiaosi") //并发队列 qos : ...

  2. Java的URL来下载网页源码

    import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.IOException; impor ...

  3. VB ListBox 添加横向滚动条

    Private Declare Function SendMessage Lib "user32 " Alias "SendMessageA" (ByVal h ...

  4. Java List&Map简单初始化方法

    Java中List与Map初始化的一些写法 // InitCollections.java - sample of init collect package com.util; import java ...

  5. OOP in JS Public/Private Variables and Methods

    Summary private variables are declared with the 'var' keyword inside the object, and can only be acc ...

  6. 树莓派+Android Things

    在开始之前 谷歌前不久发布了Android Things面向物联网的系统,用意是想让android开发者用原来开发app的方式开发硬件相关的应用,扩展了android开发的方向和前景,而谷歌的Andr ...

  7. php MYSQL 一条语句中COUNT出不同的条件

    SELECT DISTINCT c.uid, count( 1 ) AS zongji, count( if( task_type = 'mobile', true, NULL ) ) AS mobi ...

  8. react视频入门

    http://pan.baidu.com/s/1i46by8t     密码:48tt

  9. C# 深入了解泛型

    本文是根据网上&书本总结来的. 1. 介绍 泛型程序设计是程序设计语言的一种风格或范式. 泛型允许程序员在强类型程序设计语言中编写代码时使用一些以后才指定的类型,在实例化时(instantia ...

  10. Linux用户相关命令

    1.建用户: adduser snailz //新建用户 snailz passwd snailz //给用户 snailz 设置密码 2.建工作组 groupadd test //新建test工作组 ...