中南大学oj:1352: New Sorting Algorithm
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1352
题意:就是要将7 1 5 2这样的序列变成1 2 5 7最少需要多少步?给出变的规律,每次把最前面的那个数移动到比它次小的数的后面,要是它后面没有比它次小的数,就移动到最后,问最少需要多少步?
For example, we will use 7 steps to sort the sequence 7 1 5 2:
7 1 5 2 --> 1 5 7 2 --> 5 7 2 1 --> 7 2 5 1 --> 2 5 7 1 --> 5 7 1 2 --> 7 1 2 5 --> 1 2 5 7
思路:这个题目给出的数据量很大,所以一步步模拟肯定超时。那么就只能是看各种数字移动到它最终位置是否存在一定规律......经过思考,发现好像没有什么规律....于是这道题目我就放弃了.....然后我的一个队友说,一个数移动到它次小的数后面,那么就可以把这两个数合并成一个数,然后再找再合并.....这样时间复杂度会减少很多。的确是这样的,合并完之后,我们只需要用个并查集来统计有多少个数合并在一起了,然后每次移动的时候,把这些数加上即可.......只是,这样还有一个问题,如何找到比它次小的数?如果次小的数已经被合并了呢?......暴力去查找,明显耗时会很大,可能会超时,那么可以把数据进行离散化,由于所以数字都是独一无二的,那么在找次小的数的时候,我们只需要find(x-1)即可........
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
#define inf 100005
typedef long long ss;
ss father[inf],rank[inf];
ss a[inf],b[inf],n;
queue<ss>q;
queue<ss>q1;
ss erfen(ss sum)
{
ss ll=0,rr=n-1;
while(ll<=rr)
{
ss mid=(ll+rr)/2;
if(b[mid]>sum)
rr=mid-1;
else ll=mid+1;
}
return rr;
}
ss find(ss x)
{
ss i=x,root;
while(x!=father[x])
x=father[x];
root=x;
x=i;
while(x!=father[x])
{
i=father[x];
father[x]=root;
rank[root]+=rank[x];
rank[x]=0;
x=i;
}
return root;
} void liantong(ss x,ss y)
{
father[x]=y;
rank[y]+=rank[x];
rank[x]=0;
}
int main()
{
ss text;
scanf("%lld",&text);
while(text--)
{
scanf("%lld",&n);
while(!q.empty())
q.pop(); while(!q1.empty())
q1.pop();
for(ss i=0;i<n;i++)
{
scanf("%lld",&a[i]);
b[i]=a[i];
father[i]=i;
rank[i]=1;
}
sort(b,b+n);
//int ll=1,rr=n-1;
for(ss i=0;i<n;i++)
{
a[i]=erfen(a[i]);
} for(ss i=0;i<n;i++)
{
q.push(a[i]);
}
ss ans=0;
while(!q.empty())
{
ss x=q.front();
q.pop();
if(!q.empty())
{
if(x==0)
{
ss flg=0;
ss tmp=x;
while(!q.empty())
{
ss y=q.front();
if(tmp<y)
tmp=y;
else flg=1;
q.pop();
q1.push(y);
}
if(flg==1)
{
while(!q1.empty())
{
ss y=q1.front();
q1.pop();
q.push(y);
}
q.push(x);
ans+=rank[x];
}
else break;
}
else
{
ans+=rank[x];
ss y=find(x-1);
if(x!=y)
{
liantong(x,y);
}
}
}
}
printf("%lld\n",ans);
}
return 0;
}
中南大学oj:1352: New Sorting Algorithm的更多相关文章
- csuoj 1352: New Sorting Algorithm
因为每个元素都是移动到比它小1位的元素的后面: 这样的话以后的一定就可以把他们两个打包: 所以用这种方法最多扫一遍就可以了: 但是最小的那个数要不要移动呢? 如果最小的数后面的数都是升序的,那么一直扫 ...
- 中南大学第一届长沙地区程序设计邀请赛 New Sorting Algorithm
1352: New Sorting Algorithm Time Limit: 1 Sec Memory Limit: 128 MB Description We are trying to use ...
- 1306. Sorting Algorithm 2016 12 30
1306. Sorting Algorithm Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description One of the f ...
- Bubble sort of sorting algorithm
Bubble sort,It's a relatively basic algorithm.The core implementation ideas are as follows: 1.Define ...
- 排序算法 (sorting algorithm)之 冒泡排序(bubble sort)
http://www.algolist.net/Algorithms/ https://docs.oracle.com/javase/tutorial/collections/algorithms/ ...
- 九度OJ 1352 和为S的两个数字
题目地址:http://ac.jobdu.com/problem.php?pid=1352 题目描述: 输入一个递增排序的数组和一个数字S,在数组中查找两个数,是的他们的和正好是S,如果有多对数字的和 ...
- Sorting Algorithm
sorting 应该是最容易被考到的东西,自己老是学了背,背了忘.为了方便复习,这里进行总结 1. Bubble Sort 定义:每两个两个比较,每扫完一次,当前扫过的最大值放在了末尾. for i ...
- 排序算法(sorting algorithm) 之 选择排序(selection sort)
https://en.wikipedia.org/wiki/Selection_sort loop1: 4,6,1,3,7 -> 4,6,1,3,7 4,6,1,3,7 -> ,3,7 1 ...
- 排序算法(sorting algorithm)之 插入排序(insertion sort)
https://en.wikipedia.org/wiki/Insertion_sort loop1: 4,6,1,3,7 -> 4,6,1,3,7 loop2: 4,6,1,3,7 -> ...
随机推荐
- 进阶之路(基础篇) - 005 模拟PWM波控制引脚
/********************************* 代码功能:输出PWM波控制引脚 使用函数: 创作时间:2016*10*07 作者邮箱:jikexianfeng@outlook.c ...
- 【C#】Skip和Tack方法实现分页
int pageIndex = SearchModel.PageIndex <= 0 ? 1 : SearchModel.PageIndex; return BatchInfoList.Skip ...
- 浅谈 .NET 中的对象引用、非托管指针和托管指针 理解C#中的闭包
浅谈 .NET 中的对象引用.非托管指针和托管指针 目录 前言 一.对象引用 二.值传递和引用传递 三.初识托管指针和非托管指针 四.非托管指针 1.非托管指针不能指向对象引用 2.类成员指针 五 ...
- mysql_install_db 运行结果
# /usr/local/mysql/scripts/mysql_install_db \ > --defaults-file=/etc/my.cnf \ > --basedir=/usr ...
- Android WebView中软键盘会遮挡输入框相关问题
要想实现这样的软键盘出现的时候会自己主动把输入框的布局顶上去的效果,须要设置输入法的属性,有下面两种设置方式: 一.在java代码中设置例如以下: getWindow().setSo ...
- CentOS扩展库配置
背景:经常用到第三方的库,通过yum命令查询不到.例如:yum search ilbcWarning: No matches found for: ilbc 不要感叹,CentOS没你想象的支持力度那 ...
- FreeSWITCH呼叫参数之sip_cid_type
这个参数定义了呼叫中主叫信息的头字段类型.支持两种类型: 1. rpidRemote-Party-ID头,这是默认的设置.{sip_cid_type=rpid}sofia/default/user@e ...
- 【转载,待整理】初学 springmvc整合shiro
1. shiro认证流程理解 2. 整合过程 http://blog.csdn.net/dawangxiong123/article/details/53020424 http://blog.csdn ...
- Linux中SFTP命令
sftp和ftp是两种协议是不同的,sftp是ssh内含的协议,只要sshd服务器启动了,它就可用,它本身不需要ftp服务器启动. 1.常用登陆方式: 格式:sftp <user>@< ...
- hackerrank-knapsack
https://www.hackerrank.com/challenges/unbounded-knapsack 题目描述: #include <iostream> #include &l ...