题意:

给出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. 在后台操作标记的runat="server"的html标签

    价格: <input id="tbPrice" type="text" runat="server" /> HtmlInputT ...

  2. C#多线程之Parallel中 类似于for的continue,break的方法

    好久没写东西了,终于找到点知识记录下... 利用ParallelLoopState对象来控制Parallel.For函数的执行,ParallelLoopState对象是由运行时在后台创建的: Para ...

  3. 关于android的SQLiteDatabase和Cursor的一些疑问

    android数据库操作的基础有三个类:SQLiteOpenHelper,SQLiteDatabase和Cursor.其中,SQLiteOpenHelper会建立一个数据库连接,它虽然可以调用多次ge ...

  4. Sigmoid function in NN

    X = [ones(m, ) X]; temp = X * Theta1'; t = size(temp, ); temp = [ones(t, ) temp]; h = temp * Theta2' ...

  5. Windows I/O模型之一:Select模型

    1.概念理解 在进行网络编程时,我们常常见到同步(Sync)/异步(Async),阻塞(Block)/非阻塞(Unblock) 四种调用模式: 同步:所谓同步,就是在发出一个功能调用时,在没有得到结果 ...

  6. YUI Array 之each| forEach(遍历)

    1. yui-each原码: 遍历YArray.each = YArray.forEach = Lang._isNative(Native.forEach) ? function (array, fn ...

  7. php常用mysql函数

    mysql_affected_rows: 得到 MySQL 最后操作影响的列数目. mysql_close: 关闭 MySQL 伺服器连线. mysql_connect: 开启 MySQL 伺服器连线 ...

  8. Android中的消息机制:Handler消息传递机制

    参考<疯狂android讲义>第2版3.5 P214 一.背景 出于性能优化考虑,Android的UI操作并不是线程安全的,这意味着如果有多个线程并发操作UI组件,可能导致线程安全问题.为 ...

  9. 关于LayoutParams

    每一个布局均有一个叫LayoutParams的内部类,如: LinearLayout.LayoutParams  RelativeLayout.LayoutParams  AbsoluteLayout ...

  10. 在服务器上php执行某些远程函数出错

    Warning: imagecreatefromjpeg(): php_network_getaddresses: getaddrinfo failed: Name or service not kn ...