中南大学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 -> ...
随机推荐
- 联想Thinkpad笔记本自带win10改win7图文教程
一.准备工作: 1.备份转移硬盘所有文件 2.改装win7将删除所有分区,要恢复预装的win10系统需到售后 3.4G空间以上U盘,制作U盘PE启动盘 4.操作系统:联想Lenovo笔记本专用GHOS ...
- 构建高性能web之路------mysql读写分离实战(转)
一个完整的mysql读写分离环境包括以下几个部分: 应用程序client database proxy database集群 在本次实战中,应用程序client基于c3p0连接后端的database ...
- C# Httpclient客户端操作
原文地址:https://www.cnblogs.com/Xujg/p/4113387.html HttpClient 当前主流用法,异步请求,自.NET4.5开始可从Nuget包管理中获取. usi ...
- JAVA中使用HTTP 1.1提高基于AXIS 1.4的web service的性能
HTTP 1.1会在第一次连接的时候进行认证, 而在一定时间内保持连接而不用重新验证. 一般情形下,每个web service请求都会在web service服务端验证, 而验证会消耗很多时间, 因此 ...
- Sublime text —— 自定义Color theme
网上下载,XXX.tmTheme 样式,让后放置于 C:\Users\{用户名}\AppData\Roaming\Sublime Text 2\Packages\Color Scheme - Defa ...
- XGB 调参基本方法
- xgboost 基本方法和默认参数 - 实战经验中调参方法 - 基于实例具体分析 在训练过程中主要用到两个方法:xgboost.train()和xgboost.cv(). xgboost.trai ...
- JAVA中Set集合--HashSet的使用
一.使用HashSet添加一个String类型的值: public static void hashSet1(){ HashSet<String> hashSet = new HashSe ...
- django官方文档--对静态文件的管理
一.入门级理解: 在django中对静态文件的管理和模板(template)的思路是一样的.在模板的管理中django是把app用到 到的模板都保存到app目录下的templates子目录中. 静态文 ...
- iphone5刷机教程
如果不想麻烦可以在越狱之后添加源,cydia.china3gpp.com打ios7的补丁就可以了 机器为iphone5 美国sprint有锁版 1. 首先备份需要的程序和数据(把各种缓存的影片删掉再备 ...
- cnetos7最小化安装ifconfig命令找不到怎么办
我们在安装完centos7最小化系统安装完成后,执行ifconfig命令会报命令未找到.实际上在centos7上,使用“ip addr”和“ip link”命令来查找网卡详情.ifconfig命令已经 ...