http://codeforces.com/contest/722/problem/C

题目大意:给你一个串,每次删除串中的一个pos,问剩下的串中,连续的最大和是多少。

思路一:正方向考虑问题,那么就线段树+分类讨论一下就好了,然后代码中flag表示能否转移

//看看会不会爆int!数组会不会少了一维!
//取物问题一定要小心先手胜利的条件
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define haha printf("haha\n")
const int maxn = + ;
struct Tree{
bool lf, rf;
bool flag;
LL val, lval, rval;
}tree[maxn << ];
int n;
int b[maxn]; inline void push_up(int o){
int lb = o << , rb = o << | ;
if (tree[lb].flag && tree[rb].flag){
tree[o].lf = tree[o].rf = true;
tree[o].lval = tree[o].rval = tree[o].val = tree[lb].val + tree[rb].val;
return ;
}
tree[o].flag = false; if (tree[lb].lf == false) tree[o].lf = false, tree[o].lval = ;
else {
tree[o].lf = true;
if (tree[lb].flag && tree[rb].lf) tree[o].lval = tree[lb].lval + tree[rb].lval;
else tree[o].lval = tree[lb].lval;
} if (tree[rb].rf == false) tree[o].rf = false, tree[o].rval = ;
else {
tree[o].rf = true;
if (tree[rb].flag && tree[lb].rf) tree[o].rval = tree[rb].rval + tree[lb].rval;
else tree[o].rval = tree[rb].rval;
} tree[o].val = max(tree[lb].val, tree[rb].val);
if (tree[lb].rf && tree[rb].lf){
tree[o].val = max(tree[o].val, tree[lb].rval + tree[rb].lval);
} } void build_tree(int l, int r, int o){
if (l == r) {
LL val;
scanf("%lld", &val);
tree[o].val = tree[o].lval = tree[o].rval = val;
tree[o].flag = tree[o].lf = tree[o].rf = true;
return ;
}
tree[o].lf = tree[o].rf = tree[o].flag = true;
int mid = (l + r) / ;
if (l <= mid) build_tree(l, mid, o << );
if (r > mid) build_tree(mid + , r, o << | );
push_up(o);
} void update(int l, int r, int pos, int o){
if (l == r && l == pos){
tree[o].val = ;
tree[o].flag = tree[o].lf = tree[o].rf = false;
return ;
}
int mid = (l + r) / ;
if (pos <= mid) update(l, mid, pos, o << );
if (pos > mid) update(mid + , r, pos, o << | );
push_up(o);
} int main(){
scanf("%d", &n);
build_tree(, n, ); for (int i = ; i <= n; i++){
int pos; scanf("%d", &pos);
update(, n, pos, );
printf("%lld\n", tree[].val);
}
return ;
}

思路二:逆向考虑,使用并查集,因为最初是0,所以我们只需要逆向考虑就好了。

//看看会不会爆int!数组会不会少了一维!
//取物问题一定要小心先手胜利的条件
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define haha printf("haha\n")
const int maxn = 1e5 + ;
LL a[maxn], b[maxn], sum[maxn], ans[maxn];
bool vis[maxn];
int n;
int par[maxn]; int pfind(int x){
if (x == par[x]) return x;
return par[x] = pfind(par[x]);
} void unite(int x, int y){
x = pfind(x), y = pfind(y);
par[x] = y;
sum[y] += sum[x];
} int main(){
cin >> n;
for (int i = ; i <= n; i++) {
par[i] = i;
scanf("%lld", a + i);
sum[i] = a[i];
}
for (int i = ; i <= n; i++)
scanf("%d", b + i);
LL res = ;
for (int i = n; i >= ; i--){
ans[i] = res;
if (vis[b[i] + ]) unite(b[i], b[i] + );
if (vis[b[i] - ]) unite(b[i], b[i] - );
vis[b[i]] = true;
res = max(res, sum[pfind(b[i])]);
}
for (int i = ; i <= n; i++)
cout << ans[i] << endl;
return ;
}

线段树 或者 并查集 Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C的更多相关文章

  1. Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) A B C D 水 模拟 并查集 优先队列

    A. Broken Clock time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  2. Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C. Destroying Array 带权并查集

    C. Destroying Array 题目连接: http://codeforces.com/contest/722/problem/C Description You are given an a ...

  3. Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C 倒序并查集

    C. Destroying Array time limit per test 1 second memory limit per test 256 megabytes input standard ...

  4. Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C. Destroying Array

    C. Destroying Array time limit per test 1 second memory limit per test 256 megabytes input standard ...

  5. Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) B. Verse Pattern 水题

    B. Verse Pattern 题目连接: http://codeforces.com/contest/722/problem/B Description You are given a text ...

  6. Intel Code Challenge Elimination Round (Div.1 + Div.2, combined)

    A. Broken Clock time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  7. Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C. Destroying Array -- 逆向思维

    原题中需要求解的是按照它给定的操作次序,即每次删掉一个数字求删掉后每个区间段的和的最大值是多少. 正面求解需要维护新形成的区间段,以及每段和,需要一些数据结构比如 map 和 set. map< ...

  8. codeforces(Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) )(C,D)

    C. Destroying Array time limit per test 1 second memory limit per test 256 megabytes input standard ...

  9. 贪心+树状数组维护一下 Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) D

    http://codeforces.com/contest/724/problem/D 题目大意:给你一个串,从串中挑选字符,挑选是有条件的,按照这个条件所挑选出来的字符集合sort一定是最后选择当中 ...

随机推荐

  1. oracle索引

    1,建立索引 create index goods_num on goods (num) tablespace data; 2,查看表上索引 select * from USER_INDEXES wh ...

  2. UVa 1600 Patrol Robot (习题 6-5)

    传送门: https://uva.onlinejudge.org/external/16/1600.pdf 多状态广搜 网上题解: 给vis数组再加一维状态,表示当前还剩下的能够穿越的墙的次数,每次碰 ...

  3. Vue - 使用命令行搭建单页面应用

    使用命令行搭建单页面应用 我们来看一下最后完成的效果: 大纲 1. 下载 node, git, npm 2. 使用命令行安装一个项目 一. 下载工具 node , git  的下载大家可以去官网自行下 ...

  4. autocomplete+PHP+MYSQL的实现模糊查询

    1.HTML网页表单部分: <input type="text" name="course" id="course" /> 2. ...

  5. webstorm常用快捷键及(idea,phpstorm,android studio通用)使用技巧

    webstorm常用快捷键 ctrl+l 格式化代码 ctrl+shift+ -/+键   折叠/展开所有代码 打开file:ctrl+shift+n 打开一个类:ctrl+n 代码提示:Ctrl+a ...

  6. 《高性能Javascript》读书笔记-1

    第一章 加载和执行 当浏览器执行JavaScript代码时,不能同时做其他任何事情(单一进程),意味着<script>标签每次出现都霸道地让页面等带脚本的解析和执行(每个文件必须等到前一个 ...

  7. Leetcode 289 Game of Life

    According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellul ...

  8. 安装 sublime package control

    import urllib.request,os; pf = 'Package Control.sublime-package'; ipp = sublime.installed_packages_p ...

  9. linux服务器的操作禁忌

    1.linux系统是否支持开启SELINUX服务 我方linux系统的服务器不支持开启Selinux服务,如果开启了selinux服务,会导致系统异常并无法启动. 2.linux系统下能否开启NetW ...

  10. NSURL访问项目中的文件

    最近在研究视频处理,具体为:将一个mp4文件,拖入项目工程中,通过url访问文件. 开始代码如下: NSString *path = [[NSBundle mainBundle]pathForReso ...