首页 > 其他分享 >双调排序

双调排序

时间:2023-08-08 21:58:18浏览次数:25  
标签:include return 双调 int 排序 fl BitonicSort getchar

以后再说。

#include <cstdio>
#include <algorithm>
using namespace std;
int read(){
	char c=getchar();int x=0;
	while(c<48||c>57) c=getchar();
	do x=(x<<1)+(x<<3)+(c^48),c=getchar();
	while(c>=48&&c<=57);
	return x;
}
const int INF=0x3f3f3f3f;
int a[1<<20];
void ConditionalSwap(int x,int y,bool fl){
	if(fl) swap(x,y);
	if(a[x]>a[y]) swap(a[x],a[y]);
}
void BitonicChange(int l,int r,bool fl){ //bitonic->monotonic
	if(l+1==r) return;
	int p=(r-l)>>1;
	for(int i=l;i<l+p;++i)
		ConditionalSwap(i,i+p,fl);
	BitonicChange(l,l+p,fl);
	BitonicChange(r-p,r,fl);
}
void BitonicSort(int l,int r,bool fl){ //random->bitonic->monotonic
	if(l+1==r) return;
	int p=(r-l)>>1;
	BitonicSort(l,l+p,fl^1);
	BitonicSort(r-p,r,fl);
	BitonicChange(l,r,fl);
}
int main(){
	int len=read();
	for(int i=0;i<len;++i) a[i]=read();
	int n=1;
	while(n<len) n<<=1;
	for(int i=len;i<n;++i) a[i]=INF;
	BitonicSort(0,n,0);
	for(int i=0;i<len;++i) printf("%d ",a[i]);
	putchar('\n');
	return 0;
}

标签:include,return,双调,int,排序,fl,BitonicSort,getchar
From: https://www.cnblogs.com/yyyyxh/p/bitonic_sort.html

相关文章

  • LeetCode 16. 3Sum Closest 双指针+排序
    Givenanintegerarraynumsoflengthnandanintegertarget,findthreeintegersinnumssuchthatthesumisclosesttotarget.Returnthesumofthethreeintegers.Youmayassumethateachinputwouldhaveexactlyonesolution.Solution先将原数组排序,然......
  • Ps让图层以字母序排序
    https://github.com/matthewkimber/ps-layer-sort下载这个脚本,将其放置到Adobe\AdobePhotoshop2023\Presets\Scripts目录下。然后在Ps中点击文件->脚本->ps-layer-sort......
  • DataFrame排序,单列排序,多列排序
    importpandasaspd#创建示例DataFramedata={'Name':['Alice','Bob','Charlie'],'Age':[30,25,35],'Salary':[50000,60000,45000]}df=pd.DataFrame(data)#按照'Age'......
  • kettle案例六数据表关联--排序记录-记录集连接-过滤记录
    如果我们清洗的数据是多个维度的,那么很有可能对数据进行关联得到一张最终表进行分析。比如回答集合的数据里有如下字段idoptionIduser包含了谁回答了哪个问题,选项是什么。选项集合的数据里有如下字段idquestionoption我们最终希望得到的数据集合是idquestionop......
  • JS实现根据数组对象的某一属性排序
    一、冒泡排序(先了解冒泡排序机制)以从小到大排序为例,冒泡排序的原理就是通过两层循环把数组中两两相邻的元素进行比较,是的大的元素放到后边,元素交换位置,从而一步步的交换元素的位置,使得最大的元素放到数组的末尾,这样内部的循环就进行了一轮,再根据外部的循环依次再把次大一点的元素......
  • 冒泡排序(简单概叙)
    #include<stdio.h>voidbubble_sort(inta[],intc){inte=0;for(e=0;e<c-1;e+=1){inth=0;intf=0;for(f=0;f<c-1-e;f+=1){if(a[f]>a[f+1])//如果换成变量e会怎么样?......
  • 软件测试|MySQL ORDER BY详解:排序查询的利器
    简介在数据库中,我们经常需要对查询结果进行排序,以便更好地展示数据或满足特定的业务需求。MySQL提供了ORDERBY子句,使我们能够轻松地对查询结果进行排序。本文将详细介绍MySQLORDERBY的用法和示例,帮助大家更好地理解和应用这一功能。基本语法在MySQL中,ORDERBY子句用于对查询结果......
  • 【数据结构】排序1 基本概念
    0.概述:重难点:堆排序,快速排序,归并排序深入掌握各种排序算法,以选择题考察不同算法之间的对比常用排序算法的代码要会写,并且能根据给定序列选择最合适的排序算法1.排序的基本概念(简单了解即可)......
  • c#集合去重&排序常用方法
     list与数组转Hashset&SortedSet//创建hashset去重varhashSet=newHashSet<int>(){1,1,2,2};Console.WriteLine("HashSet:"+String.Join(",",hashSet));//HashSet:1,2//创建list包含重复元素varints=newList<int>{1,1,3,3,2,2};//创建数组转......
  • 拓扑排序
    拓扑序列是顶点活动网中将活动按照发生的先后顺序进行的一种排列。 拓扑排序,是对一个有向无环图(DirectedAcyclicGraph简称DAG)G进行拓扑排序,是将G中所有顶点排成一个线性序列,使得图中任意一对顶点u和v,若边(u,v)∈E(G),则u在线性序列中出现在v之前。通常,这样的线性序列称为满......