HDU 3743 Frosh Week(树状数组+离散化)

题意和思路:和POJ2299几乎一样...离散化+树状数组


#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
using namespace std;
#define maxn 1000000+10
#define LL long long
int cas=1,T;
LL a[maxn];
LL b[maxn];

int c[maxn];
int maxx,n;
int lowbit(int i)
{
	return i&(-i);
}
int sum(int i)
{
	int ans = 0;
	while (i)
	{
		ans +=c[i];
		i-=lowbit(i);
	}
	return ans;
}
void add(int i,int d)
{
	while (i<=maxn)
	{
		c[i]+=d;
		i+=lowbit(i);
	}
}

int main()
{
	//freopen("in","r",stdin);
	while (scanf("%d",&n)!=EOF && n)
	{
		for (int i = 1;i<=n;i++)
		{
			scanf("%d",&a[i]);
			b[i] = a[i];
		}
		sort(a+1,a+1+n);
        for (int i = 1;i<=n;i++)
		{
			b[i] = upper_bound(a+1,a+n+1,b[i])-a-1;
		}
/*		for (int i = 1;i<=n;i++)
			printf("%d ",b[i]);*/
		memset(c,0,sizeof(c));
		LL ans = 0;
		for (int i = 1;i<=n;i++)
		{
			add(b[i],1);
			ans+=sum(n)-sum(b[i]);
		}
		printf("%lld\n",ans);
	}
	//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
	return 0;
}

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

       
       
3 3 1 2
 

Sample Output

       
       
2
 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值