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. knockout computed实例

    function Privilege(options) { var self = this; self.fieldId = options.fieldId; self.readAccessType = ...

  2. python中实现多线程的几种方式

    python实现多线程的方式大概有 1.threading 2._thread #!/usr/bin/python #!coding:utf-8 import threading def action ...

  3. Linux系统针对网卡中断的优化处理

    摘要: 中断: 当网卡接收到数据包后,会触发硬中断,通知CPU来收包.硬中断是一个CPU和网卡交互的过程.这其实会消耗CPU资源.特别是在使用速度极快的万兆网卡 之后,大量的网络交互使得CPU很大一部 ...

  4. Div布局案例

    通常看到这个页面,会想到它是有几块组成的. 第一块,分销佣金. 第二块,包括代言.商品.二维码 其中代言又是左右结构. 于是乎基本的div结构就出来了. <div class="row ...

  5. Java实现验证码图片

    这样的实现方式对JDK版本有要求. 1 package edu.cloud.editmap.utils; import java.awt.Color; import java.awt.Font; im ...

  6. U-boot中实现Yaffs2+HwEcc

    经过老手的指点,要实现Yaffs2+HwEcc,重点在于chip->ops.mode由MTD_OOB_RAW到MTD_OOB_AUTO.经过几天的筹备,今天要对其下手了.为了真实展现分析移植过程 ...

  7. 在magento中使用正则式

    $sqlCondition = "IFNULL(_table_name.value, _table_name_default.value) REGEXP '^[^a-zA-Z]'" ...

  8. html表单提交的几种方法

    原文地址:http://www.ijser.cn/?p=34 最普通最经常使用最一般的方法就是用submit type..看代码: <form name=”form” method=”post” ...

  9. UI标签库专题十三:JEECG智能开发平台 ckfinder(ckfinder插件标签)

    1. ckfinder(ckfinder插件标签) 1.1. 參数 属性名 类型 描写叙述 是否必须 默认值 name string 属性名称 是 null value string 默认值 否 nu ...

  10. Qt on Android:QTableView不显示选中虚框

    在使用 QTableView 或 QTableWidget 时.有时我们不想要选中虚框,能够实现一个 ItemDelegate ,重写 drawFocus() 和 drawCheck()  两个虚函数 ...