当前位置:网站首页>1161 Merging Linked Lists
1161 Merging Linked Lists
2022-06-23 07:03:00 【The day the flowers fall】
The question
Given two linked lists , Find the shortest , Then insert it into the long one , Insert a short one every two , A long length must be more than twice as long as a short one .
Code
#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
using namespace std;
const int N = 1e6;
struct node
{
int add;
int data;
int ne;
}node[N];
int main()
{
int L1, L2, n;
cin >> L1 >> L2 >> n;
for (int i = 1; i <= n; i++)
{
int a, b, c;
cin >> a >> b >> c;
node[a] = {
a, b, c};
}
vector<int> l1, l2;
int t = L1;
while (t != -1)
{
l1.push_back(t);
t = node[t].ne;
}
t = L2;
while (t != -1)
{
l2.push_back(t);
t = node[t].ne;
}
if (l1.size() < l2.size()) l1.swap(l2);
reverse(l2.begin(), l2.end());
// for (int i = 0; i < l1.size(); i++) cout << l1[i] << endl;
vector<int> res;
int cnt = 0;
for (int i = 0, j = 0; i < l1.size(); i++)
{
cnt++;
res.push_back(l1[i]);
if (cnt == 2 && j < l2.size())
{
res.push_back(l2[j++]);
cnt = 0;
}
}
for (int i = 0; i < res.size(); i++)
{
if (i + 1 == res.size())
{
cout << setw(5) << setfill('0') << node[res[i]].add << ' ' << node[res[i]].data << ' ' << -1 << endl;
}
else
{
cout << setw(5) << setfill('0') << node[res[i]].add << ' ' << node[res[i]].data << ' ' << setw(5) << setfill('0') << node[res[i + 1]].add << endl;
}
}
return 0;
}
边栏推荐
- 解决挖矿病毒 sshd2(redis未设密码、清除crontab定时任务)
- Open source oauth2 framework for SSO single sign on
- 聚焦行业,赋能客户 | 博云容器云产品族五大行业解决方案发布
- 407-栈与队列(232.用栈实现队列、225. 用队列实现栈)
- Mongodb record
- QT method of compiling projects using multithreading
- JS dynamically creates a href circular download file. Only 10 or a fixed number of files can be downloaded
- WPF Command指令和INotifyPropertyChanged
- [STL] unordered of associated container_ Map Usage Summary
- 【畢業季·進擊的技術er】自己的選擇,跪著也要走
猜你喜欢
![[project training] change of linear arrow](/img/a4/7554522c13684d7590d247f28a6965.png)
[project training] change of linear arrow

网页制作存在的一些难点

How to realize video call and live interaction in a small program when live broadcasting is so popular?

MySQL MVCC多版本并发控制

xml dtd 记录

WPF command directive and inotifypropertychanged

Run typescript code directly using TS node

Eureka

Media industry under the epidemic situation, small program ecology driven digital transformation exploration

【STL】关联容器之map用法总结
随机推荐
746. 使用最小花费爬楼梯-动态规划
Intentional shared lock, intentional exclusive lock and deadlock of MySQL
[daily training] 513 Find the value in the lower left corner of the tree
MySQL function
xml schem 记录
使用ts-node直接运行TypeScript代码
数据在内存中的存储方式(C语言)
XML schema record
Cloud box is deeply convinced to create a smart dual-mode teaching resource sharing platform for Nanjing No. 1 middle school
[saison de remise des diplômes · technologie agressive er] votre choix, agenouillez - vous et partez
Idea automatically generates serialVersionUID
【项目实训10】箭头的绘制
English grammar_ Adjective comparative - Level 3 change
Linux安装mysql8.0.25
994. rotten oranges - non recursive method
Why can't the index of JS array use negative numbers
Chrome remove duplicate bookmarks
Wechat applet - Global Monitoring of certain attribute changes of GlobalData, such as monitoring of network state switching
Common setup modes (Abstract Factory & responsibility chain mode & observer mode)
Copy and paste of idea without escape