HDU 5792 World is Exploding
题意:
给出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的更多相关文章
- HDU 5792 World is Exploding 树状数组+枚举
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5792 World is Exploding Time Limit: 2000/1000 MS (Ja ...
- HDU 5792 World is Exploding (树状数组)
World is Exploding 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5792 Description Given a sequence ...
- hdu 5792 World is Exploding 树状数组
World is Exploding 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5792 Description Given a sequence ...
- HDU 5792 World is Exploding(树状数组+离散化)
http://acm.split.hdu.edu.cn/showproblem.php?pid=5792 题意: 思路: lmin[i]:表示左边比第i个数小的个数. lmax[i]:表示左边比第i个 ...
- hdu 5792 World is Exploding 树状数组+离散化+容斥
World is Exploding Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- HDU 5792 World is Exploding (离散化+树状数组)
题意:给定 n 个数,让你数出 a < b && c < d && a != b != c != d && Aa < Ab & ...
- HDU 5792:World is Exploding(树状数组求逆序对)
http://acm.hdu.edu.cn/showproblem.php?pid=5792 World is Exploding Problem Description Given a sequ ...
- hdu 5792(树状数组,容斥) World is Exploding
hdu 5792 要找的无非就是一个上升的仅有两个的序列和一个下降的仅有两个的序列,按照容斥的思想,肯定就是所有的上升的乘以所有的下降的,然后再减去重复的情况. 先用树状数组求出lx[i](在第 i ...
- HDU 5792 L - World is Exploding 。容斥原理 + 树状数组 + 离散化
题目,要求找出有多少对这样的东西,四个数,并且满足num[a]<num[b] &&num[c]>num[d] 要做这题,首先要懂得用树状数组,我设,下面的小于和大于都是严格 ...
随机推荐
- C# WinFrom 导入Excel文件,显示进度条
因为WINForm程序是在64位上运行如果使用另外一种快速的读取Excel的方法会报“未在本地计算机上注册“Microsoft.Jet.OLEDB.12.0”提供程序” 所以我就换了现在这种读取有点慢 ...
- leetcode_最长公共前缀
题目:Write a function to find the longest common prefix string amongst an array of strings. 题解:给出的函数为: ...
- CDZSC_2015寒假新人(2)——数学 B
B - B Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status ...
- Lucene学习总结之五:Lucene段合并(merge)过程分析
一.段合并过程总论 IndexWriter中与段合并有关的成员变量有: HashSet<SegmentInfo> mergingSegments = new HashSet<Segm ...
- 配置sphinx
1.先安装sphinxclient #cd /usr/local/src #wget http://sphinxsearch.com/files/sphinx-0.9.9.tar.gz ...
- Django学习之路
Django Website Collect django packages http://djangopackages.com/ django project http://code.djangop ...
- You and your research
英文版http://www.cs.virginia.edu/~robins/YouAndYourResearch.html 视频版http://www.youtube.com/watch?v=a1zD ...
- MyEclipse6.5安装SVN插件的三种方法
MyEclipse6.5安装SVN插件的三种方法 方法一.如果可以上网可在线安装 1. 打开Myeclipse,在菜单栏中选择Help→Software Updates→Find and Instal ...
- invesments 第三章 上
1. How firms issue securities: 公司如何发行股票 A. primary market: 新的股票,债券和其他的证券第一次发行的market B. ...
- live555学习之RTSP连接建立以及请求消息处理过程
1,RTSP连接的建立过程 RTSPServer类用于构建一个RTSP服务器,该类同时在其内部定义了一个RTSPClientSession类,用于处理单独的客户会话. 首先创建RTSP服务 ...