[AGC040B]Two Contests
Description
给出若干条线段 \((L[i], R[i])\) ,把他们分成两个非空的集合,最大化集合内线段交的和。
\(n\le 10 ^ 5\)
Solution
考虑最小的一个右端点 p 和最大的一个左端点 q 。
讨论:
- p 和 q 在同一集合内,那么选择一条除了这两条外最长的线段单独一个集合,剩下的和 p, q 一起一个集合。
- 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的更多相关文章
- Codeforces 1077E Thematic Contests(二分)
题目链接:Thematic Contests 题意:给出n道有类型的题目,每天组织一场专题比赛,该天题目数量必须是前一天的2倍,第一天的题目数量可以任意选择,求能消耗的最多题目数量. 题解:先整理成不 ...
- CodeForces Round #521 (Div.3) E. Thematic Contests
http://codeforces.com/contest/1077/problem/E output standard output Polycarp has prepared nn competi ...
- Team Contests - Warmup(2016年多校热身赛,2016年黑龙江省赛)
Team Contests - Warmup A 题意:... 思路:不会 代码:... 随机 B 题意:给n个点,问是否有一个圆上有最少n/3个点 思路:随机大法好. 代码:... 递推 C 题意: ...
- Codeforces Round #521 (Div. 3) E. Thematic Contests(思维)
Codeforces Round #521 (Div. 3) E. Thematic Contests 题目传送门 题意: 现在有n个题目,每种题目有自己的类型要举办一次考试,考试的原则是每天只有一 ...
- K - Two Contests
题目连接:https://atcoder.jp/contests/agc040/tasks/agc040_b 大佬题解:https://blog.csdn.net/duanghaha/article/ ...
- 2016 Multi-University Training Contests
2016 Multi-University Training Contest 1 2016 Multi-University Training Contest 2 2016 Multi-Univers ...
- Codeforces Round #235 (Div. 2) B. Sereja and Contests
#include <iostream> #include <vector> #include <algorithm> using namespace std; in ...
- Introduction to Programming Contests (stanford)
http://web.stanford.edu/class/cs9http://web.stanford.edu/class/cs97si/7si/
- UVALive 8519 Arrangement for Contests 2017西安区域赛H 贪心+线段树优化
题意 等价于给一个数列,每次对一个长度为$K$的连续区间减一 为最多操作多少次 题解: 看样例猜的贪心,10分钟敲了个线段树就交了... 从1开始,找$[i,i+K]$区间的最小值,然后区间减去最小值 ...
随机推荐
- 【NOIP2016提高A组集训第1场10.29】配对游戏
题目 流行的跳棋游戏是在一个有mn个方格的长方形棋盘上玩的.棋盘起初全部被动物或障碍物占满了.在一个方格中,'X'表示一个障碍物,一个'0'-'9'的个位数字表示一个不同种类的动物,相同的个位数字表示 ...
- 【leetcode】638. Shopping Offers
题目如下: In LeetCode Store, there are some kinds of items to sell. Each item has a price. However, ther ...
- POJ 2155 Matrix (树状数组 && 区间计数)
题意 : 给出一个N*N的矩阵, 矩阵只有可能包含0或1, 一开始则全部是0.对于矩阵可以进行两种操作, 第一种是输入 C x1 y1 x2 y2 表示, 对以(x1, y1)为左上角, 以(x2, ...
- Starting MySQL... ERROR! The server quit without updating PID file (/usr/local/mysql/data/VM_0_6_centos.pid)
刚接触MySql数据库,参考一些文章后搭建起来了也创建了数据库,程序跑到很好,一觉醒来突然连接不上了 MySql数据库了. 研究了好一会才找到原因. 现象: 登录数据库失败 [root@VM_0_6_ ...
- tomcat8.5部署管理控制台
1.修改 conf/tomcat-users.xml 文件 <role rolename="manager-gui"/> <role rolename=" ...
- mysql学生成绩排名,分组取前 N 条记录
转载 https://blog.csdn.net/jslcylcy/article/details/72627762 score表: CREATE TABLE `score` ( `student_ ...
- 在配置em时运到报错ORACLE_UNQNAME not defined
Oracle 11G R2 RAC 配置em时报错 Environment variable ORACLE_UNQNAME not defined. oracle ORACLE_UNQNAME 借楼主 ...
- mysq远程连接报错,host..
在本机登入mysql后,更改"mysql"数据库里的"user"表里的"host"项,从"localhost"改为'%' ...
- python读取文件时遇到非法字符的处理 UnicodeDecodeError: 'gbk' codec can't decode bytes in position
报错UnicodeDecodeError: 'gbk' codec can't decode bytes in position ipath = 'D:/学习/语料库/SogouC.mini/Samp ...
- JS数组方法的的返回值和是否改变该数组总结
concat() 方法 concat() 方法用于连接两个或多个数组. 该方法不会改变现有的数组,而仅仅会返回被连接数组的一个副本. 返回值 返回一个新的数组.该数组是通过把所有 arrayX 参数添 ...