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. 安装nginx 以及nginx负载均衡

    a  安装 [root@localhost ~]# yum -y install gcc automake autoconf libtool make yum install gcc gcc-c++ ...

  2. 016:URL命名与反转URL

    为什么需要URL命名? 主要解决蛋疼url变化情况,比如:哪天项目经理或领导过来说,把login改成signin,把register改成signup等蛋疼的需求——因为一旦改了url后,相关视图函数里 ...

  3. 执行jar包某类命令

    #!/bin/bash ulimit -SHn 65535 export LC_ALL=en_US.UTF-8 export LANG=en_US.UTF-8 library_path="$ ...

  4. BZOJ 3197: [Sdoi2013]assassin 树形DP + 最小费用流 + 树的同构

    Description Input Output 其实就是给出两颗树,求一种两种树同构的方式,使得不同颜色个数最少$.$树的重新构建,其实就是指定不同的点为根节点$.$ 好在树的重心有一个重要的性质: ...

  5. Django简单操作

    一.静态文件配置 静态文件配置 STATIC_URL = '/static/' STATICFILES_DIRS = [ os.path.join(BASE_DIR,'static') ] # 暴露给 ...

  6. 【商业智能VS人工智能】

    什么是智能? 从感觉到记忆到思维这一过程,称为“智慧”,智慧的结果就产生了行为和语言,将行为和语言的表达过程称为“能力”,两者合称“智能”,将感觉.去记.回忆.思维.语言.行为的整个过程称为智能过程, ...

  7. (一)SQL -- 基础知识

    SQL是一个标准的数据库语言,是面向集合的描述性非过程化语言. 优点:功能强.效率高.简单易学易维护. 缺点:非过程化语言,大多数语言都是独立执行,与上下文无关,而大多数 应用都是一个完整的过程,显然 ...

  8. mysql 数据增删改的总结

    一.在MySQL管理软件中,可以通过SQL语句中的DML语言来实现数据的操作,包括 1.使用INSERT实现数据的插入2.UPDATE实现数据的更新3.使用DELETE实现数据的删除4.使用SELEC ...

  9. 线下作业MySQL #20175201

    1.下载附件中的world.sql.zip, 参考http://www.cnblogs.com/rocedu/p/6371315.html#SECDB,导入world.sql,提交导入成功截图 2.编 ...

  10. HDU 2243 ( Trie图 矩阵构造幂和 )

    题意 :  长度不超过L,只由小写字母组成的,至少包含一个词根的单词,一共可能有多少个呢?这里就不考虑单词是否有实际意义. 比如一共有2个词根 aa 和 ab ,则可能存在104个长度不超过3的单词, ...