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. SG函数闲扯(转)

    http://ydcydcy1.blog.163.com/blog/static/216089040201342412717440/ 没来得及看.

  2. AX 2012 SSRS print setting-报表打印输出设置

    static void callerreport_printsetting(Args _args) { LedgerJournalController controller = new LedgerJ ...

  3. vs 2013 Express 无法启动程序xxx.exe,系统找不到指定文件

    由于实验室有人用了含病毒的软件,网管把实验室出口给封了,周末人家又不上班.看样子树莓派是玩不成了,所以昨天在宿舍写windows程序,最基本的窗口程序,听说这段代码初学者至少要自己敲5遍以上.代码如下 ...

  4. location.href跳转不正确

    今天写这个随笔的用意是为了记录我遇到的一种情况,导致我页面无法正确跳转 location.href跳转页面其实很简单,只要附上url就可以了,但是今天我在测试一个跳转时是这么写的: location. ...

  5. Ms sql pivot unpivot

    --建表 create table dbo.orders ( orderid int not null primary key nonclustered, orderdate datetime not ...

  6. Http的请求对象

    Servlet 客户端 HTTP 请求 当浏览器请求网页时,它会向 Web 服务器发送特定信息,这些信息不能被直接读取,因为这些信息是作为 HTTP 请求的头的一部分进行传输的.您可以查看 HTTP ...

  7. javascript高级程序设计--笔记01

    概述 JavaScript的实现包含三个部分: 1  核心(ECMAScript)   提供核心语言功能 2  文档对象模型(DOM)  一套提供了访问以及操作网页内容的API 3  浏览器对象模型( ...

  8. SharePoint report site.

    Create site. Upload rdl files into Dashboards lib. Manage Data source. Select the rdl file and click ...

  9. 测试Python代码

    作为程序员,懂得测试,这是必须的职业技能.很遗憾,我以前从未意识到这点,因此经历了很多叫苦不迭的开发生涯.当然了,期望每个人都成为测试高手也是不可能的,但是最基本的单元测试啥的是不惜的,尤其是现在中小 ...

  10. form表单中的常用控件

    <form action="#" method="post"> placeholder:输入表单是提供一个提示 <input type=&qu ...