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. MYSQL C API : mysql_real_escape_string 二进制数据存储

    #include <iostream> #include <string> #include <string.h> #include <mysql.h> ...

  2. mysql <-> sqlite

    在做程序时,sqlite数据很方便.用mysql数据导出到sqlite的步骤:(用csv文件过渡) -------------------------------  先导出到csv文件  ------ ...

  3. (LinkedList)Intersection of Two Linked Lists

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  4. XE6移动开发环境搭建之IOS篇(1):准备安装材料(有图有真相)

    网上能找到的关于Delphi XE系列的移动开发环境的相关文章甚少,本文尽量以详细的图文内容.傻瓜式的表达来告诉你想要的答案. 原创作品,请尊重作者劳动成果,转载请注明出处!!! 1.选择方案 --- ...

  5. 实习日记:图像检索算法 LSH 的总结与分析

    先贴上这两天刚出炉的C++代码.(利用 STL 偷了不少功夫,代码待优化) Head.h #ifndef HEAD_H #define HEAD_H #include "D:\\LiYang ...

  6. android Drawable的问题

    1.资源解析成Drawable getDrawable(int id); 挺简单一方法,可是 require api 21......如何向下兼容呢???? 幸亏有ContextCompat类...( ...

  7. Search history in "Maps"

    A friend of mine came to me with her iPhone yesterday. She wanted to know how to clear search histor ...

  8. socket笔记

    参考: http://www.cnblogs.com/dolphinX/p/3460545.html http://www.cnblogs.com/wei2yi/archive/2011/03/23/ ...

  9. 关于CacheLookup一个有趣的问题

    今天写一个与其他系统进行物料同步的接口,通过COM Business Connector调用Axapta3.0的方法将数据插入到物料表中,中间发生异常,事务回滚,再次调用的时候提示刚刚发生异常的物料已 ...

  10. Linux_05------Linux之vim编辑器

    行 * -/xxx 向后搜索 * -?xxx 向前搜索 * 命令模式下: * -h: 光标左移 * -j: 光标下移 * -k: 光标上移 * -l: 光标右移 * -ctrl+f: 向下翻页(fro ...