当前位置: 首页 > news >正文

lanqiaoOJ 3255:重新排队 ← STL list 单链表

【题目来源】
https://www.lanqiao.cn/problems/3255/learning/

【题目描述】
给定按从小到大的顺序排列的数字 1 到 n,随后对它们进行 m 次操作,每次将一个数字 x 移动到数字 y 之前或之后。请输出完成这 m 次操作后它们的顺序。

【输入格式】
第一行为两个数字 n, m,表示初始状态为 1 到 n 的从小到大排列,后续有 m 次操作。
第二行到第 m+1 行,每行三个数 x, y, z。当 z=0 时,将 x 移动到 y 之后;当 z=1 时,将 x 移动到 y 之前。


【输出格式】
一行,n 个数字,中间用空格隔开,表示 m 次操作完成后的排列顺序。

【输入样例】
5 3
3 1 0
5 2 1
2 1 1

【输出样例】
2 1 3 5 4

【算法分析】
★ 在大多数情况下,可以直接使用 C++ 中的 STL list,而不需手写链表。通过这种方式完成的代码通常更简洁。本例代码中将会使用 list<int>::iterator it=find(ls.begin(),ls.end(),y); 得到 y 的位置。

★ STL list:https://cplusplus.com/reference/list/
(1)
size():Returns the number of elements in the list container.
(2)
empty():Returns whether the list container is empty (i.e. whether its size is 0).
(3)
push_front():Inserts a new element at the beginning of the list, right before its current first element. 
(4)
push_back():Adds a new element at the end of the list container, after its current last element. 
(5)
pop_front():Removes the first element in the list container, effectively reducing its size by one. 
(6)
pop_back():Removes the last element in the list container, effectively reducing the container size by one.
(7)
front():Returns a reference to the first element in the list container.
(8)
back():Returns a reference to the last element in the list container.
(9)
reverse():Reverses the order of the elements in the list container.
(10)
insert():The container is extended by inserting new elements before the element at the specified position.
(11)
erase():Removes from the list container either a single element (position) or a range of elements ([first,last)).
(12)
unique():Notice that an element is only removed from the list container if it compares equal to the element immediately preceding it. Thus, this function is especially useful for sorted lists.

【算法代码】

#include <bits/stdc++.h>
using namespace std;int main() {int n,m;cin>>n>>m;list<int> ls;for(int i=1; i<=n; i++) {ls.push_back(i);}while(m--) {int x,y,z;cin>>x>>y>>z;ls.remove(x);list<int>::iterator it=find(ls.begin(),ls.end(),y);if(z==0) ls.insert(++it,x);if(z==1) ls.insert(it,x);}for(auto i:ls) cout<<i<<" ";cout<<endl;return 0;
}/*
in:
5 3
3 1 0
5 2 1
2 1 1out:
2 1 3 5 4
*/



【参考文献】
https://blog.csdn.net/mc10141222/article/details/123674677
https://cplusplus.com/reference/list/list/



 

http://www.lryc.cn/news/477403.html

相关文章:

  • 解决虚拟机启动报:此主机支持AMD-V,但AMD-V处于禁用状态
  • 【安装配置教程】二、VMware安装并配置ubuntu22.04
  • ‌5G SSB(同步信号块)位于物理层‌
  • 40.第二阶段x86游戏实战2-初识lua
  • 官方redis安装
  • OpenEuler 使用ffmpeg x11grab捕获屏幕流,rtsp推流,并用vlc播放
  • 呼叫中心报工号功能有没有价值?有没有更好的方案?
  • Unity 6 基础教程(Unity 界面)
  • Vue插槽的使用场景
  • Redis 下载安装(Windows11)
  • 求平面连接线段组成的所有最小闭合区间
  • 编译安装并刷写高通智能机器人SDK
  • 软考:案例题分析1101
  • 如何检查雷池社区版 WAF 是否安装成功?
  • 一周内从0到1开发一款 AR眼镜 相机应用?
  • vue3中setup的作用是什么?
  • java.io.FileNotFoundException: Could not locate Hadoop executable: (详细解决方案)
  • 事件捕获vs 事件冒泡,延申事件委托
  • 接口测试(十一)jmeter——断言
  • 使用buildx构建多架构平台镜像
  • 宠物领养救助管理软件有哪些功能 佳易王宠物领养救助管理系统使用操作教程
  • Spring Boot中实现多数据源连接和切换的方案
  • 科技资讯|谷歌Play应用商店有望支持 XR 头显,AR / VR设备有望得到发展
  • 关于read/write 网络IO、硬盘IO的区别
  • vue2开发 对接后端(go语言)常抛异常情况以及处理方法汇总
  • LSTM:解决梯度消失与长期依赖问题
  • Kafka在大数据处理中的作用及其工作原理
  • w~自动驾驶~合集5
  • Java优先队列的使用
  • 20241105,LeetCode 每日一题,用 Go 实现两数之和的非暴力解法