中南大学第一届长沙地区程序设计邀请赛 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 ...
随机推荐
- Ext JS 5的声明式事件监听
在前文<在Ext JS 5使用ViewControllers>中,简单的介绍了Ext JS 5的一项重要改进——声明式事件监听.在本文,将深度探讨如何使用声明式事件监听啦简化应用程序的视图 ...
- POJ #1141 - Brackets Sequence - TODO: POJ website issue
A bottom-up DP. To be honest, it is not easy to relate DP to this problem. Maybe, all "most&quo ...
- android学习笔记一——简介
android 是由Andy Rubin创立的一个手机操作系统,后被google收购. google希望同各方共同建立一个标准化.开放式的移动电话软件平台,从而在移动产业内形成了一个开放式的操作平台. ...
- 编译在arm板上使用的sqlite3的静动态库
采用的是sqlite-autoconf-3080002.tar.gz 解压 tar xvf sqlite-autoconf-3080002.tar.gz 进入 cd sqlite-autoconf-3 ...
- ios8 ios7 tableview cell 分割线左对齐
ios8中左对齐代码 //加入如下代码 -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cel ...
- Linux启动过程详解(转)
启动第一步--加载BIOS当你打开计算机电源,计算机会首先加载BIOS信息,BIOS信息是如此的重要,以至于计算机必须在最开始就找到它.这是因为BIOS中包含了CPU的相关信息.设备启动顺序信息.硬盘 ...
- Hadoop学习9--动态增加datanode
http://www.cnblogs.com/ggjucheng/archive/2012/04/18/2454689.html
- Hadoop学习2--Linux准备及环境准备
1.环境安装: 虚拟机:VMware Player 系统:Ubuntu12 注意事项:注意位数,包括系统,java,Hadoop 2.切换账号 当前登录账号是自己的账号,如果想切换到root,且是第一 ...
- [OpenGL ES 03]3D变换:模型,视图,投影与Viewport
[OpenGL ES 03]3D变换:模型,视图,投影与Viewport 罗朝辉 (http://blog.csdn.net/kesalin) 本文遵循“署名-非商业用途-保持一致”创作公用协议 系列 ...
- Windows XP下安装和配置Apache2.2.22服务器+PHP5+Mysql5
原文:http://www.chinaz.com/web/2012/0516/252021.shtml 随着PHP网站的流行,国内越来越多的站长使用php开发网站或者使用相关的php开源网站(例如:D ...