中南大学第一届长沙地区程序设计邀请赛 New Sorting Algorithm
1352: New Sorting Algorithm
Time Limit: 1 Sec Memory Limit: 128 MB
Description
We are trying to use a new sorting algorithm to sort a sequence with distinct integers.
This algorithm will be end if the sequence is already in increasing order from left to right. Or for each step, suppose x is the leftmost integer, and y is the largest integer which is smaller than x in this sequence, we will move x to the right of y if y exists, otherwise we will move x to the right of the rightmost integer.
So, how many steps will we use to sort a specific sequence with distinct integers by this new sorting algorithm?
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
Input
The first line has an integer T, means there are T test cases.
For each test case, there is one integer N (2 <= N <= 105) in the first line, means the sequence has N distinct integers. Then there are N integers in the next line describing this sequence. Every integer of this sequence is in range [0, 109].
The size of the input file will not exceed 5MB.
Output
For each test case, print an integer in one line, indicates the number of steps we will use to sort this sequence by the new soring algorithm.
Sample Input
3
3
3 7 8
4
7 1 5 2
5
5 4 3 2 1
Sample Output
0
7
10
HINT
Source
对于节点X 。f(x)=i+1 。 {X-1 ,X -2 ,...X-i}均在X左端出现。
1、1的右边没有升序排列好 。
Ans= f(1) + f(2) +.....f(N) ;
2 、1的右边升序排列好
Ans= f(num[1]) + f(num[2]) +.....f(num[1所在位置-1]) ;
#include <iostream>
#include <string>
#include <string.h>
#include <map>
#include <stdio.h>
#include <algorithm>
#include <queue>
#include <vector>
#include <math.h>
#include <set>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
typedef long long LL ; using namespace std; const int Max_N = ; struct Node{
int id ;
int num ;
friend bool operator < (const Node A ,const Node B){
return A.num < B.num ;
}
}; Node node[Max_N] ;
int Left[Max_N] ;
int num[Max_N] ;
int N ; int find_Left(int x){
if(Left[x] == x)
return x ;
else
return Left[x] = find_Left(Left[x]) ;
} LL gao(){
LL sum = ;
int R ,i , j ,ok = ;
for(i = node[].id ; i < N ; i++){
if(num[i] > num[i+]){
ok = ;
break ;
}
}
if(ok)
R = N ;
else
R = node[].id - ;
for(i = ; i <= R ; i++){
int n = num[i] ;
find_Left(n) ;
find_Left(n-) ;
sum += (LL)(Left[n] - n + ) ;
if(Left[n-] != Left[n])
Left[n-] = Left[n] ;
}
return sum ;
} int main(){
int T ;
cin>>T ;
while(T--){
scanf("%d",&N) ;
for(int i = ; i <= N ; i++){
Left[i] = i ;
node[i].id = i ;
scanf("%d",&node[i].num) ;
}
sort(node+ ,node++N) ;
for(int i = ; i <= N ; i++)
num[node[i].id] = i ;
/* for(int i = 1 ; i <= N ; i++)
printf("%d ",num[i]) ;
puts("") ;*/
cout<<gao()<<endl ;
}
return ;
}
中南大学第一届长沙地区程序设计邀请赛 New Sorting Algorithm的更多相关文章
- 中南大学第一届长沙地区程序设计邀请赛 To Add Which?
1350: To Add Which? Time Limit: 1 Sec Memory Limit: 128 MB Description There is an integer sequence ...
- Minieye杯第十五届华中科技大学程序设计邀请赛现场同步赛 I Matrix Again
Minieye杯第十五届华中科技大学程序设计邀请赛现场同步赛 I Matrix Again https://ac.nowcoder.com/acm/contest/700/I 时间限制:C/C++ 1 ...
- Minieye杯第十五届华中科技大学程序设计邀请赛网络赛D Grid(简单构造)
链接:https://ac.nowcoder.com/acm/contest/560/D来源:牛客网 题目描述 Give you a rectangular gird which is h cells ...
- H-Modify Minieye杯第十五届华中科技大学程序设计邀请赛现场赛
题面见 https://ac.nowcoder.com/acm/contest/700#question 题目大意是有n个单词,有k条替换规则(单向替换),每个单词会有一个元音度(单词里元音的个数)和 ...
- Minieye杯第十五届华中科技大学程序设计邀请赛网络赛 部分题目
链接:https://pan.baidu.com/s/12gSzPHEgSNbT5Dl2QqDNpA 提取码:fw39 复制这段内容后打开百度网盘手机App,操作更方便哦 D Grid #inc ...
- 2014嘉杰信息杯ACM/ICPC湖南程序设计邀请赛暨第六届湘潭市程序设计竞赛
比赛链接: http://202.197.224.59/OnlineJudge2/index.php/Contest/problems/contest_id/36 题目来源: 2014嘉杰信息杯ACM ...
- 江西财经大学第一届程序设计竞赛 G题 小Q的口袋校园
链接:https://www.nowcoder.com/acm/contest/115/G来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536 ...
- 电信学院第一届新生程序设计竞赛题解及std
首先非常感谢各位同学的参加,还有出题验题同学的辛勤付出 昨天想偷懒就是不想再把我C++11的style改没了,大家看不懂的可以百度一下哦,懒得再写gcc了,毕竟代码是通的 //代表的是行注释,所以那个 ...
- “九韶杯”河科院程序设计协会第一届程序设计竞赛 D数列重组 next_permutation
"九韶杯"河科院程序设计协会第一届程序设计竞赛 D数列重组 next_permutation 题目 原题链接: https://ac.nowcoder.com/acm/conte ...
随机推荐
- [oracle] ORACLE_HOME_LISTNER is not SET, unable to auto-start Oracle Net Listener
安装好Oracle数据库后: 执行 dbstart和dbshut会提示: [oracle@oracle11g ~]$ dbstartORACLE_HOME_LISTNER is not SET, un ...
- Python(迭代器 生成器 装饰器 递归 斐波那契数列)
1.迭代器 迭代器是访问集合元素的一种方式.迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束.迭代器只能往前不会后退,不过这也没什么,因为人们很少在迭代途中往后退.另外,迭代器的一大优 ...
- ubuntu 安装 VISUAL STUDIO CODE
1.下载VISUAL STUDIO CODE .DEB/.RPM 2. # For .deb sudo dpkg -i <file>.deb # For .rpm (Fedora and ...
- 【jdbcTemplate】使用jdbcTemplate查询的三种回调
用于查询的回调接口定义主要有以下三种: org.springframework.jdbc.core.ResultSetExtractor. 基本上属于JdbcTemplate内部使用的Callbac ...
- Maven的几个核心概念
POM (Project Object Model) 一个项目所有的配置都放置在 POM 文件中:定义项目的类型.名字,管理依赖关系,定制插件的行为等等.比如说,你可以配置 compiler 插件让它 ...
- Windows2012修改光驱盘符
1.输入diskmgmt.msc打开磁盘管理器 2.找到需要修改的盘符,右键点击修改盘符
- 第二次正式java web开发项目的总结(回收站恢复)
都说互联网行业加班很是厉害,记得前不久网上还晒出了几个大城市互联网行业的加班排名调查,但是我们公司,或者说我们项目组倒是非常的例外,进公司也差不多半年了,才仅仅上个月有一个周六加过一天班而已. 不过好 ...
- title与h1标签的区别和联系
很多新站长在网站SEO过程中,会认为把H1等同于Title. 其实两是有区别和联系的,两者不能划等号.下面主要从文章和页面角度分析title和H1. H1等同于title吗? H1不等于title.H ...
- Hadoop学习2--Linux准备及环境准备
1.环境安装: 虚拟机:VMware Player 系统:Ubuntu12 注意事项:注意位数,包括系统,java,Hadoop 2.切换账号 当前登录账号是自己的账号,如果想切换到root,且是第一 ...
- Tomcat插件与Jetty插件在MyEclipse中的配置
-Djetty.port=8101 jetty:run tomcat6:run <plugin> <groupId>org.apache.tomcat.maven</gr ...