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. Ddos 攻击

    title: Ddos 防御相关 tags: Ddos, 安全, 防御 grammar_cjkRuby: true --- 防御基础 1.1. 攻击流量到底多大 谈到DDoS防御,首先就是要知道到底遭 ...

  2. Gs_Class._BaseQueryWeb查询页面基类(aspx.net)

    using System;using System.Data;using System.Configuration;using System.Collections;using System.Web; ...

  3. ContourLine

    #define MULTI_PLOT true //Determine whether or not to plot multiple iterations. #define X_MAX 1.0 // ...

  4. Swift中GCD与NSOperation相关

    GCD Swift 3必看:从使用场景了解GCD新API 常用写法: dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_ ...

  5. 面试题-Java基础-开发环境

    1.什么是Java虚拟机?为什么Java被称作是“平台无关的编程语言”? Java虚拟机是一个可以执行Java字节码的虚拟机进程.Java源文件被编译成能被Java虚拟机执行的字节码文件.Java被设 ...

  6. CodeForces 546D

    Description 两个士兵在玩一个游戏,开始的时候第一个士兵选择一个数n,并把这个数交给第二个士兵,第二个士兵必须选择一个x满足x>1 且n能被x整除,然后将n变为n/x,然后把这个数交给 ...

  7. C#第十天

    1.c#中的访问修饰符 public :公开的公共的 private:私有的,只能在当前类的内部访问 protected:受保护的,只能在当前类的内部以及该类的子类中访问. internal:只能在当 ...

  8. .net后台代码临时表创建

    写法一: var dt = new DataTable(); dt.Columns.Add(new DataColumn("Id", System.Type.GetType(&qu ...

  9. Python学习日志(一)

    1.os - Normal Method: os.name() : os.getcwd(): 给出当前的目录,python当前的工作目录 os.listdir(): 返回 os.remove():删除 ...

  10. 分布式缓存HttpRuntime.cache应用到单点登陆中_优化登陆

    以前的设计方案,是我们在数据库中放一个表,用作存储验证登陆成功的用户,并且生成用户TOKEN(令牌) 分布式缓存+集群的解决方案图: 相应的代码: DE层中配置文件: receiveTimeout=& ...