线段树 或者 并查集 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一定是最后选择当中 ...
随机推荐
- VPS服务器利用WINSCP软件进行SFTP管理服务器文件
虽然我使用VPS时间也不是很久,但是我善于进行统计和分析,从我在的一些VPS交流QQ群中,可以看到基本上使用搬瓦工VPS的站长群中新手较多,甚至很多人都搞不明白VPS与V-P-N的区别都直接选择VPS ...
- Activity设置singleTask无法通过Intent获取值的问题
AActivity跳转BActivity ,AActivity设置lauchmode = "SingleTask"的话,在getIntent无法获取BActivity里面的内容,无 ...
- YII学习第二十三天,accessRules用法
访问控制过滤器(Access Control Filter)访问控制过滤器是检查当前用户是否能执行访问的controller action的初步授权模式. 这种授权模式基于用户名,客户IP地址和访问类 ...
- mysql创建用户、授权[转]
一, 创建用户: 命令:CREATE USER 'username'@'host' IDENTIFIED BY 'password'; 说明:username - 你将创建的用户名, host - 指 ...
- 四大类NoSQL数据库
http://blog.sina.com.cn/s/blog_636415010101945l.html 原文:http://blog.monitis.com/index.php/2011/05/22 ...
- From windows live writer
天线数据长度: 4*14*9664*4 = 2164736 信道估计长度: 614400 均衡: 12*1200*4 = 57600
- ev=ev || window.event 与 ev = window.event || ev 区别
event是事件对象(也是window的属性),但不是标准的,只有IE支持.在W3C标准支持的浏览器下事件对象是引发事件函数的第一个参数,参数名随意.var oEvent = ev || event; ...
- MC- 交易并设置止损
using System; using System.Drawing; using System.Linq; using PowerLanguage.Function; using ATCenterP ...
- erlang四种监控策略
转自:http://jasionq.blog.163.com/blog/static/10970577920133883158424/ Supervisor Behaviour是一个用来实现一个sup ...
- 4.编写Java应用程序。首先,定义一个时钟类——Clock,它包括三个int型 成员变量分别表示时、分、秒,一个构造方法用于对三个成员变量(时、分、秒) 进行初始化,还有一个成员方法show()用于显示时钟对象的时间。其次,再定义 一个主类——TestClass,在主类的main方法中创建多个时钟类的对象,使用这 些对象调用方法show()来显示时钟的时间。
Clock package com.hanqi.test; public class Clock { int hour,minute,second; Clock(int h,int m,int s) ...