【问题述】

给出一个随机的排列,请你计算最大值减最小值的差小于等于0~n-1的区间分别有多少个。

输入格式

输入文件名为sum.in。

第一行一个数T(<=10),表示数据组数

对于每一组数据:

第一行一个数n(1<=n<=100,000)

第二行n个数a1...an,表示一个随机的排列

【输出】

输出文件名为sum.out。

对于每组数据输出n行,分别表示差值小于等于0~n-1的区间个数

【输入输出样例】

sum.in

sum.out

1

4

3 2 4 1

4

5

7

10

【数据说明】

对于30%的数据,1<=n<=300;

对于60%的数据,1<=n<=5000

对于100%的数据,1<=n<=100000

分析:考虑暴力,O(n^2)枚举区间,O(n)求最值,可以过30分.

如何把n^3优化到n^2?我们可以在找最值的时候优化一下,我们并不一定非要在固定了区间后再去找最值然后更新当前区间,而是我们先固定左端点,然后从左往右扩展去找最值,用最值更新区间信息.

比如,我们已经求得当前区间的最小值minx和最大值maxx,向右扩展一位得到新的minx和maxx,那么ans[maxx - minx]++.当然,我们也可以用ST表O(1)维护最值,复杂度也是O(n^2)的.

题目数据范围很明显提示复杂度要O(nlogn),考虑怎么优化60分的算法.我们向右跳并不需要每次只跳一位,因为可能有很多位置不会更新minx和maxx,我们需要找到下一位能更新minx和maxx的位置,利用单调栈来记录.维护一个单调上升的单调栈和一个单调下降的.因为这是一个随机的排列,所以差不多会跳logn次.

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; int T,n,a[],nextmin[],nextmax[],head,pos[],p[],minx,maxx;
long long ans[]; int main()
{
scanf("%d", &T);
while (T--)
{
memset(ans, , sizeof(ans));
memset(a, , sizeof(a));
scanf("%d", &n);
for (int i = ; i <= n; i++)
scanf("%d", &a[i]);
for (int i = ; i <= n; i++)
nextmin[i] = nextmax[i] = n + ;
head = ;
memset(p, , sizeof(p));
for (int i = ; i <= n; i++)
{
while (head > && a[i] < p[head])
{
nextmin[a[pos[head]]] = i;
head--;
}
p[++head] = a[i];
pos[head] = i;
}
memset(pos, , sizeof(pos));
head = ;
memset(p, 0x3f3f3f, sizeof(p));
for (int i = ; i <= n; i++)
{
while (head > && a[i] > p[head])
{
nextmax[a[pos[head]]] = i;
head--;
}
p[++head] = a[i];
pos[head] = i;
}
for (int i = ; i <= n; i++)
{
minx = maxx = a[i];
int j = i;
while (j <= n)
{
if (nextmax[maxx] < nextmin[minx])
{
ans[maxx - minx] += nextmax[maxx] - j;
j = nextmax[maxx];
maxx = a[nextmax[maxx]];
}
else
{
ans[maxx - minx] += nextmin[minx] - j;
j = nextmin[minx];
minx = a[nextmin[minx]];
}
}
}
for (int i = ; i < n; i++)
ans[i] += ans[i - ];
for (int i = ; i < n; i++)
printf("%lld\n", ans[i]);
} return ;
}

noip模拟赛 排列的更多相关文章

  1. CH Round #54 - Streaming #5 (NOIP模拟赛Day1)

    A.珠 题目:http://ch.ezoj.tk/contest/CH%20Round%20%2354%20-%20Streaming%20%235%20(NOIP模拟赛Day1)/珠 题解:sb题, ...

  2. 2016-06-19 NOIP模拟赛

          2016-06-19 NOIP模拟赛 by coolyangzc 共3道题目,时间3小时 题目名 高级打字机 不等数列 经营与开发 源文件 type.cpp/c/pas num.cpp/c ...

  3. NOIP模拟赛20161022

    NOIP模拟赛2016-10-22 题目名 东风谷早苗 西行寺幽幽子 琪露诺 上白泽慧音 源文件 robot.cpp/c/pas spring.cpp/c/pas iceroad.cpp/c/pas ...

  4. contesthunter暑假NOIP模拟赛第一场题解

    contesthunter暑假NOIP模拟赛#1题解: 第一题:杯具大派送 水题.枚举A,B的公约数即可. #include <algorithm> #include <cmath& ...

  5. NOIP模拟赛 by hzwer

    2015年10月04日NOIP模拟赛 by hzwer    (这是小奇=> 小奇挖矿2(mining) [题目背景] 小奇飞船的钻头开启了无限耐久+精准采集模式!这次它要将原矿运到泛光之源的矿 ...

  6. 大家AK杯 灰天飞雁NOIP模拟赛题解/数据/标程

    数据 http://files.cnblogs.com/htfy/data.zip 简要题解 桌球碰撞 纯模拟,注意一开始就在袋口和v=0的情况.v和坐标可以是小数.为保险起见最好用extended/ ...

  7. 队爷的讲学计划 CH Round #59 - OrzCC杯NOIP模拟赛day1

    题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的讲学计划 题解:刚开始理解题意理解了好半天,然后发 ...

  8. 队爷的Au Plan CH Round #59 - OrzCC杯NOIP模拟赛day1

    题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的Au%20Plan 题解:看了题之后觉得肯定是DP ...

  9. 队爷的新书 CH Round #59 - OrzCC杯NOIP模拟赛day1

    题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的新书 题解:看到这题就想到了 poetize 的封 ...

随机推荐

  1. bzoj 1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会【tarjan】

    几乎是板子,求有几个size>1的scc 直接tarjan即可 #include<iostream> #include<cstdio> #include<cstri ...

  2. [AHOI2006] 文本编辑器editor

    Description 这些日子,可可不和卡卡一起玩了,原来可可正废寝忘食的想做一个简单而高效的文本编辑器.你能帮助他吗?为了明确任务目标,可可对"文本编辑器"做了一个抽象的定义: ...

  3. 【NOIP模拟赛】一道挖掉背景的数学题

    Title:[Empty] Time Limit:1000 ms Memory Limit:131072 KBytes Description 给定n与p,求\(\left\lfloor x^n\ri ...

  4. Android 性能优化(20)多核cpu入门:SMP Primer for Android

    SMP Primer for Android 1.In this document Theory Memory consistency models Processor consistency CPU ...

  5. Spring-security配置代码

    @Configuration public static class WebSecurityConfigurer extends WebSecurityConfigurerAdapter{ @Over ...

  6. Laravel5.1学习笔记23 Eloquent 序列化

    Eloquent: Serialization Introduction Basic Usage Hiding Attributes From JSON Appending Values To JSO ...

  7. Integer / BigInteger / BigDecimal 方法

    import java.math.BigDecimal; import java.math.*; public class Main{ public static void main(String[] ...

  8. Python 将中文转拼音

    文字转拼音 import os.path class PinYin(object): def __init__(self): self.word_dict = {} def load_word(sel ...

  9. C#.NET,技巧篇(DataGridView线程操作)

    这个系列的文章,主要是平时做C#.NET(Framework 3.5)开发的时候,积累的经验和技巧.我们平时总有这样的体会,遇到一个特别难解决的问题,网上寻它千百度也没能搜索到有用的信息.这时你肯定会 ...

  10. Farseer.net轻量级开源框架 入门篇:修改数据详解

    导航 目   录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 入门篇: 添加数据详解 下一篇:Farseer.net轻量级开源框架 入门篇: 删除数据详解 ...