sgu180:Inversions
来源:互联网 发布:二手车估价软件排名 编辑:程序博客网 时间:2024/06/06 05:23
180. Inversions
time limit per test: 0.25 sec.
memory limit per test: 4096 KB
memory limit per test: 4096 KB
input: standard
output: standard
output: standard
There are N integers (1<=N<=65537) A1, A2,.. AN (0<=Ai<=10^9). You need to find amount of such pairs (i, j) that 1<=i<j<=N and A[i]>A[j].
Input
The first line of the input contains the number N. The second line contains N numbers A1...AN.
Output
Write amount of such pairs.
Sample test(s)
Input
5 2 3 1 5 4
Output
3
裸的求逆序对,果断树状数组+离散化。
ps:1.排序的条件写<,不要写<=
2.相同的数,优先级也相同
ps:1.排序的条件写<,不要写<=
2.相同的数,优先级也相同
#include <cstdio>#include <algorithm>using namespace std;const int MAXN = 65540;int N = 0;struct number{ int v; int node;}A[MAXN] = {0};int c[MAXN] = {0}, t[MAXN] = {0};int top = 0;bool cmp(const number &P, const number &Q) { return P.v < Q.v;}int find(int cur){ int re = 0; for(; cur; cur -= cur&(-cur)) re += c[cur]; return re;}void insert(int cur){ for(; cur <= top; cur += cur&(-cur)) c[cur] += 1;}int main(){ scanf("%d", &N); for(int i = 1; i <= N; ++i) { scanf("%d", &A[i].v); A[i].node = i; } sort(A+1, A+N+1, cmp); A[0].v = -20; for(int i = 1; i <= N; ++i) t[A[i].node] = A[i-1].v!=A[i].v?++top:top; long long ans = 0; for(int i = 1; i <= N; ++i) { insert(t[i]);ans += (long long)(i-find(t[i])); } printf("%I64d", ans); return 0;}
0 0
- sgu180:Inversions
- SGU180——Inversions
- SGU180:Inversions(树状数组)
- sgu180 Inversions(树状数组+离散化)
- sgu180
- Inversions
- sgu 180 Inversions
- SGU 180 Inversions
- SGU 180 Inversions
- SGU 180 Inversions
- 1009. Triple Inversions (35)
- Codechef:Sherlock and Inversions
- 1009. Triple Inversions (35)
- Kattis:K-Inversions
- Ural1523-K-inversions
- 1009. Triple Inversions (35)
- Infinite Inversions CodeForces
- Little Elephant and Inversions CF220E
- Python学习笔记-第三章 使用字符串
- Python 文件对象
- 第十二周项目三(4)用递归法输出Fibnacci第20项数
- R 语言与简单的回归分析
- d3d在win消息循环是也能显示
- sgu180:Inversions
- R语言与回归分析几个假设的检验
- Android4.4 Framework分析——Zygote进程的启动过程
- eclipse删除空行或者删除注释
- IOS ARC项目使用非ARC文件 MRC项目使用ARC文件
- VC++6.0和VC++2010的区别
- R语言学习系列(数据挖掘之决策树算法实现--ID3代码篇)
- CodeForces - 482C Game with Strings(状压DP,期望)
- MFC动态画线,画圆,画椭圆,画矩形