NYOJ 233 &&NYOJ 322 Sort(树状数组)
2015-02-25 20:20
393 查看
链接:click here
题意:
描述 You want to processe a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. Then how many times it need.
For example, 1 2 3 5 4, we only need one operation : swap 5 and 4.
输入The input consists of T number of test cases.(<0T<1000) Each case consists of two lines: the first line contains a positive integer n (n <= 1000); the next line contains a permutation of the n integers from 1 to n.输出For each case, output the minimum times need to sort it in ascending order on a single line.样例输入
样例输出
思路:就是求一组数据的逆序数,树状数组求法,不解释:
代码:
题意:
描述 You want to processe a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. Then how many times it need.
For example, 1 2 3 5 4, we only need one operation : swap 5 and 4.
输入The input consists of T number of test cases.(<0T<1000) Each case consists of two lines: the first line contains a positive integer n (n <= 1000); the next line contains a permutation of the n integers from 1 to n.输出For each case, output the minimum times need to sort it in ascending order on a single line.样例输入
2 3 1 2 3 4 4 3 2 1
样例输出
0 6
思路:就是求一组数据的逆序数,树状数组求法,不解释:
代码:
#include <math.h> #include <queue> #include <deque> #include <vector> #include <stack> #include <stdio.h> #include <ctype.h> #include <string.h> #include <stdlib.h> #include <iostream> #include <algorithm> using namespace std; #define lowbit(a) a&-a #define Max(a,b) a>b?a:b #define Min(a,b) a>b?b:a #define mem(a,b) memset(a,b,sizeof(a)) int dir[4][2]= {{1,0},{-1,0},{0,1},{0,-1}}; const double eps = 1e-6; const double Pi = acos(-1.0); static const int inf= ~0U>>2; static const int maxn =110; int h[1000001],w[100],Map[200]; int m[10001], N; void Add(int i) { while(i<=1001) { m[i]++; i+=lowbit(i); } } int Sum(int i) { int res=0; while(i>0) { res+=m[i]; i-=lowbit(i); } return res; } int main() { int T,temp; scanf("%d",&T); while(T--) { scanf("%d",&N); memset(m,0,sizeof(m)); int ans=0; for(int i=1; i<=N; i++) { scanf("%d",&temp); Add(temp); ans+=(i-Sum(temp)); } printf("%d\n",ans); } return 0; }When you want to give up, think of why you persist until now!
相关文章推荐
- NYOJ 322 Sort (归并排序求逆序数) (树状数组求逆序数)
- nyoj322 sort 归并排序,树状数组
- nyoj 322 Sort 【树状数组】
- NYOJ 322 Sort 和 NYOJ 116 士兵杀敌(二) 【树状数组】
- NYOJ117&& 树状数组求逆序数
- ACM--归并排序&&树状数组--nyoj 117--求逆序数
- nyoj 士兵杀敌(四) 123 (线段树&&树状数组) 更新区间值求特定位置的值
- nyoj--108--士兵杀敌(一)(区间求和&&树状数组)
- poj2299 Ultra-QuickSort&&NYOJ117 求逆序数 (树状数组求逆序对数+离散化)+(归并排序)
- ♥NYOJ 123-士兵杀敌(四)【线段树&&树状数组】
- nyoj--108--士兵杀敌(一)(区间求和&&树状数组)
- NYOJ 117 求逆序数 【树状数组】或【归并排序】
- nyoj 123 树状数组 插线问点
- hdu 1166 敌兵布阵(水...线段树&&树状数组)
- NYOJ 士兵杀敌(二) 树状数组
- NYOJ 士兵杀敌(四) 树状数组
- NYOJ 233 Sort it
- poj&nbsp;2352&nbsp;Stars(树状数组)
- HDOJ 2852 KiKi's K-Number(树状数组)
- NYOJ-522 Interval【树状数组】