您的位置:首页 > 其它

HDU 5792 World is Exploding

2016-08-03 16:50 495 查看
Problem Description

Given a sequence A with length n,count how many quadruple (a,b,c,d) satisfies: a≠b≠c≠d,1≤a<b≤n,1≤c<d≤n,Aa<Ab,Ac>Ad.

 

Input

The input consists of multiple test cases. 

Each test case begin with an integer n in a single line.

The next line contains n integers A1,A2⋯An.
1≤n≤50000
0≤Ai≤1e9

 

Output

For each test case,output a line contains an integer.

 

Sample Input

4
2 4 1 3
4
1 2 3 4

 

Sample Output

1
0

按照b来计算答案,看看左边a的数量,以及每一对会产生的数量。
#include<set>
#include<map>
#include<cmath>
#include<stack>
#include<queue>
#include<bitset>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define rep(i,j,k) for (int i = j; i <= k; i++)
#define per(i,j,k) for (int i = j; i >= k; i--)
using namespace std;
typedef __int64 LL;
const int low(int x) { return x&-x; }
const double eps = 1e-8;
const int INF = 0x7FFFFFFF;
const int mod = 1e9 + 7;
const int N = 1e5 + 10;
int T, n, m, a
, b
;
LL f[2]
, s
;

LL get(int t, int x)
{
LL res = 0;
for (int i = x; i; i -= low(i)) res += f[t][i];
return res;
}

void insert(int t, int x, LL y)
{
for (int i = x; i < m; i += low(i)) f[t][i] += y;
}

int main()
{
//scanf("%d", &T);
//while (T--)
while (scanf("%d", &n) != EOF)
{
rep(i, 1, n) scanf("%d", &a[i]), b[i] = a[i];
sort(b + 1, b + n + 1); m = unique(b + 1, b + n + 1) - b;
rep(i, 1, n) a[i] = lower_bound(b + 1, b + m, a[i]) - b;
rep(i, 1, m) f[0][i] = f[1][i] = 0;
LL ans = 0, sum = 0;
per(i, n, 1)
{
s[i] = get(0, a[i] - 1);
insert(0, a[i], 1);
}
rep(i, 1, n)
{
insert(1, a[i], 1);
s[i] += i - get(1, a[i]);
sum += s[i];
}
sum /= 2;
rep(i, 1, m) f[0][i] = f[1][i] = 0;
rep(i, 1, n)
{
ans += get(0, a[i] - 1)*(sum - s[i]) - get(1, a[i] - 1);
insert(0, a[i], 1); insert(1, a[i], s[i]);
}
printf("%lld\n", ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  HDU