zoj 5823 Soldier Game 2018 青岛 I
题意:现在有n个人,现在可以把这n个人分成若干组,只有连续的人才能被分为一组,并且一个组内最多2个人,现在问你 所有组内的最大值-最小值 这个差值最小是多少。
题解:
将每个人的情况3种情况都拿出来,把这些所有的情况从小到大排序,然后我们枚举起点,然后一直不停的添加线段,然后直到当前的区间内的所有线段,我们可以选择其中的一部分,使得这些线段能够不重合的前提下巧好覆盖1-n的区间内。
tree[x] 假设管理的区间为 [l, r] tree[x][i][j] i代表的是左端点 j 代表的是右端点, 当i == 1 的时候,j == 1的时候,我们管理的是 [l+1,r+1]这个区间的状态 i == 0,j == 0 管理的是 [l, r]这个区间的状态。
所以我们pushup的时候不重合的搞就好了。
代码:
#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lch(x) tr[x].son[0]
#define rch(x) tr[x].son[1]
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL mod = (int)1e9+;
const int N = 1e5 + ;
int a[N][];
pll p[N<<];
bool cmp(pll & x, pll & y){
return a[x.fi][x.se] < a[y.fi][y.se];
}
int tree[N<<][][];
void Build(int l, int r, int rt){
tree[rt][][] = tree[rt][][] = tree[rt][][] = tree[rt][][] = ;
if(l == r) {
tree[rt][][] = ;
return ;
}
int m = l+r >> ;
Build(lson); Build(rson);
}
void PushUp(int rt){
tree[rt][][] = (tree[rt<<][][] && tree[rt<<|][][]) || (tree[rt<<][][] && tree[rt<<|][][]);
tree[rt][][] = (tree[rt<<][][] && tree[rt<<|][][]) || (tree[rt<<][][] && tree[rt<<|][][]);
tree[rt][][] = (tree[rt<<][][] && tree[rt<<|][][]) || (tree[rt<<][][] && tree[rt<<|][][]);
tree[rt][][] = (tree[rt<<][][] && tree[rt<<|][][]) || (tree[rt<<][][] && tree[rt<<|][][]);
}
void Update(int L, int op,int l, int r, int rt){
if(l == r){
tree[rt][][op] ^= ;
return ;
}
int m = l+r >> ;
if(L <= m) Update(L, op, lson);
else Update(L, op, rson);
PushUp(rt);
}
int main(){
int T, n;
scanf("%d", &T);
while(T--){
scanf("%d", &n);
int m = ;
for(int i = ; i <= n; ++i){
scanf("%d", &a[i][]);
p[++m] = pll(i,);
if(i-) {
a[i-][] = a[i-][] + a[i][];
p[++m] = pll(i-,);
}
}
sort(p+, p++m, cmp);
Build(,n,);
LL ans = INF;
for(int i = , j=; i <= m; ++i){
while(j <= m && !tree[][][]){
Update(p[j].fi, p[j].se, , n, );
j++;
}
if(tree[][][]) ans = min(ans, 1ll*a[p[j-].fi][p[j-].se] - a[p[i].fi][p[i].se]);
Update(p[i].fi, p[i].se, , n, );
}
printf("%lld\n", ans);
}
return ;
}
zoj 5823 Soldier Game 2018 青岛 I的更多相关文章
- 2018 青岛ICPC区域赛E ZOJ 4062 Plants vs. Zombie(二分答案)
Plants vs. Zombies Time Limit: 2 Seconds Memory Limit: 65536 KB BaoBao and DreamGrid are playin ...
- ACM-ICPC 2018 青岛赛区现场赛 K. Airdrop && ZOJ 4068 (暴力)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4068 题意:吃鸡游戏简化为二维平面上有 n 个人 (xi,yi) ...
- ACM-ICPC 2018 青岛赛区现场赛 D. Magic Multiplication && ZOJ 4061 (思维+构造)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4061 题意:定义一个长度为 n 的序列 a1,a2,..,an ...
- ACM ICPC 2018 青岛赛区 部分金牌题题解(K,L,I,G)
目录: K Airdrop I Soldier Game L Sub-cycle Graph G Repair the Artwork ———————————————————— ps:楼主脑残有点严 ...
- ACM-ICPC 2018青岛网络赛-H题 Traveling on the Axis
题目:略(不知道怎么从ZOJ搬题) 地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4054 把这题的每个点分成两种情况 ...
- ZOJ 4063 - Tournament - [递归][2018 ACM-ICPC Asia Qingdao Regional Problem F]
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4063 Input Output Sample Input 2 3 ...
- ZOJ 4067 - Books - [贪心][2018 ACM-ICPC Asia Qingdao Regional Problem J]
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4067 题意: 给出 $n$ 本书(编号 $1 \sim n$), ...
- 2018青岛网络赛G - Couleur 区间上的启发式合并
题意:给出\(a[1...n]\),共\(n\)次操作,每次删除一个位置\(p_i\)(强制在线),此时区间会变为两个分离的区间,求每次操作的最大区间逆序对 首先要知道必要的工具,按权值建立的主席树可 ...
- ACM-ICPC 2018 青岛赛区网络预赛 J. Press the Button(数学)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4056 题意:有一个按钮,时间倒计器和计数器,在时间[0,t]内, ...
随机推荐
- poj 1050 To the Max(最大子矩阵之和)
http://poj.org/problem?id=1050 我们已经知道求最大子段和的dp算法 参考here 也可参考编程之美有关最大子矩阵和部分. 然后将这个扩大到二维就是这道题.顺便说一下,有 ...
- python协程详解
目录 python协程详解 一.什么是协程 二.了解协程的过程 1.yield工作原理 2.预激协程的装饰器 3.终止协程和异常处理 4.让协程返回值 5.yield from的使用 6.yield ...
- JDK的命令行工具系列 (二) javap、jinfo、jmap
javap: 反编译工具, 可用来查看java编译器生成的字节码 参数摘要: -help 帮助 -l 输出行和变量的表 -public 只输出public方法和域 -protected 只输出publ ...
- Unity经典游戏编程之:球球大作战
版权声明: 本文原创发布于博客园"优梦创客"的博客空间(网址:http://www.cnblogs.com/raymondking123/)以及微信公众号"优梦创客&qu ...
- 【转】C++文件读写详解(ofstream,ifstream,fstream)
转:http://blog.csdn.net/kingstar158/article/details/6859379 摘要:具体用法,上面链接中,文章写的很详细,讲解ofstream,ifstream ...
- react-navigation报错
用react-navigation配置路由时,出现如下报错或白屏. 我的代码原来是 import {AppRegistry} from 'react-native'; import App from ...
- 汇总VSCode中比较好用的插件
使用vscode编辑器两年的时间,总结出前端一些比较方便的插件 1. Auto Close Tag 自动添加HTML / XML关闭标签 2. Auto Complete Tag 自动完成标签 3 A ...
- CodeForces 427D Match & Catch
洛谷题目页面传送门 & CodeForces题目页面传送门 给定\(2\)个字符串\(a,b,|a|=n,|b|=m\),求最长的既在\(a\)中出现恰好\(1\)次又在\(b\)中出现恰好\ ...
- Javascript中,实现类与继承的方法和优缺点分析
Javascript是一种弱类型语言,不存在类的概念,但在js中可以模仿类似于JAVA中的类,实现类与继承 第一种方法:利用Javascript中的原型链 //首先定义一个父类 function An ...
- Docker之- 使用Docker 镜像和仓库
目录 使用Docker 镜像和仓库 什么是 Docker 镜像 列出 Docker 镜像 tag 标签 Docker Hub 拉取镜像 查找镜像 构建镜像 创建Docker Hub 账号 使用 Doc ...