首页 > 其他分享 >E. Queue Sort

E. Queue Sort

时间:2024-03-21 22:35:22浏览次数:14  
标签:Sort head prev int next Queue include linknode

这题教训主要是观察和思考

本来上来的想法就是链表加类似插排?但肉眼可见的tle....

#include<iostream>
#include<vector>
#include<algorithm>
#include<math.h>
#include<sstream>
#include<string>
#include<string.h>
#include<iomanip>
#include<stdlib.h>
#include<map>
#include<queue>
#include<limits.h>
#include<climits>
#include<fstream>
#include<stack>
typedef long long ll;
using namespace std;


struct linknode
{
	linknode* next;
	ll val;
	linknode* prev;
	linknode() { val = 0; next = NULL; prev = NULL; }
};
bool is_not_del(linknode* s)
{
	linknode* rb = s;
	if (rb->next->val < rb->val)return false;
	rb = rb->next;
	while (rb->next != s)
	{
		if (rb->next->val < rb->val)return false;
		rb = rb->next;
	}
	return true;
}
void xg(linknode* s)
{
	linknode* p = s->next;
	linknode* pp = p->next;
	while (p != NULL)
	{
		delete p;
		p = pp;
		pp = pp->next;
	}
	return;
}
int main()
{
	ios::sync_with_stdio(false);//imp
	cin.tie(nullptr);//imp
	linknode* head;
	linknode* h = new linknode;
	head = h;
	int t; cin >> t;
	for (int iii = 0; iii < t; iii++)
	{
		int n; cin >> n;
		int xx;
		cin >> xx;
		h->val = xx;
		for (int ii = 1; ii < n; ii++)
		{
			cin >> xx;
			linknode* th = new linknode;
			h->next = th;
			th->prev = h;
			h = h->next;
			h->val = xx;
		}
		head->prev = h;
		h->next = head;
		int mintimes = 0;
		while (true)
		{
			ll thval = head->val;
			while (h->val >= thval and h!=head)h = h->prev;
			if (h == head)break;
			//插入节点
			if (h == head->prev)
			{
				head = head->next;
				h = head->prev;
			}
			else
			{
				h->next->prev = head;
				linknode* headn = h->next;
				linknode* hh = head->next;
				h->next = head;
				head->next->prev = head->prev;
				head->prev->next = head->next;
				head->prev = h;
				head->next = headn;
				head = hh;
				h = head->prev;
			}
			mintimes++;
		}
		if (is_not_del(head))cout << mintimes << endl;
		else cout << -1 << endl;
	}
	return 0;
}

但是通过观察和推理可以知道,当且仅当该列的最小元素后面有序的时候,这个才一定成立
因为每一次前面的插排到后面都不会改变原有的状态,如果后面不是满足要求的时候,那么插排再多次也没用
代码:

#include<iostream>
#include<vector>
#include<algorithm>
#include<math.h>
#include<sstream>
#include<string>
#include<string.h>
#include<iomanip>
#include<stdlib.h>
#include<map>
#include<queue>
#include<limits.h>
#include<climits>
#include<fstream>
#include<stack>
typedef long long ll;
using namespace std;

ll lst[200010];
int main()
{
	int t; cin >> t;
	for (int ii = 0; ii < t; ii++)
	{
		int n; cin >> n;
		ll minx = LLONG_MAX;
		ll minxid = 0;
		for (int i = 0; i < n; i++)
		{
			cin >> lst[i];
			if (lst[i] < minx)
			{
				minxid = i;
				minx = lst[i];
			}
		}
		bool is_end = true;
		for (int i = minxid; i < n-1; i++)
		{
			if (lst[i + 1] < lst[i])
			{
				is_end = false;
				break;
			}
		}
		if (is_end)cout << minxid << endl;
		else cout << -1 << endl;
	}
	return 0;
}

总结:做题要带

标签:Sort,head,prev,int,next,Queue,include,linknode
From: https://www.cnblogs.com/zzzsacmblog/p/18088392

相关文章

  • qsort实现函数排序(2)
    qsort实现结构体排序#include<stdio.h>#include<stdlib.h>#include<string.h>structstu{ charname[20]; intage;};intcmp_by_name(void*p1,void*p2){ returnstrcmp(((structstu*)p1)->name,((structstu*)p2)->name);}voidprint(s......
  • 在sort中传入仿函数
    仿函数就是用来控制排列顺序的map<int,int,Compare>是这样,list.sort()也是这样.//List双向链表.cpp:此文件包含"main"函数。程序执行将在此处开始并结束。//#include<iostream>#include<list>usingnamespacestd;structCompare{ booloperator()(constint&......
  • stack和queue
    stack的介绍1.stack是一种容器适配器,专门用在具有后进先出操作的上下文环境中,其删除只能从容器的一端进行元素的插入与提取操作。2.stack是作为容器适配器被实现的,容器适配器即是对特定类封装作为其底层的容器,并提供一组特定的成员函数来访问其元素,将特定类作为其底层的,......
  • 【Java】List, Set, Queue, Map 区别?
    目录List,Set,Queue,Map区别?Collection和CollectionsListArrayList和Array区别?ArrayList与LinkedList区别?ArrayList能添加null吗?ArrayList插入和删除时间复杂度?LinkedList插入和删除时间复杂度?LinkedList为什么不能实现RandomAccess接口?SetComparabl......
  • qsort函数[3]---冒泡排序与qsort函数的结合
    冒泡排序与qsort函数的结合首先给大家回顾一下冒泡排序voidbubble_sort(intarr[],intsz){ //确定趟数 inti=0; for(i=0;i<sz-1;i++) { //每趟进行两两互相比较 intj=0; for(j=0;j<sz-i-1;j++) { if(arr[j]<arr[j+1]) ......
  • P2824 [HEOI2016/TJOI2016] 排序 与 ABC297_g Range Sort Query 题解
    洛谷题目链接:排序abc题目链接:Atcoder或者洛谷两道差不多的题拿出来说说。本题有双\(\log\)做法,也有单\(\log\)做法,都讲讲。双\(\log\)做法对于第一个题而言,询问最终\(pos\)位置上的数为多少,那么这个问题是否具有单调性?这个是很有意思的点,我们考虑只关注某个数\(x\)......
  • queue 和 stack 容器
    queue容器1.queue基本概念**概念:先进先出队列中只有队头和队尾才可以被外界使用,因此队列不允许有遍历行为2.queue常用接口构造函数:queue<T>que;//queue采用模板类实现,queue对象的默认构造形式queue(constqueue&que);//拷贝构造函数赋值操作:queue&operator=......
  • SortedMap、NavigableMap、TreeMap介绍和使用
    SortedMap、NavigableMap、TreeMap介绍和使用SortedMap接口:SortedMap是一个接口,继承自Map接口,它定义了对键值对按照键的自然顺序或自定义顺序进行排序的功能。SortedMap中的键值对是按照键的顺序排列的,因此可以根据键的顺序进行范围查找和遍历操作。SortedMap接口提供了一......
  • 关于js数组方法sort()负数排序的陷阱
    今天在刷力扣题的时候遇到数组排序的问题,想着图个方便就使用了arr.sort(),刚开始用正数进行测试用例的时候没有出错,问题:在使用负数的测试用例时,预期目标是 [-10,-2,-1...1,2,3],结果出现了 [-1,-2,-10......1,2,3]这样的结果解析:在网上找了一下发现,sort()这个方法:默认......
  • C# 队列(Queue)
    原文链接:https://blog.csdn.net/qq_38693757/article/details/130891605一、概述表示对象的先进先出集合。队列和其他的数据结构一样,是一种存储容器,它遵循先进先出的原则,能够存储任意类型,但并不能获取到指定的位置,只能存入和取出,取出元素后,Queue内部的元素自动删除,其实队列......