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. DNS域欺骗攻击详细教程之Linux篇

    .DNS域欺骗攻击原理 DNS欺骗即域名信息欺骗是最常见的DNS安全问题.当一 个DNS服务器掉入陷阱,使用了来自一个恶意DNS服务器的错误信息,那么该DNS服务器就被欺骗了.DNS欺骗会使那些易受攻 ...

  2. java如何在一个有序的数组类插入一个数!

    第一种:依次与有序数组中的每个数进行比较,然后找到位置之后,定义一个新的数组,该信数组的长度加一,再使用system.arraycopy将于数组copy到新数组!import java.util.Ar ...

  3. J2SE知识点摘记(一)

    1.        数组的声明时无法指定数组的长度. 2.        一维数组的声明和内存的分配 "数据类型    数组名[]; //声明一维数组     数组名=  new 数据类型[ ...

  4. rdlc部署zt

    原文:rdlc部署zt 偶然间遇到“ 未能加载文件或程序集microsoft.reportviewer.winforms ……”的一个错误,以前web是遇到过,没想到winform部署也会遇到.找了半 ...

  5. Linux中service命令和/etc/init.d/的关系

    Linux中service命令和/etc/init.d/的关系   service xxx启动 /etc/init.d/ 目录下的xxx脚本 如一个脚本名为 mysvc保存在/etc/init.d/下 ...

  6. Advanced Fruits(好题,LCS的模拟)

    Advanced Fruits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  7. UVA 10163 Storage Keepers(dp + 背包)

    Problem C.Storage Keepers  Background Randy Company has N (1<=N<=100) storages. Company wants ...

  8. table+js实现网站左侧列表下拉隐藏

    <script language="javascript">                    function showHide(obj){            ...

  9. Web设计中打开新页面或页面跳转的方法 js跳转页面

    Web设计中打开新页面或页面跳转的方法 一.asp.net c# 打开新页面或页面跳转 1. 最常用的页面跳转(原窗口被替代):Response.Redirect("newpage.aspx ...

  10. 20141021WinForm控件属性

    一.Form对象属性:Name:窗体类的类名(一)外观:BackColor:窗体背景色ForeColor:窗体文字色BackgroundImage:背景图片Font:设置字体FormBorderSty ...