题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3743

Frosh Week

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)

Total Submission(s) : 5   Accepted Submission(s) : 1

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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

3

3

1

2

Sample Output

2

题解:

/*本题给定一个数n,然后给出n个数的序列,

要求最少交换多少次可使原序列变成递增的。

根据以往的经验,也就是求序列的逆序数的和。

可用线段树破之。

但本题有个陷阱,即原序列中的n个数并没说是从1到n的某个排列。

对此我们可以先进行离散化。离散化在本题很简单,

只需记录数在原序列中的下标,然后按数值从小到大排序。

排序后的下表id对应该数在原数组中的相对大小,

然后我们按从小到大的顺序插入,查询。*/

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int MAXN=+;
struct node
{
int num,id;
}Per[MAXN]; int C[MAXN];
int Lowbit[MAXN]; //C[i] = a[i-lowbit(i)+1] + …+ a[i],下表从1开始
//Lowbit[i]=i&(i^(i-1));或Lowbit[i]=i&(-i);
//1.查询 int QuerySum(int p)
//查询原数组中下标1-p的元素的和
{
int nSum=;
while(p>)
{
nSum+=C[p];
p-=Lowbit[p];
}
return nSum;
} //2.修改+初始化
void Modify(int p,int val)
//原数组中下表为p的元素+val,导致C[]数组中部分元素值的改变
{
while(p<=MAXN-)
{
C[p]+=val;
p+=Lowbit[p];
}
}
//********************************************** bool cmp(node a,node b)
{
return a.num<b.num;
} int main()
{
int n,i;
__int64 ans;
for(i=;i<MAXN;i++)
Lowbit[i]=i&(-i);
while(~scanf("%d",&n))
{
for(i=;i<=n;i++)
//scanf("%d",&da[i]);
{
scanf("%d",&Per[i].num);
Per[i].id=i;
}
memset(C,,sizeof(C));
ans=;
sort(Per+,Per+n+,cmp);
for(i=;i<=n;i++)
{
ans+=(__int64)QuerySum(n)-(__int64)QuerySum(Per[i].id);
Modify(Per[i].id,);
}
printf("%I64d\n",ans);
}
return ;
}

ps:本来是在树状数组专题做的题目,结果自己写了好几次  RE  MLT  WA 各种错误都出现过  还是没有A掉  无语了

下面是我的错误代码  请无视 谢谢  囧

 /*自己的错误代码  0.0*/
/*re mlt wa 好无语*/
#include<iostream> using namespace std ; const int MAXN=+; int sum[MAXN];
int n ; int lowbit(int x) //取x的最低位1,比如4,则返回4,如5,则返回1
{
return x&(-x);
} void update(int i, int val) //将第i个元素增加val
{
//i的祖先都要增加val
while(i <= n)
{
sum[i] += val;
i += lowbit(i); //将i的二进制未位补为得到其祖先
}
} int Sum(int i) //求前i项的和
{
int s = ;
//将前i项分段
while(i > )
{
s += sum[i];
i -= lowbit(i); //去掉i的二进制最后一个
}
return s;
} int main()
{
int i;
int a[],b[];
while(scanf("%d",&n)!=EOF)
{
int count=;
memset(b,,sizeof(b));
memset(sum,,sizeof(sum));
for(i=;i<=n;i++)
{
scanf("%d",&a[i]);
} for(i=;i<=n; i++)
{
b[i] = Sum(a[i]); //求前a[i]项的和
update(a[i],); //第a[i]个元素+1
count+=i-b[i]-;
} cout<<count<<endl; } return ;
}

HDU 3743 Frosh Week (线段树+离散化)的更多相关文章

  1. HDU - 1542 扫描线入门+线段树离散化

    扫描线算法+线段树维护简介: 像这种求面积的并集的题目,就适合用扫描线算法解决,具体来说就是这样 类似这种给出点的矩形的对角的点的坐标,然后求出所有矩形面积的交集的问题,可以采用扫描线算法解决.图如下 ...

  2. 【HDU】5249-KPI(线段树+离散化)

    好久没写线段树都不知道怎么写了... 很easy的线段树二分问题 #include<cstdio> #include<set> #include<queue> #i ...

  3. HDU 4288 Coder 【线段树+离线处理+离散化】

    题意略. 离线处理,离散化.然后就是简单的线段树了.需要根据mod 5的值来维护.具体看代码了. /* 线段树+离散化+离线处理 */ #include <cstdio> #include ...

  4. HDU5124:lines(线段树+离散化)或(离散化思想)

    http://acm.hdu.edu.cn/showproblem.php?pid=5124 Problem Description John has several lines. The lines ...

  5. POJ 2528 Mayor's posters(线段树+离散化)

    Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...

  6. poj 2528 Mayor's posters(线段树+离散化)

    /* poj 2528 Mayor's posters 线段树 + 离散化 离散化的理解: 给你一系列的正整数, 例如 1, 4 , 100, 1000000000, 如果利用线段树求解的话,很明显 ...

  7. [poj2528] Mayor's posters (线段树+离散化)

    线段树 + 离散化 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayor ...

  8. [UESTC1059]秋实大哥与小朋友(线段树, 离散化)

    题目链接:http://acm.uestc.edu.cn/#/problem/show/1059 普通线段树+离散化,关键是……离散化后建树和查询都要按照基本法!!!RE了不知道多少次………………我真 ...

  9. poj 2528 Mayor's posters 线段树+离散化技巧

    poj 2528 Mayor's posters 题目链接: http://poj.org/problem?id=2528 思路: 线段树+离散化技巧(这里的离散化需要注意一下啊,题目数据弱看不出来) ...

  10. hdu 5700区间交(线段树)

    区间交 Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submiss ...

随机推荐

  1. mysql 主从master-slave同步复制 配置,为读写分离准备

    1.为方便,我在一个windows下安装两个mysql实例,端口分别是 3306.3307 打开 my.ini 或 my-default.ini 文件 配置 basedir datadir 和port ...

  2. PHP判断变量是否存在及函数isset() 、empty()与is_null的区别

    一.举例说明 A.如何判断一个变量是否定义? <?php // 假设不存在$test 变量 if (isset($test)) { echo '$test 已经set', '<br/> ...

  3. python 字典的函数

    clear(),清空 注意单纯的赋值就相当于c语言中引用,只事额外起了一个别名,所以他们指向相同的地址, 所以令c={},只是另外开辟了一个新的空间让c为空,并没有改变之前的空间,所以{}与clear ...

  4. Servlet.init() for servlet springMvc

  5. java21

    1:字符流(掌握) (1)字节流操作中文数据不是特别的方便,所以就出现了转换流. 转换流的作用就是把字节流转换字符流来使用. (2)转换流其实是一个字符流 字符流 = 字节流 + 编码表 (3)编码表 ...

  6. Git配置姓名和邮箱问题

    今天在安装Git for windows完成后,配置姓名和邮箱.按照廖雪峰老师的步骤,在开始菜单里找到"Git"->"Git Bash",单击后并没有跳出 ...

  7. 51. N-Queens

    题目: The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two que ...

  8. Android笔记:多线程

    定义线程的两个方法: 1. class MyThread extends Thread { public void run() {// 处理具体的逻辑 } } new MyThread().start ...

  9. Windows下安装Redis

    1.首先,Redis官方是支持Linux系统的,我这里不多说,需要的可以参考:http://www.oschina.net/question/12_18065/ 2.Windows 64位下载地址:h ...

  10. C++ 基础知识复习(一)

    数据类型,常量与变量部分:(发现有些点竟然这么多年第一次发现) C++基本数据类型有哪些: 答:整型,浮点型,void型. 注:其他各种数据类型均是这三种类型的扩充,另外void类型在实际程序中经常用 ...