HDU 3743 Frosh Week (线段树+离散化)
题目链接: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 (线段树+离散化)的更多相关文章
- HDU - 1542 扫描线入门+线段树离散化
扫描线算法+线段树维护简介: 像这种求面积的并集的题目,就适合用扫描线算法解决,具体来说就是这样 类似这种给出点的矩形的对角的点的坐标,然后求出所有矩形面积的交集的问题,可以采用扫描线算法解决.图如下 ...
- 【HDU】5249-KPI(线段树+离散化)
好久没写线段树都不知道怎么写了... 很easy的线段树二分问题 #include<cstdio> #include<set> #include<queue> #i ...
- HDU 4288 Coder 【线段树+离线处理+离散化】
题意略. 离线处理,离散化.然后就是简单的线段树了.需要根据mod 5的值来维护.具体看代码了. /* 线段树+离散化+离线处理 */ #include <cstdio> #include ...
- HDU5124:lines(线段树+离散化)或(离散化思想)
http://acm.hdu.edu.cn/showproblem.php?pid=5124 Problem Description John has several lines. The lines ...
- POJ 2528 Mayor's posters(线段树+离散化)
Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...
- poj 2528 Mayor's posters(线段树+离散化)
/* poj 2528 Mayor's posters 线段树 + 离散化 离散化的理解: 给你一系列的正整数, 例如 1, 4 , 100, 1000000000, 如果利用线段树求解的话,很明显 ...
- [poj2528] Mayor's posters (线段树+离散化)
线段树 + 离散化 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayor ...
- [UESTC1059]秋实大哥与小朋友(线段树, 离散化)
题目链接:http://acm.uestc.edu.cn/#/problem/show/1059 普通线段树+离散化,关键是……离散化后建树和查询都要按照基本法!!!RE了不知道多少次………………我真 ...
- poj 2528 Mayor's posters 线段树+离散化技巧
poj 2528 Mayor's posters 题目链接: http://poj.org/problem?id=2528 思路: 线段树+离散化技巧(这里的离散化需要注意一下啊,题目数据弱看不出来) ...
- hdu 5700区间交(线段树)
区间交 Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submiss ...
随机推荐
- CSS3实现阴阳鱼
直接上代码: <!doctype html> <html> <head> <meta charset="utf-8" /> < ...
- C# 中的委托和事件(转)
引言 委托 和 事件在 .Net Framework中的应用非常广泛,然而,较好地理解委托和事件对很多接触C#时间不长的人来说并不容易.它们就像是一道槛儿,过了这个槛的人,觉得真是太容易了,而没有过去 ...
- 15个JavaScript本地存储技术的函数库和工具
当构建更复杂的JavaScript应用程序运行在用户的浏览器是非常有用的,它可以在浏览器中存储信息,这样的信息可以被共享在不同的页面,浏览会话. 在最近的过去,这将有可能只被cookies文本文件保存 ...
- URL详解
浏览器因特网资源:URL是浏览器寻找信息时所需的资源位置,通过URL,应用程序才能找到并使用共享因特网上大量的数据资源. 大部分URL都遵循一种标准的格式: ①HTTP协议(http://或者http ...
- [Centos] Centos 7笔记
命令行与图形界面 在X-Window图形操作界面中按“Alt+Ctrl+功能键Fnn=1~6”就可以进入Console字符操作界面.这就意味着你可以同时拥有X-Window加上6个Console字 ...
- jq的$()里面 一定要是字符串类型!!!!!!!!!!!!!!!!!!!!!!!!
var s = "[value="+uid+"]"; $(s).attr("checked",'true');
- $_SERVER["SCRIPT_NAME"]、$_SERVER["PHP_SELF"]、$_SERVER["QUERY_STRING"]、$_SERVER["REQUEST_URI"]
1.$_SERVER["SCRIPT_NAME"] 说明:包含当前脚本的路径 2.$_SERVER["PHP_SELF"] 说明:当前正在执行脚本的文件名 3. ...
- 利用 autoconf 和 automake 生成 Makefile 文件
一.相关概念的介绍 什么是 Makefile?怎么书写 Makefile?竟然有工具可以自动生成 Makefile?怎么生成啊?开始的时候,我有这么多疑问,所以,必须得先把基本的概念搞个清楚. 1.M ...
- ngnix 配置CI框架 与 CI的简单使用
ngnix 支持 CI框架1.修改config.php 参考网址:https://www.chenyudong.com/archives/codeigniter-in-nginx-and-url-re ...
- 【Python基础学习六】函数
1.创建函数 Python中函数的关键字def来定义. def fibs(num): f=[0,1] for i in range(1,num): f.append(f[-1]+f[-2]) retu ...