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;
}

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

  1. 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元组那就是求逆序数的做法 三元组的话就用一个树状 ...

  2. cf 61E. Enemy is weak 树状数组求逆序数(WA) 分类: Brush Mode 2014-10-19 15:16 104人阅读 评论(0) 收藏

    #include <iostream> #include <algorithm> #include <cstdio> #include <cstring> ...

  3. E. Enemy is weak 解析(思維、離散化、BIT、線段樹)

    Codeforce 61 E. Enemy is weak 解析(思維.離散化.BIT.線段樹) 今天我們來看看CF61E 題目連結 題目 給一個數列\(a\),求有多少\((i,j,k)\),\(i ...

  4. 题解-Enemy is weak

    Enemy is weak 求序列 \(a\{n\}\) 中的三元逆序对数量. 数据范围:\(3\le n\le 1e6\). 这题真是一道又好又水的题,可是我看别人的题解做法真是玄学难懂,于是蒟蒻要 ...

  5. Codeforces Beta Round #57 (Div. 2) E. Enemy is weak

    求满足条件的三元组的个数,可以转换求一元组和二元组组成的满足条件的三元组的个数,且对于(x),(y,z),x > y,且x出现的p_x < p_y. x可直接枚举O(n),此时需要往后查询 ...

  6. Codeforces 741B Arpa's weak amphitheater and Mehrdad's valuable Hoses

    [题目链接] http://codeforces.com/problemset/problem/741/B [题目大意] 给出一张图,所有连通块构成分组,每个点有价值和代价, 要么选择整个连通块,要么 ...

  7. codeforces 742D Arpa's weak amphitheater and Mehrdad's valuable Hoses ——(01背包变形)

    题意:给你若干个集合,每个集合内的物品要么选任意一个,要么所有都选,求最后在背包能容纳的范围下最大的价值. 分析:对于每个并查集,从上到下滚动维护即可,其实就是一个01背包= =. 代码如下: #in ...

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

    题意: 给出一个数组,数组的每一个元素都是不一样的,求出对于3个数组下标 i, j, k such that i < j < k and ai > aj > ak where ...

  9. Codeforces 741B Arpa's weak amphitheater and Mehrdad's valuable Hoses (并查集+分组背包)

    <题目链接> 题目大意: 就是有n个人,每个人都有一个体积和一个价值.这些人之间有有些人之间是朋友,所有具有朋友关系的人构成一组.现在要在这些组中至多选一个人或者这一组的人都选,在总容量为 ...

随机推荐

  1. 修改默认的undo_retention参数设置

    昨天,一个朋友的数据库数据被误操作删除掉了,请求我帮忙进行恢复. 数据库版本是Oracle10g Release 2的,我首先想到的是使用Flashback Query进行闪回恢复,不幸的是ORA-0 ...

  2. Oracle EBS-SQL (PO-7):检查异常-非批准的供应商设置供货比例.sql

    select distinct msr.sourcing_rule_name            名称,msi.description                          说明,msi ...

  3. VS 2012 插件卸载(删除自己安装的插件)

    给VS 装了一个插件,装完之后感觉别扭,所以想卸载,[工具]--> [扩展和更新]-->[找到想要卸载的插件点击一下就会出现禁用或卸载]

  4. click through rate prediction

    包括内容如下图: 使用直接估计法,置信区间置信率的估计: 1.使用二项分布直接估计 $p(0.04<\hat{p}<0.06) = \sum_{0.04n\leq k \leq 0.06n ...

  5. LINQ查询操作符 LINQ学习第二篇[转]

    一.投影操作符 1. Select Select操作符对单个序列或集合中的值进行投影.下面的示例中使用select从序列中返回Employee表的所有列: using (NorthwindDataCo ...

  6. C# DataTable几个常用的查询表达式【转】

    DataTable dt = GetDetails().Tables[0]; //获取可用的DataTable            // var m = dt.AsEnumerable().Last ...

  7. win下vm10+mac os 10.9安装遇到问题

    在windows 8下安装vm10.0.0+mac os 10.9遇到问题记录例如以下: 一.因为之前我装的vm9+mac os 10.7: 二.准备安装mac os 10.9,把vm9换成vm10: ...

  8. 跟我一起学extjs5(24--模块Form的自己定义的设计[2])

    跟我一起学extjs5(24--模块Form的自己定义的设计[2])         在本节中将要增加各种类型的字段,在增加字段的时候因为能够一行增加多个字段,因此层次结构又多了一层fieldcont ...

  9. python单/双下划线使用

    在Python编程中经常会遇到函数(function),方法(method)及属性(attribute)以下划线'_'作为前缀,这里做个总结. 主要存在四种情形: 1. object # public ...

  10. 《think in python》学习-2

    高能提示:本文大量编程术语与释义,一些释义如有偏差恕不讨论. 变量,表达式 数据类型: print 4 #打印整数 int print 4.1#打印浮点数 float print "hell ...