首页 > 其他分享 >HDU 2838 Cow Sorting(树状数组)

HDU 2838 Cow Sorting(树状数组)

时间:2023-06-12 14:36:07浏览次数:62  
标签:HDU Sorting Cow int LL cows line include grumpiness


题意:首先每次只能交换相邻的两头牛,并且最后要求升序排列,所以最后整个序列的逆序是0,每次交换只可以消除1个逆序。(令a[i]的逆序是从1到i-1比它大的数的个数。)

思路:对于某个数,要把它变成有序的,那么很容易可以推算出公式就是它自身的逆序数乘自身的值再加上它的逆序数的和,自己算算看看。

吐槽:一开始没想清楚树状数组下标的问题,后来没注意会爆int搞了好久...



#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 100000+100
#define LL long long
int cas=1,T;
int a[maxn];
LL L[maxn];
LL c[maxn];
int lowbit(int x)
{
	return x&(-x);
}
LL sum(int i)
{
	LL 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()
{
	int n;
	while (scanf("%d",&n)!=EOF && n)
	{
		memset(L,0,sizeof(L));
		memset(c,0,sizeof(c));
		for (int i = 1;i<=n;i++)
        {
			scanf("%d",&a[i]);
			L[i]=sum(maxn)-sum(a[i]);
			add(a[i],1);
	//		printf("%d ",L[i]);
		}
		memset(c,0,sizeof(c));
		LL ans = 0;
		for (int i = 1;i<=n;i++)
		{
			LL temp = a[i]*L[i];
            ans+= temp+sum(a[i]);
			add(a[i],a[i]);
		}
		printf("%lld\n",ans);
	}
	//freopen("in","r",stdin);
	//scanf("%d",&T);
	//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
	return 0;
}



Description



Sherlock's N (1 ≤ N ≤ 100,000) cows are lined up to be milked in the evening. Each cow has a unique "grumpiness" level in the range 1...100,000. Since grumpy cows are more likely to damage Sherlock's milking equipment, Sherlock would like to reorder the cows in line so they are lined up in increasing order of grumpiness. During this process, the places of any two cows (necessarily adjacent) can be interchanged. Since grumpy cows are harder to move, it takes Sherlock a total of X + Y units of time to exchange two cows whose grumpiness levels are X and Y. 

Please help Sherlock calculate the minimal time required to reorder the cows.



 



Input



Line 1: A single integer: N 
Lines 2..N + 1: Each line contains a single integer: line i + 1 describes the grumpiness of cow i.



 



Output



Line 1: A single line with the minimal time required to reorder the cows in increasing order of grumpiness.



 



Sample Input



3 2 3 1



 



Sample Output



7



Hint



Input Details Three cows are standing in line with respective grumpiness levels 2, 3, and 1. Output Details 2 3 1 : Initial order. 2 1 3 : After interchanging cows with grumpiness 3 and 1 (time=1+3=4). 1 2 3 : After interchanging cows with grumpiness 1 and 2 (time=2+1=3).



 







标签:HDU,Sorting,Cow,int,LL,cows,line,include,grumpiness
From: https://blog.51cto.com/u_16156555/6462539

相关文章

  • HDU 1394 Minimum Inversion Number(树状数组)
    题意:有一个n个整数的排列,这n个整数就是0,1,2,3...n-1这n个数(但不一定按这个顺序给出)。现在先计算一下初始排列的逆序数,然后把第一个元素a1放到an后面去,形成新排列a2a3a4...ana1,然后再求这个排列的逆序数。继续执行类似操作(一共要执行n-1次)直到产生排列ana1a2...an-1为止。......
  • HDU 3743 Frosh Week(树状数组+离散化)
    题意和思路:和POJ2299几乎一样...离散化+树状数组#include<cstdio>#include<queue>#include<cstring>#include<iostream>#include<cstdlib>#include<algorithm>#include<vector>#include<map>#include<string>#includ......
  • 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......