题目传送门

题意:现在有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的更多相关文章

  1. 2018 青岛ICPC区域赛E ZOJ 4062 Plants vs. Zombie(二分答案)

    Plants vs. Zombies Time Limit: 2 Seconds      Memory Limit: 65536 KB BaoBao and DreamGrid are playin ...

  2. ACM-ICPC 2018 青岛赛区现场赛 K. Airdrop && ZOJ 4068 (暴力)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4068 题意:吃鸡游戏简化为二维平面上有 n 个人 (xi,yi) ...

  3. ACM-ICPC 2018 青岛赛区现场赛 D. Magic Multiplication && ZOJ 4061 (思维+构造)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4061 题意:定义一个长度为 n 的序列 a1,a2,..,an ...

  4. ACM ICPC 2018 青岛赛区 部分金牌题题解(K,L,I,G)

     目录: K Airdrop I Soldier Game L Sub-cycle Graph G Repair the Artwork ———————————————————— ps:楼主脑残有点严 ...

  5. ACM-ICPC 2018青岛网络赛-H题 Traveling on the Axis

    题目:略(不知道怎么从ZOJ搬题) 地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4054 把这题的每个点分成两种情况 ...

  6. 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 ...

  7. 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$), ...

  8. 2018青岛网络赛G - Couleur 区间上的启发式合并

    题意:给出\(a[1...n]\),共\(n\)次操作,每次删除一个位置\(p_i\)(强制在线),此时区间会变为两个分离的区间,求每次操作的最大区间逆序对 首先要知道必要的工具,按权值建立的主席树可 ...

  9. ACM-ICPC 2018 青岛赛区网络预赛 J. Press the Button(数学)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4056 题意:有一个按钮,时间倒计器和计数器,在时间[0,t]内, ...

随机推荐

  1. 曹工杂谈:Linux服务器上,Spring Boot 原地修改 jar 包配置文件/替换class文件,免去重复上传的麻烦

    一.前言 相信很多同学有这样的需求,现在很多公司都有多地的研发中心,经常需要跨地区部署,比如,博主人在成都,但是服务器是北京的.一般城市间网络都不怎么好,上传一个几十兆的jar包那是真的慢,别说现在微 ...

  2. Mobile game forensics

    My friend Carrie'd like to know "Garena 传说对决" violates any mobile risks such as insecure d ...

  3. Linux及Windows下ActiveMQ下载与安装教程

    原文连接:(http://www.studyshare.cn/blog-front//blog/details/1170/0 )一.下载 Windows: 1.官网下载地址:这里 2.百度网盘下载:这 ...

  4. 4如何用PHP给MySQL数据库添加记录

    首先连接数据库(依旧用第二篇的方法) 假设数据库表里只有id,name,email三列 添加以下代码 $inputemail=写你要的email;$inputname=写你要的name;//先设定你要 ...

  5. Centos安装git并配置ssh

    1.下载git安装包 git-2.9.4.tar.gz 2.解压 tar -xzvf git-2.9.4.tar.gz 3.修改解压后的文件名 mv git-2.9.4 git 4.安装git依赖的库 ...

  6. ThreadLocal线程隔离

    package com.cookie.test; import java.util.concurrent.atomic.AtomicInteger; /** * author : cxq * Date ...

  7. 理解MySQL(一)--MySQL介绍

    一.Mysql逻辑架构: 1. 第一层:服务器层的服务,连接\线程处理. 2. 第二层:查询执行引擎,MySQL的核心服务功能,包括查询解析.分析.优化和缓存,所有跨存储引擎的功能都在这一层实现. 3 ...

  8. 算法与数据结构基础 - 回溯(Backtracking)

    回溯基础 先看一个使用回溯方法求集合子集的例子(78. Subsets),以下代码基本说明了回溯使用的基本框架: //78. Subsets class Solution { private: voi ...

  9. Python 学习笔记(6)— 字符串格式化

    字符串格式化处理 远古写法 以前通常使用运算符号 % ,%s 插入的值 String 类型,%.3f 指插入的值为包含 3 位小数的浮点数: format1 = "%s, %s!" ...

  10. if else 深度优化

    一. if else表达式过于复杂 if ((condition1 && condition2 ) || ((condition2 || condition3) && ...