Description

给出若干条线段 \((L[i], R[i])\) ,把他们分成两个非空的集合,最大化集合内线段交的和。

\(n\le 10 ^ 5\)

Solution

考虑最小的一个右端点 p 和最大的一个左端点 q 。

讨论:

  1. p 和 q 在同一集合内,那么选择一条除了这两条外最长的线段单独一个集合,剩下的和 p, q 一起一个集合。
  2. p 和 q 不在同一集合内,那么考虑令 \(a_i = max(p - l_i+1, 0), b_i = max(r_i - q + 1, 0)\) , 问题就转换成了把 {1,2...n} 划分成两个集合 \(S, T\) ,并且最大化 \(min\{a_i\} + min\{b_j\}\), \(i\in S, j \in T\),可以把 \((a_i, b_i)\) 按 \(a_i\) 从大到小排序,然后 \(T\) 选的就是一段后缀。

code

#include <cstdio>
#include <cstring>
#include <vector>
#include <iostream>
#include <algorithm> using namespace std; #define End exit(0)
#define LL long long
#define mp make_pair
#define SZ(x) ((int) x.size())
#define GO cerr << "GO" << endl
#define DE(x) cout << #x << " = " << x << endl
#define DEBUG(...) fprintf(stderr, __VA_ARGS__) void proc_status()
{
freopen("/proc/self/status","r",stdin);
string s; while(getline(cin, s)) if (s[2] == 'P') { cerr << s << endl; return; }
} template<typename T> inline T read()
{
register T x = 0;
register char c; register int f(1);
while (!isdigit(c = getchar())) if (c == '-') f = -1;
while (x = (x << 1) + (x << 3) + (c ^ 48), isdigit(c = getchar()));
return x * f;
} template<typename T> inline bool chkmin(T &a,T b) { return a > b ? a = b, 1 : 0; }
template<typename T> inline bool chkmax(T &a,T b) { return a < b ? a = b, 1 : 0; } const int maxN = 1e5 + 2; struct Info
{
int a, b;
bool operator > (const Info& B) const {
return a > B.a;
}
} info[maxN + 2]; int n, N;
int L[maxN + 2], R[maxN + 2]; void input()
{
n = read<int>();
for (int i = 1; i <= n; ++i)
L[i] = read<int>(), R[i] = read<int>();
} int solve()
{
int p = 1e9, q = 0;
for (int i = 1; i <= n; ++i) chkmin(p, R[i]);
for (int i = 1; i <= n; ++i) chkmax(q, L[i]); for (int i = 1; i <= n; ++i) info[i].a = max(p - L[i] + 1, 0);
for (int i = 1; i <= n; ++i) info[i].b = max(R[i] - q + 1, 0); sort(info + 1, info + 1 + n, greater<Info>()); static int suf[maxN + 2]; suf[n + 1] = 1e9;
for (int i = n; i >= 1; --i) suf[i] = min(suf[i + 1], info[i].b); int ans = 0;
for (int i = 1; i <= n; ++i) chkmax(ans, max(p - q + 1, 0) + R[i] - L[i] + 1);
for (int i = 1; i < n; ++i) chkmax(ans, suf[i + 1] + info[i].a); return ans;
} int main()
{
#ifndef ONLINE_JUDGE
freopen("xhc.in", "r", stdin);
freopen("xhc.out", "w", stdout);
#endif
input();
printf("%d\n", solve());
return 0;
}

[AGC040B]Two Contests的更多相关文章

  1. Codeforces 1077E Thematic Contests(二分)

    题目链接:Thematic Contests 题意:给出n道有类型的题目,每天组织一场专题比赛,该天题目数量必须是前一天的2倍,第一天的题目数量可以任意选择,求能消耗的最多题目数量. 题解:先整理成不 ...

  2. CodeForces Round #521 (Div.3) E. Thematic Contests

    http://codeforces.com/contest/1077/problem/E output standard output Polycarp has prepared nn competi ...

  3. Team Contests - Warmup(2016年多校热身赛,2016年黑龙江省赛)

    Team Contests - Warmup A 题意:... 思路:不会 代码:... 随机 B 题意:给n个点,问是否有一个圆上有最少n/3个点 思路:随机大法好. 代码:... 递推 C 题意: ...

  4. Codeforces Round #521 (Div. 3) E. Thematic Contests(思维)

    Codeforces Round #521 (Div. 3)  E. Thematic Contests 题目传送门 题意: 现在有n个题目,每种题目有自己的类型要举办一次考试,考试的原则是每天只有一 ...

  5. K - Two Contests

    题目连接:https://atcoder.jp/contests/agc040/tasks/agc040_b 大佬题解:https://blog.csdn.net/duanghaha/article/ ...

  6. 2016 Multi-University Training Contests

    2016 Multi-University Training Contest 1 2016 Multi-University Training Contest 2 2016 Multi-Univers ...

  7. Codeforces Round #235 (Div. 2) B. Sereja and Contests

    #include <iostream> #include <vector> #include <algorithm> using namespace std; in ...

  8. Introduction to Programming Contests (stanford)

    http://web.stanford.edu/class/cs9http://web.stanford.edu/class/cs97si/7si/

  9. UVALive 8519 Arrangement for Contests 2017西安区域赛H 贪心+线段树优化

    题意 等价于给一个数列,每次对一个长度为$K$的连续区间减一 为最多操作多少次 题解: 看样例猜的贪心,10分钟敲了个线段树就交了... 从1开始,找$[i,i+K]$区间的最小值,然后区间减去最小值 ...

随机推荐

  1. Mac系统Pycharm永久激活

    网上找了很多Pycharm永久激活的方法,前面几步几乎都一样,最后激活的那步却总行不通,于是这边记录下 一.本人下载的是2018.2.7版本,官方有很多版本可供下载,下载地址http://www.je ...

  2. 【NOIP2016提高A组模拟9.17】数格子

    题目 分析 设表示每一行的状态,用一个4位的二进制来表示,当前这一行中的每一个位数对下一位有没有影响. 设\(f_{i,s}\)表示,做完了的i行,其状态为s,的方案数. 两个状态之间是否可以转移就留 ...

  3. 【leetcode】Global and Local Inversions

    题目如下: We have some permutation A of [0, 1, ..., N - 1], where N is the length of A. The number of (g ...

  4. 【leetcode】Reorganize String

    题目如下: Given a string S, check if the letters can be rearranged so that two characters that are adjac ...

  5. 大数乘法(A * B Problem Plus)问题

    大数乘法问题一般可以通过将大数转换为数组来解决. 解题思路 第1步 第2步 第3步 第4步 样例输入1 56 744 样例输出1 800 样例输入2 -10 678 样例输出2 -6780 样例输入3 ...

  6. FJWC2017&FJOI2017一试 游记

    day1 ​ 早上是以前泉州七中的杨国烨讲课.(据说当时看新闻说是一对双胞胎一起上thu的其中一个)课题是图论/网络流. ​ 下午第一道一开始推出来了一个之和面积有关的式子,然后觉得可以容斥一发,觉得 ...

  7. vue中的methods,conputed,watcher

    todo 1,computed(计算属性) 属性的结果会被缓存, 除非依赖的响应式属性变化才会重新计算, 主要 当做属性来使用;(虽然是个方法,但是当做属性使用)(场景:引用一些经过操作的数据) co ...

  8. python学习之路(23)

    类和实例 面向对象最重要的概念就是类(Class)和实例(Instance),必须牢记类是抽象的模板,比如Student类,而实例是根据类创建出来的一个个具体的“对象”,每个对象都拥有相同的方法,但各 ...

  9. 「PKUSC 2018」真实排名

    题目链接 戳我 \(Solution\) 我们将现在所要进行的数设为\(now\) 我们分情况讨论一下 他自己不翻倍 他自己翻倍 我们首先来看看\(1\)操作 如果要满足他对排名没有影响,那么不能进行 ...

  10. JDK1.7安装配置环境变量+图文说明Jmeter安装

    Jmeter通常用于并发测试,本文介绍Jmeter工具的安装步骤. 工具/原料   WIN7 Jmeter安装包 JDK 一.安装JDK   1 [步骤一]安装jdk 1.下载jdk,到官网下载jdk ...