PAT (Top Level) Practise 1009 Triple Inversions (35)
来源:互联网 发布:linux误删除网卡文件 编辑:程序博客网 时间:2024/06/05 18:07
1009. Triple Inversions (35)
Given a list of N integers A1, A2, A3,...AN, there's a famous problem to count the number of inversions in it. An inversion is defined as a pair of indices i < j such that Ai > Aj.
Now we have a new challenging problem. You are supposed to count the number of triple inversions in it. As you may guess, a triple inversion is defined as a triple of indices i < j < k such that Ai > Aj > Ak. For example, in the list {5, 1, 4, 3, 2} there are 4 triple inversions, namely (5,4,3), (5,4,2), (5,3,2) and (4,3,2). To simplify the problem, the list A is given as a permutation of integers from 1 to N.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N in [3, 105]. The second line contains a permutation of integers from 1 to N and each of the integer is separated by a single space.
Output Specification:
For each case, print in a line the number of triple inversions in the list.
Sample Input:221 2 3 4 5 16 6 7 8 9 10 19 11 12 14 15 17 18 21 22 20 13Sample Output:
8
求三个数的逆序对数,只要求出一个数前后的逆序数相乘即可,直接树状数组都不用离散。
#include<cstdio>#include<cstring>#include<cmath>#include<queue>#include<vector>#include<iostream>#include<stack>#include<algorithm>#include<bitset>#include<functional>using namespace std;typedef unsigned long long ull;typedef long long LL;const int maxn = 1e5 + 10;const int low(int x){ return x&-x; }int n, a[maxn], f[maxn], L[maxn], R[maxn];void add(int x){ for (int i = x; i <= n; i += low(i)) f[i]++;}int get(int x){ int ans = 0; for (int i = x; i; i -= low(i)) ans += f[i]; return ans;}int main(){ while (scanf("%d", &n) != EOF) { for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= n; i++) f[i] = 0; for (int i = 1; i <= n; i++) L[i] = i - 1 - get(a[i]), add(a[i]); for (int i = 1; i <= n; i++) f[i] = 0; for (int i = n; i; i--) R[i] = get(a[i]), add(a[i]); LL ans = 0; for (int i = 1; i <= n; i++) ans += (LL)L[i] * R[i]; printf("%lld\n", ans); } return 0;}
- PAT (Top Level) Practise 1009 Triple Inversions (35)
- pat-top 1009. Triple Inversions (35)
- PAT (Top Level) Practise 1002Business (35)
- PAT (Top Level) Practise 1018Subnumbers (35)
- PAT-TL 1009. Triple Inversions
- PAT (Top Level) Practise 1003 Universal Travel Sites (35)
- PAT (Top Level) Practise 1005Programming Pattern (35)
- PAT (Top Level) Practise 1006 Tree Traversals - Hard Version (35)
- PAT (Top Level) Practise 1007 Red-black Tree (35)
- PAT (Top Level) Practise 1008Airline Routes (35)
- PAT (Top Level) Practise 1010Lehmer Code (35)
- PAT (Top Level) Practise 1011 Cut Rectangles (35)
- PAT (Top Level) Practise 1012Greedy Snake (35)
- PAT (Top Level) Practise 1013 Image Segmentation (35)
- PAT (Top Level) Practise 1014 Circles of Friends (35)
- PAT (Top Level) Practise 1015. Letter-moving Game (35)
- PAT (Top Level) Practise 1016 Uniqueness of MST (35)
- PAT (Top Level) Practise 1017 The Best Peak Shape (35)
- php连接mysql
- 乘火车选座小技巧(须知)
- Fast-rcnn Notes
- 互联网大负载系统开发中的session管理
- 公司用的-----------在一个字符串前面补指定个数的0或者空格,
- PAT (Top Level) Practise 1009 Triple Inversions (35)
- php memcached拓展setOption
- iOS直播
- 计算机网络19--P2P应用
- Maze problem 的OO Design - 写给煜小堆
- IO_test
- [PhoneGap] Android開發Facebook取得Key Hashes
- POJ 3264 Balanced Lineup (RMQ)
- AFNetworking通讯框架