中南大学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 -> ...
随机推荐
- CentOS 7 systemd的坑
一.概述 在从 CentOS 6 迁移到 CentOS 7 的过程中,可能有一些地方需要调整,最显著的地方莫过于 systemd 带来的改变,不同的管理服务的方式,不同的日志方式,设置时区,时间等等. ...
- VS与SQL资源
经常在网上看到有同学费尽心思的找SQL server数据库各版本的下载地址,看到别人的求助贴就不自觉的想去帮助他们,但是一个一个去帮助又不太现实,毕竟个人精力有限,既然大家有需求,那么笔者就本着乐于分 ...
- 【Oracle】Oracle索引
在关系数据库中,索引是一种与表有关的数据库结构,它可以使对应于表的SQL语句执行得更快.索引的作用相当于图书的目录,可以根据目录中的页码快速找到所需的内容. 对于数据库来说,索引是一个必选项,但对于现 ...
- GitHub Desktop 代码库管理工具
1.GitHub Desktop 简介 GitHub Desktop 是用于 GitHub 项目版本控制软件. 官网下载地址 GitHub Desktop 其它下载地址 GitHub Desktop ...
- Xcode 各版本简介
1.Xcode 验证 在终端输入 spctl 命令,并带上安装的 Xcode 的路径 $ spctl --assess --verbose /Applications/Xcode.app 之后会看到类 ...
- vc2010 属性值无效 灾难性故障 解决方法
原文链接: http://blog.csdn.net/enterlly/article/details/8739281 说明: 我遇到这个问题是这样的,在为某个类添加消息时出现的.因为该类不在此工程的 ...
- 近期对招聘Android开发者的一些思考
公司要招聘Android开发者,故面试了大概十来个人.由于是小公司,所以来的人大多是90后,比較年轻.90后大概二十三四岁吧,从简历上看都写了一到两年的工作经验. 也由于是小公司,所以对工作经验这些没 ...
- 题目要求:将a,b两个数的值进行交换,并且不使用任何的中间变量。
a = a+b; b = a-b; a = a-b;
- Map和JSON的互相转换
JSON-Lib方式 /** * 函数注释:parseJSON2Map()<br> * 用途:该方法用于json数据转换为<Map<String, Object> ...
- springboot 多环境配置yml或properties
https://www.cnblogs.com/mr-yang-localhost/p/8971327.html springboot 多环境配置 https://blog.csdn.net/li ...