首页 > 其他分享 >Min Max Sort

Min Max Sort

时间:2023-01-26 23:33:51浏览次数:46  
标签:Sort elements Min Max pos permutation operation becomes first

题目链接

题目描述:

You are given a permutation \(p\) of length \(n\) (a permutation of length \(n\) is an array of length \(n\) in which each integer from \(1\) to \(n\) occurs exactly once).

You can perform the following operation any number of times (possibly zero):

  1. choose two different elements \(x\) and \(y\) and erase them from the permutation;
  2. insert the minimum of \(x\) and \(y\) into the permutation in such a way that it becomes the first element;
  3. insert the maximum of \(x\) and \(y\) into the permutation in such a way that it becomes the last element.

For example, if \(p=[1,5,4,2,3]\) and we want to apply the operation to the elements \(3\) and \(5\)
, then after the first step of the operation, the permutation becomes \(p=[1,4,2]\); and after we insert the elements, it becomes \(p=[3,1,4,2,5]\).

Your task is to calculate the minimum number of operations described above to sort the permutation \(p\) in ascending order (i. e. transform \(p\) so that \(p1<p2<⋯<pn\)).

输入描述:

The first line contains a single integer \(t (1≤t≤10^4)\) — the number of test cases.

The first line of the test case contains a single integer \(n (1≤n≤2⋅10^5)\) — the number of elements in the permutation.

The second line of the test case contains \(n\) distinct integers from \(1\) to \(n\) — the given permutation \(p\).

The sum of \(n\) over all test cases doesn't exceed \(2⋅10^5\).

输出描述:

For each test case, output a single integer — the minimum number of operations described above to sort the array \(p\) in ascending order.

样例:

input:

4
5
1 5 4 2 3
3
1 2 3
4
2 1 4 3
6
5 2 4 1 6 3

output:

2
0
1
3

Note:

In the first example, you can proceed as follows:

  1. in the permutation \(p=[1,5,4,2,3]\), let's choose the elements \(4\) and \(2\), then, after applying the operation, the permutation becomes \(p=[2,1,5,3,4]\);
  2. in the permutation \(p=[2,1,5,3,4]\), let's choose the elements \(1\) and \(5\), then, after applying operation, the permutation becomes \(p=[1,2,3,4,5]\).

AC代码:

#include <bits/stdc++.h>

using namespace std;

// 对于n个数,可以从1 ~ n, 2 ~ n - 1, 3 ~ n - 2...这样子操作
// 如果原数组中两个数的位置跟排列中的不一样就说明两个数需要操作
// 如果两个数需要操作,那么由于操作会把两个数放在最左边和最右边
// 所以在排列中这两个数左右两边的所有数也需要操作
// 所以可以从排列中间的数开始,判断原数组两个数的相对位置
// 依次往两边判断两个数是否需要操作
// 最后l和r所指的那两个数就是最先需要操作的那两个数,输出l即可
void solve()
{
	int n;
	scanf("%d", &n);

	vector<int> pos(n + 1); // 存每个数的位置

	for(int i = 1; i <= n; i ++)
	{
		int x;
		scanf("%d", &x);

		pos[x] = i; // x 的位置在 i
	}

	// 不用考虑奇偶的情况
	int l = (n + 1) / 2, r = (n + 2) / 2; 

	while(l > 0 && (l == r || (pos[l] < pos[l + 1] && pos[r - 1] < pos[r])))
	{
		l --;
		r ++;
	}

	printf("%d\n", l);
}

int main()
{
	int T;
	scanf("%d", &T);

	while(T --)
		solve();

	return 0;
}

标签:Sort,elements,Min,Max,pos,permutation,operation,becomes,first
From: https://www.cnblogs.com/KSzsh/p/17068263.html

相关文章

  • Day17 - mini-Web框架
    1.web框架概述web框架和web服务器的关系介绍前面已经学习过web服务器,我们知道web服务器主要是接收用户的http请求,根据用户的请求返回不同的资源数据,但是之前我们开......
  • mac为终端Terminal设置代理访问
    这里我的ssport:1086,httpport:1087以下配置基于当前端口更换所有终端应用的代理如果没有设置代理的话,连github这个地址都上不去。临时设置exporthtt......
  • minecraft mods descrip
    1.【AdvancedFinders】矿物探测器mod显示玩家周围附近矿石的方向(指针显示水平面上可到达的矿石)探测地下深部矿脉(箭头显示最近矿脉的方向(上/下))发现大型矿床时发出信......
  • 腾讯出品小程序自动化测试框架【Minium】系列(三)元素定位详解
    写在前面昨天转发这篇文章时,看到群里有朋友这样说:这么卷吗?这个框架官方已经不维护了。姑且不说卷不卷的问题,要是能卷明白,别说还真不错;不维护又怎样?我想学习,想会,分享给......
  • docker-compose安装minio
    minio:RELEASE.2022-09-07T22-25-02Z创建文件vidocker-compose.yml脚本内容如下:version:'3'services:minio:image:minio/minio:RELEASE.2022-09-07T22-......
  • 迁移学习(DDC)《Deep Domain Confusion: Maximizing for Domain Invariance》
    论文信息 论文标题:DeepDomainConfusion:MaximizingforDomainInvariance论文作者:EricTzeng,JudyHoffman,NingZhang,KateSaenko,TrevorDarrell论文来源:ar......
  • jQuery练习4京东商品详情页面(minicart显示隐藏)
    视频//7.鼠标移入移出切换显示迷你购物车functionminicart(){$('#minicart').hover(function(){$(this).addClass('minicart').children('div').show()......
  • 腾讯出品小程序自动化测试框架【Minium】系列(二)项目配置及测试套件使用说明
    一、写在前面真的人这一散漫惯了,收心就很难了,上午把小程序开发环境启动后,在QQ游戏里,杀了三把象棋,5把2D桌球,一上午没了,还是没法心静下来去学点东西。那就老样子,逼着自己开......
  • C语言库函数qsort的使用
    前言qsort是C语言的库函数,使用前需包含头文件#include<stdlib.h>,函数原型是voidqsort(void*base,size_tnum,size_twidth,int(__cdecl*compare)(constvoid*......
  • LTE和Wimax异构网络垂直切换matlab仿真
    1.算法描述随着通信产业的迅猛发展,用户对通信有了更高的期望,不仅要求有稳定的语音通信,而且要求能够进行数据和多媒体的通信,这使得异构网络之间的融合成为一个非常重要......