CodeForces - 61E Enemy is weak

Description

The Romans have attacked again. This time they are much more than the Persians but Shapur is ready to defeat them. He says: "A lion is never afraid of a hundred sheep".

Nevertheless Shapur has to find weaknesses in the Roman army to defeat them. So he gives the army a weakness number.

In Shapur‘s opinion the weakness of an army is equal to the number of triplets
i,?j,?k such that
i?<?j?<?k and ai?>?aj?>?ak where
ax is the power of man standing at position
x. The Roman army has one special trait — powers of all the people in it are distinct.

Help Shapur find out how weak the Romans are.

Input

The first line of input contains a single number n (3?≤?n?≤?106) — the number of men in Roman army. Next line contains
n different positive integers
ai (1?≤?i?≤?n,?1?≤?ai?≤?109) — powers of men in the Roman army.

Output

A single integer number, the weakness of the Roman army.

Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use
cout (also you may use
%I64d).

Sample Input

Input

3
3 2 1

Output

1

Input

3
2 3 1

Output

0

Input

4
10 8 3 1

Output

4

Input

4
1 5 4 3

Output

1

题意:求满足三个数是下标i < j < k, 值 a[i] > a[j] > a[k] 的个数

思路:对于每一个数我们向前找比它大的数,向后找比它小的数,相乘就得到这个数的结果了。然后统计全部数的可能,基于数量太大,我们用归并算法。在处理的时候计算

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 1e6 + 10;

struct Node {
	ll val, front, rear;
} a[maxn], b[maxn];
ll ans;

void merge_sort(Node *A, int x, int y, Node *T) {
	if (y - x > 1) {
		int m = x + (y -x) / 2;
		int p = x, q = m, i = x;
		merge_sort(A, x, m, T);
		merge_sort(A, m, y, T);
		while (p < m || q < y) {
			if (q >= y || (p < m && A[p].val <= A[q].val)) {
				ans += A[p].front * (q - m);
				A[p].rear += (q - m);
				T[i++] = A[p++];
			}
			else {
				ans += A[q].rear * (m - p);
				A[q].front += (m - p);
				T[i++] = A[q++];
			}
		}
		for (i = x; i < y; i++)
			A[i] = T[i];
	}
}

int main() {
	int n;
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
		scanf("%lld", &a[i].val);
	ans = 0;
	merge_sort(a, 0, n, b);
	cout << ans << endl;
	return 0;
}
时间: 2024-10-12 14:19:04

CodeForces - 61E Enemy is weak的相关文章

CodeForces 61E Enemy is weak 求i&lt;j&lt;k &amp;&amp; a[i]&gt;a[j]&gt;a[k] 的对数 树状数组

题目链接:点击打开链接 题意是求 i<j<k && a[i]>a[j]>a[k] 的对数 如果只有2元组那就是求逆序数的做法 三元组的话就用一个树状数组x表示 数字i前面有多少个比自己大的个数 然后每次给这个y数组求和,再把x中>a[i]的个数存入y中即可 #include <algorithm> #include <cctype> #include <cassert> #include <cstdio> #in

三元逆序对 求i&lt;j&lt;k &amp;&amp; a[i]&gt;a[j]&gt;a[k] 的对数 树状数组Codeforces 61E Enemy is weak

http://codeforces.com/problemset/problem/61/E E. Enemy is weak time limit per test 5 seconds memory limit per test 256 megabytes input standard input output standard output The Romans have attacked again. This time they are much more than the Persian

Codeforces 61E Enemy is weak 乞讨i&amp;lt;j&amp;lt;k &amp;amp;&amp;amp; a[i]&amp;gt;a[j]&amp;gt;a[k] 对数的 树阵

主题链接:点击打开链接 意大利正在寻求称号 i<j<k && a[i]>a[j]>a[k] 的对数 假设仅仅有2元组那就是求逆序数的做法 三元组的话就用一个树状数组x表示 数字i前面有多少个比自己大的个数 然后每次给这个y数组求和,再把x中>a[i]的个数存入y中就可以 #include <algorithm> #include <cctype> #include <cassert> #include <cstdio&

Codeforces 741B Arpa&#39;s weak amphitheater and Mehrdad&#39;s valuable Hoses

[题目链接] http://codeforces.com/problemset/problem/741/B [题目大意] 给出一张图,所有连通块构成分组,每个点有价值和代价, 要么选择整个连通块,要么只能在连通块中选择一个,或者不选,为最大价值 [题解] 首先我们用并查集求出连通块,然后对连通块进行分组背包即可. [代码] #include <cstdio> #include <vector> #include <algorithm> #include <cstr

codeforces 742D Arpa&#39;s weak amphitheater and Mehrdad&#39;s valuable Hoses ——(01背包变形)

题意:给你若干个集合,每个集合内的物品要么选任意一个,要么所有都选,求最后在背包能容纳的范围下最大的价值. 分析:对于每个并查集,从上到下滚动维护即可,其实就是一个01背包= =. 代码如下: 1 #include <stdio.h> 2 #include <algorithm> 3 #include <string.h> 4 #include <vector> 5 using namespace std; 6 const int N = 1000 + 5;

cf 61 E. Enemy is weak 离散化+树状数组

题意: 给出一个数组,数组的每一个元素都是不一样的,求出对于3个数组下标 i, j, k such that i < j < k and ai > aj > ak where ax is the value at position x. 的个数 明显数组的值太大了 先离散化,然后就是简单的树状数组了 对于每一个i,只要统计i前面的数中比a[i]大的数的个数,和i后面的数中比a[i]小的数的个数即可 #include <cstdio> #include <cstri

树状数组之求逆对数

Ultra-QuickSort In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. For the input s

Risk UVA - 12264 拆点法+最大流+二分

/** 题目:Risk UVA - 12264 链接:https://vjudge.net/problem/UVA-12264 题意:给n个点的无权无向图(n<=100),每个点有一个非负数ai. 若ai==0则此点归敌方所有,若ai>0则此点归你且上面有ai个属于你的士兵. 保证至少有一个属于你的点与敌方的点相邻.你可以让你的每个士兵最多移动一次 ,每次可以待在原地或者去到相邻的属于你的领地,但每个点至少要留1各士兵, 使得最薄弱的关口尽量坚固.关口是指与敌方点相邻的点,薄弱与坚固分别指兵少

IOS的一个关于球碰撞的小游戏

这个游戏是关于一个球随机在屏幕上移动,可以用手指来操纵令一个球,如果两个球碰撞到一起,就表示输了,非常简单的一个游戏 在StoryBoard里定义两个UIImageView和一个start按钮 然后把这两个UIimageView跟代码链接 @property (weak, nonatomic) IBOutlet UIImageView *player; @property (weak, nonatomic) IBOutlet UIImageView *enemy; @property (weak