中南大学第一届长沙地区程序设计邀请赛 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 ...
随机推荐
- Hibernate3回顾-1-部署
web备份版本,详见doc版本. 一.背景(部署简单回顾) 我们知道,一个Hibernate快速上手的简单流程是这样. 1引入对应jar包. 中间涉及log4的jar包和配置,略. 2 实体类 pac ...
- HackerRank "Favorite sequence"
Typical topological sorting problem .. why is it 'difficult'? #include <iostream> #include < ...
- PHP替换,只替换匹配到的第一个
function str_replace_limit($search, $replace, $subject, $limit=-1) { if (is_array($search)) { foreac ...
- 328. Odd Even Linked List
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...
- 在DataList、Repeater的HeaderTemplate和FooterTemplate模板中寻找控件FindControl
[程序代码] <asp:Repeater ID="Repeater1" runat="server"> <HeaderTemplate> ...
- CE_现金银行对账单的手工导入和调节(案例)
2014-07-14 Created By BaoXinjian
- BestCoder Round #86 部分题解
Price List 题意: 有n件商品,每天只能买一件,并且会记录账本,问有多少次一定记多了? 题解: 就是求和,最后如果大于和就输出1,否则0. 代码: #include <bits/std ...
- 用socket操作redis
代码: $cmd = "*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$3\r\nbar\r\n"; // set foo bar $socket = socke ...
- CLR如何调用虚方法、属性和事件
方法代表在类型或类型的实例上执行某些操作的代码.在类型上执行操作,称为静态方法:在类型的实例上执行操作,称为非静态方法.任何方法都有一个名称.一个签名和一个返回值(可以是void). CLR允许一个类 ...
- 防篡改php文件校验程序
<?php /** * 校验线上源文件是否和本地的一致 * User: Administrator * Date: 2015/11/26 * Time: 9:30 */ include_once ...