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. RMIC命令提示找不到类的问题

    问题环境: RMI服务类已经写好. 目录结构: -- A ----- B -------- C ------------- RMIImpl.class RMIImpl.java : package B ...

  2. DHCP底层参考

    [原创翻译,水平有限] ISC DHCP支持802.1的以太网帧,令牌环和FDDI等网络.为了桥接物理层和DHCP层,它还必须实现IP和UDP协议帧. 这源于UNIX BSD socket 对未配置接 ...

  3. 手机浏览器无法获取COOKIE的原因

    手机浏览器上无法使用cookie,肯能是 1. 浏览器禁用 COOKIE ,这个简单开启即可. 2. 可能是手机所在时区有问题,将COOKIE有效期设置更长时间测试下,在更改时区

  4. IOS开发小技巧,知识点

    1.IOS模拟器第一次打开需要进入“设置”中关掉"Auto-Capitalization"选项. 2.NSInteger转化 NSString类型: [NSString strin ...

  5. magento数据查询

    一.查询出所有的数据: 1.以mysql查询语句在magento里执行,以此来查询你所需要的语句! $read = Mage::getSingleton('core/resource')->ge ...

  6. PerformSelector 和 NSInvocation

  7. Java的I/O总结

    概念:Java语言中数据流是接收和发送数据的管道.流是一位单项的. 流的分类: 按照字节字符:InputStream和OutputStream是字节输入输出流的抽象父类.Reader和Writer是字 ...

  8. digitalocean注册验证账户、激活账号教程

    注册digitalocean账号很简单,使用优惠链接注册digitalocean还能赠送10美元余额,digitalocean vps是优秀的SSD VPS,最便宜的套餐只要5美元/月. 由于中国大陆 ...

  9. Vultr VPS测试IP $5/月KVM-512MB/15G SSD/1T

    vultr主要卖点是:SSD硬盘.超低价格.全球13个机房.10Gb服务器带宽.3GHz CPU.性能优异.vultr vps详细评测可参考这篇文章. 以下是vutlr vps官方提供的测试IP地址: ...

  10. 利用python httplib模块 发送Post请求测试web服务是否正常起来!

    最近在学习python,恰好老大最近让我搞个基于post请求测试web服务是否正常启用的小监控,上网查了下资料,发现强大的Python恰好能够用上,所以自己现学现卖,顺便锻炼下自己. 由于本人也刚接触 ...