题意:

给出n代表序列的长度,接下来给出序列A。找出abcd满足abcd互不相等1<=a<b<c<d<=n的同时A[a]<A[b],A[c]>A[d],问这样的abcd有几个.

思路:先忽略四个数两两不相等的条件,那就是(,逆序对个数)乘上(顺序对个数)。例如{2,4,1,3},逆序对就是{(2,1),(4,1),(4,3)} ,顺序对就是{(2,4),(2,3),(1,3)},这样3*3=9,一共九个符合a<b && c>d的四元组。但其中有很多不合法的,对于t这个数,不合法情况的个数就是 (关于t的逆序对个数×关于t的顺序对个数),一一减去就是结果了。(不合法的规律多写几组便能找到)

输入:

4

2 4 1 3

4

1 2 3 4

输出:

1
0

代码:

#include <iostream>

#include <cstdio>

#include <cstdlib>

#include <cmath>

#include <vector>

#include <queue>

#include <cstring>

#include <string>

#include <algorithm>

using namespace std;

typedef  long long  ll;

typedef unsigned long long ull;

#define MM(a,b) memset(a,b,sizeof(a));

#define inf 0x7f7f7f7f

#define FOR(i,n) for(int i=1;i<=n;i++)

#define CT continue;

#define PF printf

#define SC scanf

const int mod=1000000007;

const int N=1e6+10;

int tmp[N],a[N],n,m,c[N],pos[N];

ll lmin[N],rmin[N],lmax[N],rmax[N];

int lowbit(int i)

{

return i&(-i);

}

void add(int x)

{

while(x<=m)

{

c[x]+=1;

x+=lowbit(x);

}

}

int query(int x)

{

int res=0;

while(x>0)

{

res+=c[x];

x-=lowbit(x);

}

return res;

}

int main()

{

while(~scanf("%d",&n))

{

for(int i=1;i<=n;i++)

{

scanf("%d",&tmp[i]);

a[i]=tmp[i];

}

sort(tmp+1,tmp+n+1);//将tmp按从小到大排序,用于树状数组查找顺/逆序对

m=unique(tmp+1,tmp+n+1)-tmp-1;//去重

for(int i=1;i<=n;i++)

pos[i]=lower_bound(tmp+1,tmp+m+1,a[i])-tmp;//找到原来的第i个数在排序后应在的位置

MM(c,0);

for(int i=1;i<=n;i++)//查询0-i-1之间比a[i]小的数放在lmin[i]里,在i-m之间比a[i]大的放在lmax里

{

lmin[i]=query(pos[i]-1);//

lmax[i]=query(m)-query(pos[i]);

add(pos[i]);

}

MM(c,0);

for(int i=n;i>=1;i--)//逆着再来一次

{

rmin[i]=query(pos[i]-1);

rmax[i]=query(m)-query(pos[i]);

add(pos[i]);

}

ll ans=0,l=0,r=0;

for(int i=1;i<=n;i++)  {r+=rmax[i],l+=lmax[i];};

ans=l*r;

for(int i=1;i<=n;i++)//去除不合法的

{

ans-=lmin[i]*rmin[i];

ans-=lmax[i]*rmax[i];

ans-=lmax[i]*lmin[i];

ans-=rmax[i]*rmin[i];

}

printf("%lld\n",ans);

}

return 0;

}

HDU 5792 World is Exploding的更多相关文章

  1. HDU 5792 World is Exploding 树状数组+枚举

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5792 World is Exploding Time Limit: 2000/1000 MS (Ja ...

  2. HDU 5792 World is Exploding (树状数组)

    World is Exploding 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5792 Description Given a sequence ...

  3. hdu 5792 World is Exploding 树状数组

    World is Exploding 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5792 Description Given a sequence ...

  4. HDU 5792 World is Exploding(树状数组+离散化)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=5792 题意: 思路: lmin[i]:表示左边比第i个数小的个数. lmax[i]:表示左边比第i个 ...

  5. hdu 5792 World is Exploding 树状数组+离散化+容斥

    World is Exploding Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  6. HDU 5792 World is Exploding (离散化+树状数组)

    题意:给定 n 个数,让你数出 a < b && c < d && a != b != c != d  && Aa < Ab & ...

  7. HDU 5792:World is Exploding(树状数组求逆序对)

    http://acm.hdu.edu.cn/showproblem.php?pid=5792 World is Exploding Problem Description   Given a sequ ...

  8. hdu 5792(树状数组,容斥) World is Exploding

    hdu 5792 要找的无非就是一个上升的仅有两个的序列和一个下降的仅有两个的序列,按照容斥的思想,肯定就是所有的上升的乘以所有的下降的,然后再减去重复的情况. 先用树状数组求出lx[i](在第 i ...

  9. HDU 5792 L - World is Exploding 。容斥原理 + 树状数组 + 离散化

    题目,要求找出有多少对这样的东西,四个数,并且满足num[a]<num[b] &&num[c]>num[d] 要做这题,首先要懂得用树状数组,我设,下面的小于和大于都是严格 ...

随机推荐

  1. Mock测试框架

    一.前言 使用Mock框架进行单元测试,能够使用当前系统已经开发的接口方法模拟数据.(未写完,慢慢完善) 二.例子 1.引用Moq

  2. JavaScript禁止用户多次提交方法

    [当服务器超载时,会出现提交卡顿的现象,但是用户在操作时,会不停重复点击提交,会造成服务器压力更大.所以我们需要进行限制] [1]将提交按钮禁止 <html> <head> & ...

  3. spring cuowu

    spring常见错误总结 在学习spring过程中遇见了种种不同的异常错误,这里做了一下总结,希望遇见类似错误的同学们共勉一下. 1. 错误一 Error creating bean with nam ...

  4. Cortex-M3寄存器等基础知识

    1.寄存器 CM3拥有R0~R15通用寄存器和一些特殊功能寄存器 R0~R12这些通用寄存器,复位初始值都是不可预料的 2.CM3有R0到R15的通用寄存器组 注:绝大部分的16位thumb只能访问R ...

  5. poj 1149

    #include <cstdio> #include <cstring> #include <queue> #define _clr(x, y) memset(x, ...

  6. [Red5]Red5之Flash流媒体服务器的安装与使用教程完整版(组图)

    参看下面链接:http://www.cuplayer.com/player/PlayerCode/Red5/2013/0319/760.html

  7. Windows7上FTP服务器建立

    1. FTP服务器建立 注意:千万不能使用FTP和ftp建立用户,否则无法登陆ftp服务器. 1.1本地机器上创建一个用户 这个用户是用来登录到FTP的.我的电脑右键->管理->本地用户和 ...

  8. hibernate update部分更新

    hibernate update Hibernate 中如果直接使用 Session.update(Object o); 会把这个表中的所有字段更新一遍. 比如: view plaincopy to ...

  9. shell脚本分类

    shell脚本分为三类:登录脚本.交互式脚本.非交互式脚本 一. 登录脚本类似于windows下的计算机设置中的登录脚本和账户设置下的登录脚本的合集(我是这么理解的哈). 其配置文件的关键词为pref ...

  10. re模块

    Python 的 re 模块(Regular Expression 正则表达式)提供各种正则表达式的匹配操作,和 Perl 脚本的正则表达式功能类似,使用这一内嵌于 Python 的语言工具,尽管不能 ...