http://acm.fzu.edu.cn/problem.php?pid=2136

题目大意: 给你n个袋子每个袋子里都装有糖果,然后呢你可以每次抽取一个连续的一个区间的袋子,然后带走里面最多糖果数目的袋子。

求区间长度从1到n你能带走的糖果数目最坏的情况是多少,也就是求所有区间的最大值的最小值

分析: 如果从小的开始查找他每次只要找到他能覆盖的区间,那么他就是这个区间的最大值,所以我们每次查询这个点就行了

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<ctype.h>
#include <iostream>
#include <map>
using namespace std;
#define INF 1e9+7
#define Lson r<<1|1
#define Rson r<<1
#define N 101000 int ans[N];
struct node
{
int l,r,la,lb,Max;///la是这个区间左边连续的个数,lb是右边,Max是区间最大的连续序列
int mid()
{
return (l+r)/;
}
int len()
{
return (r-l+);
}
}a[N*];
struct Node
{
int x,y;
}P[N]; int cmp(Node c,Node d)
{
return c.x<d.x;
} void BuildTree(int r,int L,int R)
{
a[r].l = L;
a[r].r = R;
a[r].la = a[r].lb = a[r].Max = ; if(L == R)
return; BuildTree(Lson, L, a[r].mid());
BuildTree(Rson, a[r].mid()+, R);
}
void Qurry(int r,int x,int y)
{
if(a[r].l == a[r].r)
{
a[r].la = a[r].lb = a[r].Max = ;
return;
} if(y > a[r].mid())
Qurry(Rson, x, y);
else
Qurry(Lson, x, y); a[r].Max = max(a[Lson].Max, max(a[Rson].Max, a[Lson].lb+a[Rson].la));
a[r].la = a[Lson].la;
a[r].lb = a[Rson].lb; if(a[Lson].la == a[Lson].len())
a[r].la += a[Rson].la;
if(a[Rson].lb == a[Rson].len())
a[r].lb += a[Lson].lb;
} int main()
{
int T;
scanf("%d", &T);
while(T --)
{
int n;
scanf("%d", &n);
memset(P, , sizeof(P));
for(int i=; i<n; i++)
{
scanf("%d", &P[i].x);
P[i].y = i;
} BuildTree(, , n-);
sort(P, P+n, cmp); int m=;
memset(ans, , sizeof(ans));
for(int i=; i<n; i++)
{
Qurry(, P[i].x, P[i].y);///查询y点的最大序列
int tmp=a[].Max; while(m <= tmp)
ans[m ++]=P[i].x;
} for(int i=; i<=n; i++)
printf("%d\n", ans[i]);
}
return ;
}

Problem 2136 取糖果---FUOJ (线段树+维护)的更多相关文章

  1. FZU Problem 2136 取糖果

     Problem 2136 取糖果 Time Limit: 1000 mSec    Memory Limit : 32768 KB  Problem Description 有N个袋子放成一排,每个 ...

  2. fzu 2136 取糖果 好几种方法解决。

    Problem 2136 取糖果 Accept: 39    Submit: 101 Time Limit: 1000 mSec    Memory Limit : 32768 KB Problem ...

  3. hdu 4037 Development Value(线段树维护数学公式)

    Development Value Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others ...

  4. SPOJ 1557 GSS2 - Can you answer these queries II (线段树+维护历史最值)

    都说这题是 GSS 系列中最难的,今天做了一下,名副其实 首先你可以想到各种各样的在线乱搞想法,线段树,主席树,平衡树,等等,但发现都不太可行. 注意到题目也没有说强制在线,因此可以想到离线地去解决这 ...

  5. HDU3564 --- Another LIS (线段树维护最值问题)

    Another LIS Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  6. Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)

    题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...

  7. Codeforces 834D The Bakery【dp+线段树维护+lazy】

    D. The Bakery time limit per test:2.5 seconds memory limit per test:256 megabytes input:standard inp ...

  8. [动态dp]线段树维护转移矩阵

    背景:czy上课讲了新知识,从未见到过,总结一下. 所谓动态dp,是在动态规划的基础上,需要维护一些修改操作的算法. 这类题目分为如下三个步骤:(都是对于常系数齐次递推问题) 1先不考虑修改,不考虑区 ...

  9. cf213E 线段树维护hash

    链接 https://codeforces.com/contest/213/problem/E 题目大意 给出两个排列a.b,长度分别为n.m,你需要计算有多少个x,使 得\(a_1 + x; a_2 ...

随机推荐

  1. shell script练习

    执行脚本的几种方式: 1. sh a.sh 或者  bash a.sh  调用的是 /bin/bash 进程执行的,所以脚本不需要执行权限. 2. 直接使用绝对路径执行, /home/script/a ...

  2. (转载)SQL Server 2005 如何启用xp_cmdshell组件

    原文地址:http://www.cnblogs.com/atree/p/SQL_SERVER_xp_cmdshell.html [错误描述]: SQL Server阻止了对组件‘xp_cmdshell ...

  3. Vi中的^M问题

    一般情况下,windows下编辑过的文件放到Linux下行尾会多出一个^M符号 1.可以通过dos2unix 命令作用与文件消除 2.或者在VI内通过 只需要在vi/vim 中输入命令:%s/\r// ...

  4. 记一个eclipse 错误 Undefined variable from import: randrange

    我自己定义了一个random.py   和pev的包含路径一样的 解决办法是: 出现这种问题,可以import 这个模块,print 这个模块

  5. 苹果手机制作gif图片

    前一段介绍了一款很好用的在模拟器上录制gif图片的工具licecap(地址:http://www.cnblogs.com/10-19-92/p/5593785.html), 但是licecap不能使用 ...

  6. 三层VS控制器

    三层中的Model 是数据模型 与数据库字段一一对应而mvc中的model是 显示模型,指在页面的显示字段的列一一对应,不一定在数据库中有这样的一个表,如当页 面上要显示多个表的综合信息时. 当在Co ...

  7. dedecms代码研究二

    dedecms代码研究(2)从index开始现在继续,今天讲的主要是dedecms的入口代码.先打开index.PHP看看里面是什么吧.打开根目录下的index.php嗯映入眼帘的是一个if语句.检查 ...

  8. ZC706以太网扩展板接口

    端口 BANK-VADJ PIN NAME PIN_NAME RGMII0 rgmii_0_txc 10 AB14 LA15_N PHY_0_GTXCLK rgmii_0_rxc 10 AE13 LA ...

  9. Libevent库 编译与使用

    Libevent官网:http://libevent.org/ windows 7下编译: 编译环境: windows 7 + VS2010 (1)解压libevent到F:\libevent\lib ...

  10. windows7下安装python3的scrapy框架

    强大的Anaconda和Spyder.不过如何在这个平台上安装Scrapy呢. 打开MS-DOS(win+R输入cmd回车) 然后输入: conda install -c scrapinghub sc ...