set+线段树 Codeforces Round #305 (Div. 2) D. Mike and Feet
/*
题意:对于长度为x的子序列,每个序列存放为最小值,输出长度为x的子序列的最大值
set+线段树:线段树每个结点存放长度为rt的最大值,更新:先升序排序,逐个添加到set中
查找左右相邻的位置,更新长度为r - l - 1的最大值,感觉线段树结构体封装不错!
详细解释:http://blog.csdn.net/u010660276/article/details/46045777
其实还有其他解法,先掌握这种:)
*/
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <iostream>
#include <cstring>
#include <set>
using namespace std; typedef long long ll;
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1 const int MAXN = 2e5 + ;
const int INF = 0x3f3f3f3f; struct A
{
int v, id;
bool operator < (const A &a) const
{
return v < a.v;
}
}a[MAXN];
struct SegmentTree
{
int add[MAXN << ];
int mx[MAXN << ]; void push_down(int rt)
{
if (add[rt])
{
add[rt<<] = add[rt<<|] = add[rt];
mx[rt<<] = mx[rt<<|] = add[rt];
add[rt] = ;
}
} void build(int l, int r, int rt)
{
add[rt] = mx[rt] = ;
if (l == r) return ;
int mid = (l + r) >> ;
build (lson); build (rson);
} void updata(int ql, int qr, int c, int l, int r, int rt)
{
if (ql <= l && r <= qr) {mx[rt] = c; add[rt] = c; return ;}
push_down (rt);
int mid = (l + r) >> ;
if (ql <= mid) updata (ql, qr, c, lson);
if (qr > mid) updata (ql, qr, c, rson);
} int query(int x, int l, int r, int rt)
{
if (l == r) return mx[rt];
push_down (rt);
int mid = (l + r) >> ;
if (x <= mid) return query (x, lson);
return query (x, rson);
}
}tree; int ans[MAXN]; int main(void) //Codeforces Round #305 (Div. 2) D. Mike and Feet
{
int n;
while (scanf ("%d", &n) == )
{
for (int i=; i<=n; ++i)
{
scanf ("%d", &a[i].v); a[i].id = i;
}
sort (a+, a++n); tree.build (, n, );
set<int> S; S.insert (); S.insert (n + );
set<int>::iterator it;
for (int i=; i<=n; ++i)
{
int l, r;
it = S.lower_bound (a[i].id);
r = *it; it--; l = *it;
if (r - l - >= ) tree.updata (, r-l-, a[i].v, , n, );
S.insert (a[i].id);
} for (int i=; i<=n; ++i)
ans[i] = tree.query (i, , n, );
for (int i=; i<=n; ++i)
printf ("%d%c", ans[i], (i==n) ? '\n' : ' ');
} return ;
} /*
10
1 2 3 4 5 4 3 2 1 6
*/
/*
jinye的解法:原理一样,找到最小值是a[i]的最长区间,用l[],r[]记录左右端点的位置
比线段树快多了:)
*/
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std; const int MAXN = 2e5 + ;
const int INF = 0x3f3f3f3f;
int a[MAXN], l[MAXN], r[MAXN], ans[MAXN]; int main(void) //Codeforces Round #305 (Div. 2) D. Mike and Feet
{
int n;
while (scanf ("%d", &n) == )
{
memset (ans, , sizeof (ans));
for (int i=; i<=n; ++i)
{
scanf ("%d", &a[i]);
l[i] = r[i] = i;
} for (int i=; i<=n; ++i)
{
while (l[i] > && a[i] <= a[l[i]-]) l[i] = l[l[i]-];
}
for (int i=n; i>=; --i) //倒过来回超时
{
while (r[i] < n && a[i] <= a[r[i]+]) r[i] = r[r[i]+];
} for (int i=; i<=n; ++i)
{
int x = r[i] - l[i] + ;
ans[x] = max (ans[x], a[i]);
} for (int i=n-; i>=; --i) //可能范围扩展的太厉害了
{
ans[i] = max (ans[i], ans[i+]);
} for (int i=; i<=n; ++i)
printf ("%d%c", ans[i], (i==n) ? '\n' : ' ');
} return ;
}
数组效率更高
set+线段树 Codeforces Round #305 (Div. 2) D. Mike and Feet的更多相关文章
- Codeforces Round #305 (Div. 1) B. Mike and Feet 单调栈
B. Mike and Feet Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/547/pro ...
- Codeforces Round #305 (Div. 2)D. Mike and Feet(单调栈)
题意 n个值代表n个熊的高度 对于size为x的group strength值为这个group(连续的几个熊)中熊的最小的height值 对于x(1<=x<=n) 求出最大的strengt ...
- Codeforces Round #305 (Div. 2) D. Mike and Feet 单调栈
D. Mike and Feet time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- Codeforces Round #305 (Div. 2) D. Mike and Feet
D. Mike and Feet time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- Codeforces Round #305 (Div. 1) B. Mike and Feet
Mike is the president of country What-The-Fatherland. There are n bears living in this country besid ...
- 数论/暴力 Codeforces Round #305 (Div. 2) C. Mike and Frog
题目传送门 /* 数论/暴力:找出第一次到a1,a2的次数,再找到完整周期p1,p2,然后以2*m为范围 t1,t2为各自起点开始“赛跑”,谁落后谁加一个周期,等到t1 == t2结束 详细解释:ht ...
- 暴力 Codeforces Round #305 (Div. 2) B. Mike and Fun
题目传送门 /* 暴力:每次更新该行的num[],然后暴力找出最优解就可以了:) */ #include <cstdio> #include <cstring> #includ ...
- 字符串处理 Codeforces Round #305 (Div. 2) A. Mike and Fax
题目传送门 /* 字符串处理:回文串是串联的,一个一个判断 */ #include <cstdio> #include <cstring> #include <iostr ...
- 线段树 Codeforces Round #197 (Div. 2) D. Xenia and Bit Operations
题目传送门 /* 线段树的单点更新:有一个交叉更新,若rank=1,or:rank=0,xor 详细解释:http://www.xuebuyuan.com/1154895.html */ #inclu ...
随机推荐
- Hibernate中的merge使用详情解说
merge的作用是:新new一个对象,如果该对象设置了ID,则这个对象就当作游离态处理: 当ID在数据库中不能找到时,用up ...
- 使用C语言获取字符串或文件的MD5值
libmd5地址:https://sourceforge.net/projects/libmd5-rfc/ MD5Demo1.c #include <stdio.h> #include & ...
- bzoj4105: [Thu Summer Camp 2015]平方运算
填坑 我不知道怎么算的,但是所有环的LCM数不会超过60 然后用线段树维护这个东西,每个节点记录子树内的循环节 没到循环节的暴力枚举 复杂度是nlogn再乘以循环节长度 #include<cst ...
- bzoj3134: [Baltic2013]numbers
稍微用脑子想一想,要是一个回文数,要么s[i]==s[i+1]要么s[i]==s[i+2]就可以实锤了 所以多开两维表示最近两位选的是什么数就完了 注意前导0 #include<cstdio&g ...
- 一步一步学Silverlight 2系列(7):全屏模式支持
一步一步学Silverlight 2系列(7):全屏模式支持 概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言V ...
- Oracle:sequence问题研究
一直以来,以为sequence是不间断地持续增长的:但今天发现sequence是会跳号,这种情况发生在RAC环境下.在单实例环境下,应该不存在的. sequence截图如下: 数据库表中发生了跳号: ...
- Redis和StackExchange.Redis
redis有多个数据库1.redis 中的每一个数据库,都由一个 redisDb 的结构存储.其中,redisDb.id 存储着 redis 数据库以整数表示的号码.redisDb.dict 存储着该 ...
- java -- 虚拟机和内存
从大方向来分:栈内存,堆内存,方法区,本地方法栈,程序计数器 java从存储数据的角度来分: 寄存器(register):最快的存储区,由编译器根据需求进行分配,不由认为控制. 堆栈(statck): ...
- 洛谷P2473奖励关——状压DP
题目:https://www.luogu.org/problemnew/show/P2473 还是对DP套路不熟悉... 像这种前面影响后面,而后面不影响前面的问题就应该考虑倒序递推: 看n只有15那 ...
- CodeForces 1103C. Johnny Solving
题目简述:给定简单(无自环.无重边)连通无向图$G = (V, E), 1 \leq n = |V| \leq 2.5 \times 10^5, 1 \leq m = |E| \leq 5 \time ...