hdu3743——Frosh Week
2014-08-25 20:29
232 查看
Problem Description
During Frosh Week, students play various fun games to get to know each other and compete against other teams. In one such game, all the frosh on a team stand in a line, and are then asked to arrange themselves according to some criterion,
such as their height, their birth date, or their student number. This rearrangement of the line must be accomplished only by successively swapping pairs of consecutive students. The team that finishes fastest wins. Thus, in order to win, you would like to
minimize the number of swaps required.
Input
The first line of input contains one positive integer n, the number of students on the team, which will be no more than one million. The following n lines each contain one integer, the student number of each student on the team. No
student number will appear more than once.
Output
Output a line containing the minimum number of swaps required to arrange the students in increasing order by student number.
Sample Input
Sample Output
Source
University of Waterloo Local Contest 2010.10.02
Recommend
一开始没离散化,错了2次
During Frosh Week, students play various fun games to get to know each other and compete against other teams. In one such game, all the frosh on a team stand in a line, and are then asked to arrange themselves according to some criterion,
such as their height, their birth date, or their student number. This rearrangement of the line must be accomplished only by successively swapping pairs of consecutive students. The team that finishes fastest wins. Thus, in order to win, you would like to
minimize the number of swaps required.
Input
The first line of input contains one positive integer n, the number of students on the team, which will be no more than one million. The following n lines each contain one integer, the student number of each student on the team. No
student number will appear more than once.
Output
Output a line containing the minimum number of swaps required to arrange the students in increasing order by student number.
Sample Input
3 3 1 2
Sample Output
2
Source
University of Waterloo Local Contest 2010.10.02
Recommend
一开始没离散化,错了2次
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; int tree[1000010]; int arr[1000010]; struct node { int Id; __int64 v; }val[1000010]; int n; int cmp(node a,node b) { return a.v<b.v; } inline int lowbit(int x) { return x&(-x); } void add(int x,int val) { for(int i=x;i<=1000010;i+=lowbit(i)) tree[i]+=val; } __int64 sum(int x) { __int64 ans=0; for(int i=x;i;i-=lowbit(i)) ans+=tree[i]; return ans; } int main() { while(~scanf("%d",&n)) { __int64 ans=0; memset(tree,0,sizeof(tree)); for(int i=1;i<=n;i++) { scanf("%I64d",&val[i].v); val[i].Id=i; } sort(val+1,val+n+1,cmp); for(int i=1;i<=n;i++) arr[val[i].Id]=i; for(int i=1;i<=n;i++) { add(arr[i],1); ans+=i-sum(arr[i]); } printf("%I64d\n",ans); } return 0; }
相关文章推荐
- HDU 3743 Frosh Week
- Frosh Week (hdu 3743 树状数组)
- hdu 3743 Frosh Week (归并排序·逆序数)
- hdu 3743 Frosh Week(求逆序数方法总结)
- HDU 3743 Frosh Week(树状数组或归并排序求逆序)
- HDU 3743 Frosh Week(逆序对-BIT)
- HDU 3743 Frosh Week 树状数组
- HDU - 3743 Frosh Week(树状数组+离散化)
- HDU 3743 Frosh Week 【归并排序模板题】
- HDU 3743 Frosh Week(树状数组+离散化)
- HDU 3743 Frosh Week (归并排序)
- HDU 3743 Frosh Week(归并排序求逆序对)
- HDU 3743 Frosh Week
- 【树状数组(逆序数)】hdu 3743 Frosh Week
- HDU 3743 Frosh Week【树状数组 离散化】
- hdu 3743 Frosh Week (离散化+树状数组,求逆序对)
- Frosh Week(HDU_3743)归并排序+逆序数对
- HDU 3743 Frosh Week
- HDU 3743 Frosh Week(树状数组求逆序数)
- 归并排序 hdu 3743