线段树 或者 并查集 Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C
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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C. Destroying Array -- 逆向思维
原题中需要求解的是按照它给定的操作次序,即每次删掉一个数字求删掉后每个区间段的和的最大值是多少. 正面求解需要维护新形成的区间段,以及每段和,需要一些数据结构比如 map 和 set. map< ...
- 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 ...
- 贪心+树状数组维护一下 Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) D
http://codeforces.com/contest/724/problem/D 题目大意:给你一个串,从串中挑选字符,挑选是有条件的,按照这个条件所挑选出来的字符集合sort一定是最后选择当中 ...
随机推荐
- ZS and The Birthday Paradox
ZS and The Birthday Paradox 题目链接:http://codeforces.com/contest/711/problem/E 数学题(Legendre's formula) ...
- 3、FileInputStream--->类文件输入流(读取文件数据)
Api介绍 定义 FileInputStream 用于读取诸如图像数据之类的原始字节流.要读取字符流,请考虑使用 FileReader 构造方法 FileInputStream(File file) ...
- UVa 1354 Mobile Computing | GOJ 1320 不加修饰的天平问题 (例题 7-7)
传送门1(UVa): https://uva.onlinejudge.org/external/13/1354.pdf 传送门2(GOJ): http://acm.gdufe.edu.cn/Probl ...
- 《Mastering Opencv ...读书笔记系列》车牌识别(I)
http://blog.csdn.net/jinshengtao/article/details/17883075/ <Mastering Opencv ...读书笔记系列>车牌识别(I ...
- win2008阿里一键环境包mysql老是1067报错
目前查看并不是染毒导致mysql无法启动,而是在mysql的配置文件中配置了log这个参数,这个参数语义不明确,请您将“--log”更改为general_log_file并添加一行“general_l ...
- javascript apply()和call()
原文链接 http://www.jb51.net/article/30883.htm 想要理解透彻apply()和call() ,还要需要理解this 作用域 局部变量 全局变量 js apply ...
- Java IO 嵌套流、文本的输入输出和存储
Java IO 嵌套流.文本的输入输出和存储 @author ixenos 1. 组合流过滤器(嵌套流) a) 跨平台文件分割符:常量字符串 java.io.File.seperator 等 ...
- Udemy - Angular 2 - The Complete Guide 笔记
1. install > npm install -g angular-cli 2. create app > ng new first-app 3. build app > cd ...
- 与malloc有关的问题
nefu 1026 申请动态空间存放字符串,将其排序后输出 http://acm.nefu.edu.cn/JudgeOnline/problemShow.php?problem_id=1026 #in ...
- 统计英文文章中各单词的频率,打印频率最高的十个单词(C语言实现)
一.程序思路及相关代码 首先打开文件,代码如下 FILE *fp; char fname[10]; printf("请输入要分析的文件名:\n"); scanf("%s ...