首页 > 其他分享 >HDU 3743 Frosh Week(树状数组+离散化)

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

时间:2023-06-12 14:35:37浏览次数:36  
标签:Week HDU int 3743 number student team line include


题意和思路:和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



 






标签:Week,HDU,int,3743,number,student,team,line,include
From: https://blog.51cto.com/u_16156555/6462544

相关文章

  • POJ 2352 HDU1541 Stars(树状数组)
    题意:二维平面给定n个点的坐标,然后要你输出每个点的“等级“。每个点的等级是它的左下放的点个数(包括正下放和正左方的点)。即要你输出对于每个点(x,y)来说,有多少点的坐标(xi,yi)满足xi<=x且yi<=y。思路:题目给出的坐标中已经是按y升序排列,那么其实只用考虑x轴,那么显然就是在前面的......
  • HDU 3277 Marriage Match III(并查集+二分+最大流)
    题意:和HDU3081一样的题意,只不过多了一个条件,每个女孩除了能选自己喜欢的男生之外,还能选不超过K个自己不喜欢的男生,问游戏最多能进行几轮思路:除了选喜欢的,还能选任意K个不喜欢的,怎么建图呢?一开始我想每个女孩连喜欢的男孩,而且选K个不喜欢的男孩也连边,可是这K个要怎么确定呢?这种显然......
  • HDU 3081 Marriage Match II(二分+并查集+最大流)
    题意:有N个女孩要与N个男孩玩配对游戏.每个女孩有一个可选男孩的集合(即该女孩可以选自己集合中的任意一个男孩作为该轮的搭档).然后从第一轮开始,每个女孩都要和一个不同的男孩配对.如果第一轮N个女孩都配对成功,那么就开始第二轮配对,女孩依然从自己的备选男孩集合中选择,但是不能......
  • HDU 1556 Color the ball(树状数组区间更新)
    题意:中文题思路:一道区间更新,单点查询的裸题,用线段树做更好,因为还没看到所以这里用树状数组做。树状数组标记区间的方法很特别,比如给区间[a,b]内的气球涂颜色时,我们add(a,1),add(b+1,-1),单点查询的时候sum(x)就是x这个气球被涂色的总次数。建议先在纸上自己试一下看看,有点抽象,可以这......
  • hdu2084数塔(DP)
    思路:简单的数塔DP#include<stdio.h>#include<string.h>#include<algorithm>usingnamespacestd;inta[105][105],dp[105][105];intmain(){intt,n,i,j;scanf("%d",&t);while(t--){scanf("%d",......
  • HDU 5491 The Next(构造)
    题意:给你D(D<=2^31),s1和s2,求比D大的第一个数并且这个数二进制1的数目在[s1,s2]范围里,输出这个数思路:直接从D+1算起,如果1的数目>s2那就跳过,如果s1>sum1,那么就将这个数的低位s1-sum1个0补成1,那么就是最优的了#include<bits/stdc++.h>usingnamespacestd;#defineLLlonglongint......
  • HDU 5489 Removed Interval(DP)
    题意:求去掉某一个长度为L的子串的LIS思路:画画图其实比较显然的想法是去掉这个区间的时候答案是右边以第一个数开头的LIS+左边最后一个数小于右边第一个数的LIS,为什么是右边以第一个数开头的LIS呢,因为如果是在这个L的后第二个是最佳答案的话那么我在这个“窗口”滑到L+1这个位置的时......
  • HDU 5492 Find a path(DP)
    思路:将式子化开其实就是求(n+m-1)*s1-s2的最小值,s1表示各个格子的平方和,s2表示和的平方,留意到数据范围较小,令dp[i][j][k]为走到第i行第j列当前和为k的平方和的最小值,最后答案就是(n+m-1)*dp[i][j][k]-k*k#include<bits/stdc++.h>usingnamespacestd;#defineinf1e9inta[33][3......
  • 考研周记-week16
    准时的周记6.5~6.11记录一下本周的考研进度情况英语本周继续单词的复习,因为最近一直在赶数学的进度,所以将单词放在了晚上回宿舍之后在背,等到线代过完一轮,就恢复正常的英语复习数学数学方面,本周结束了概率论的复习,线性代数也进行了一半,准备下周结束数学一基础阶段的第一轮复......
  • 2024备考Week13
    一、本周总结:使用时间:(先目标30h,刚达到,下周目标35h)总计30h,数学10h40min,专业课8h50min,英语9h30min。整体效率比之前高,在逐步恢复学习状态中。二、存在问题:1.数学所花时间不够,英语所花时间太多,时间分配不均;2.学习时间有待进一步挖掘,还有很大提升空间,争取下周35h,逐步提升学习状态。......