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. linux中找不到/etc/sysconfig/iptables

    解决办法初始化iptables. #iptables -F #service iptables save #service iptables restart

  2. [BZOJ2339][HNOI2011]卡农

    [BZOJ2339][HNOI2011]卡农 试题描述 输入 见"试题描述" 输出 见"试题描述" 输入示例 见"试题描述" 输出示例 见& ...

  3. 2-jQuery - AJAX load() 方法【进阶篇】

    第一篇的AJAX load() 方法过于简单,补充一下,完整的. 格式 $(selector).load(URL,data,callback); 源码 <!DOCTYPE html> &l ...

  4. agnentX学习存在疑问?

    在RFC2741中这样定义: 4.3中有如下段落: A general architectural division of labor between master agent and  subage ...

  5. webapi中的路由前缀

    Route Prefixes Often, the routes in a controller all start with the same prefix. For example: public ...

  6. NSBundle、UIImageView和UIButton对比、Xcode文档安装路径、Xcode模拟器安装路径

    1.NSBundle1> 一个NSBundle代表一个文件夹,利用NSBundle能访问对应的文件夹2> 利用mainBundle就可以访问软件资源包中的任何资源3> 模拟器应用程序 ...

  7. use 2 stacks to simulate a queue

    class Stack{ private: ; ]; public: void push(int n); int pop(); int peek(); int size(); }; void Stac ...

  8. Jenkins - 持续集成环境搭建【转】

    1. Jenkins 概述 Jenkins是一个开源的持续集成工具.持续集成主要功能是进行自动化的构建.自动化构建包括自动编译.发布和测试,从而尽快地发现集成错误,让团队能够更快的开发内聚的软件. 2 ...

  9. strace -o /tmp/dhc$$ dhclient -d eth2

    http://askubuntu.com/questions/5187/why-is-dhclient-saying-siocsifaddr-permission-denied ip link add ...

  10. @ResponseBody返回json时,json数据丢失或者报错

    现象: 1.报错:There is a cycle in the hierarchy! 2.返回至前台的json不完整,字段丢失. 错误原因: eg:entity1的属性有list<entiti ...