中南大学第一届长沙地区程序设计邀请赛 New Sorting Algorithm

本文探讨了一种新型排序算法,用于计算特定序列在排序过程中所需的步骤数。通过实例展示了算法的工作原理,并提供了输入输出样例,旨在帮助读者理解算法实现和应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 = 100008 ;

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 = 0 ;
   int R ,i , j ,ok = 0 ;
   for(i = node[1].id ; i < N ; i++){
      if(num[i] > num[i+1]){
         ok = 1 ;
         break ;
      }
   }
   if(ok)
      R = N ;
   else
      R = node[1].id - 1 ;
   for(i = 1 ; i <= R ; i++){
       int n = num[i] ;
       find_Left(n) ;
       find_Left(n-1) ;
       sum += (LL)(Left[n] - n + 1) ;
       if(Left[n-1] != Left[n])
          Left[n-1] = Left[n] ;
   }
   return sum ;
}

int main(){
    int T ;
    cin>>T ;
    while(T--){
         scanf("%d",&N) ;
         for(int i = 1 ; i <= N ; i++){
            Left[i] = i ;
            node[i].id = i ;
            scanf("%d",&node[i].num) ;
         }
         sort(node+1 ,node+1+N) ;
         for(int i = 1 ; i <= N ; i++)
            num[node[i].id] = i ;
       /*  for(int i = 1 ; i <= N ; i++)
            printf("%d  ",num[i]) ;
         puts("") ;*/
         cout<<gao()<<endl ;
    }
    return 0 ;
}

 

转载于:https://www.cnblogs.com/liyangtianmen/p/3473699.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值