中南大学第一届长沙地区程序设计邀请赛 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 ...
随机推荐
- PO、VO、BO、DTO
J2EE开发中大量的专业缩略语很是让人迷惑,尤其是跟一些高手讨论问题的时候,三分钟就被人家满口的专业术语喷晕了,PO VO BO DTO POJO DAO,一大堆的就来了(听过老罗对这种现象的批判的朋 ...
- EV电池指标及特点
在电池的大家族中,蓄电池的种类是最多的,共同的特点是可以经历多次充电.放电循环,反复使用,这也正是蓄电池作为电动汽车动力源的基础.当然,并不是所有的蓄电池都适合应用于电动汽车,从全球新能源汽车的发展来 ...
- 实现手电筒Flash Light 关键代码
实现手电筒Flash Light 关键代码 实现Flash的逻辑 view.setOnClickListener(new OnClickListener() { @Override public vo ...
- SQL SERVER 2008安装时出现不能在控件上调用 Invoke 或 BeginInvoke错误 解决方法
或者 SQL SERVER 2008安装时要求重启,但重启后仍要求重启.都可以使用此方法. 注册表的 "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet ...
- <limits.h>和<float.h>
头文件<limits.h>中定义了用于表示整类型大小的常量.以下所列的值是可接受的最小值,实际系统中可能有更大的值. CHAR_BIT char类型的位数 CHAR_MAX UCHAR_M ...
- C#之线程和并发
建议大家对C#撑握的不错的时候,可以去看一些开源项目.走技术这条路,就要耐得住寂寞(群里双休日说要让群主找妹子进群的人必须反思),练好内功.不撑握C#高级知识点,别想看懂优秀的开源项目,更别指望吸收其 ...
- GL_GL系列 - 预算管理分析(案例)
2014-07-09 Created By BaoXinjian
- OAF_MDS系列1_OAF页面元数据结构MDS的解析(概念)
2014-06-06 Created By BaoXinjian
- U8Bom查询
select temp.*,Inventory.cInvCode,Inventory.cInvName,Inventory.cInvStd from ( select b.InvCode as 'PI ...
- Guava 9-I/O
字节流和字符流 Guava使用术语”流” 来表示可关闭的,并且在底层资源中有位置状态的I/O数据流.术语”字节流”指的是InputStream或OutputStream,”字符流”指的是Reader ...