当前位置:网站首页>4273. Linked list merging (day 64)
4273. Linked list merging (day 64)
2022-07-19 16:47:00 【Zhangxueheng】
List of articles
1: subject
Given two single chain tables L1=a1→a2→…→an−1→an and L2=b1→b2→…→bm−1→bm.
If n≥2m, Your task is to reverse the shorter list , Then merge it into a longer linked list , Get the shape of a1→a2→bm→a3→a4→bm−1… Result .
For example, give two linked lists as 6→7 and 1→2→3→4→5, You should output 1→2→7→3→4→6→5.
Add
This question may contain nodes that are not in the two single linked lists , These nodes need not be considered .
Input format
Input first gives two linked lists in the first row L1 and L2 The address of the header node of , And positive integers N, That is, the total number of nodes given .
The address of a node is 5 Nonnegative integer of digits ( May contain leading 0), Empty address NULL use −1 Express .
And then N That's ok , Each line gives the information of a node in the following format :
Address Data Next
among Address Is the address of the node ,Data No more than 105 The positive integer ,Next Is the address of the next node .
The title ensures that there is no empty linked list , And the longer list is at least twice as long as the shorter list .
Output format
Output the result linked list in order , Each node occupies a row , The format is the same as the input .
Data range
1≤N≤105
sample input :
00100 01000 7
02233 2 34891
00100 6 00001
34891 3 10086
01000 1 02233
00033 5 -1
10086 4 00033
00001 7 -1
sample output :
01000 1 02233
02233 2 00001
00001 7 34891
34891 3 10086
10086 4 00100
00100 6 00033
00033 5 -1
difficulty : Simple
when / Empty limit :0.4s / 64MB
Total number of passes :1673
Total attempts :4062
source :PAT Class a real topic 1161
Algorithm tags
2: Realization :
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#define x first
#define y second
using namespace std;
typedef pair<int, int> PII;
const int N = 100010;
int h1, h2, n;
int v[N], ne[N];
int main()
{
scanf("%d%d%d", &h1, &h2, &n);
while (n -- )
{
int addr, val, next;
scanf("%d%d%d", &addr, &val, &next);
v[addr] = val, ne[addr] = next;
}
vector<PII> a, b;
for (int i = h1; i != -1; i = ne[i]) a.push_back({
i, v[i]});
for (int i = h2; i != -1; i = ne[i]) b.push_back({
i, v[i]});
if (a.size() < b.size()) swap(a, b);
vector<PII> c;
for (int i = 0, j = b.size() - 1; i < a.size(); i += 2, j -- )
{
c.push_back(a[i]);
if (i + 1 < a.size()) c.push_back(a[i + 1]);
if (j >= 0) c.push_back(b[j]);
}
for (int i = 0; i < c.size(); i ++ )
{
printf("%05d %d ", c[i].x, c[i].y);
if (i + 1 < c.size()) printf("%05d\n", c[i + 1].x);
else puts("-1");
}
return 0;
}
边栏推荐
- Cloud service ecs/rds: build ECS management Linux, build cloud database management, and create RDS MySQL;
- Ribbon负载均衡策略
- 【Unity3D】UGUI之InputField
- Ribbon modify the default load balancing policy
- Entropy technology passed the registration: the annual revenue was 1.955 billion, and the book balance of accounts receivable was 290million
- JMeter 21 day clock in Day12
- CANoe:. What is a vmodule file
- Data Lake (13): spark and iceberg integrate DDL operations
- 面试题 01.04. 回文排列-辅助数组法
- Alibaba cloud (OSS) file upload and deletion
猜你喜欢
分布式笔记(03)— 分布式缓存之 Redis(单机模式、Redis Cluster 模式概述)
向数据库表中插入中文数据报错“1366 (HY000): Incorrect string value: ‘\xE5\x90\x95\xE5‘ for column ‘name‘ at row 1“
CSDN认证C1级别学习笔记 - Web进阶篇
【mysql篇-进阶篇】存储引擎
【云原生】3.6 RuoYi-Cloud部署实战(中)
Vs2019 MFC slider control control inherits csliderctrl class to redraw self paint
Nacos中使用ribbon
Nacos cluster construction
Aspose. OCR 22.6 for . NET//Aspose. OCR
【Unity3D】UGUI之Dropdown
随机推荐
Weekly resume of personal IP lab · issue 19
WCF——实现带认证的服务及常见报错
字符集7-10
Nacos cluster construction
占位,稍等补
Quick review of interview (II): why is cross entropy useful
CANoe:. What is a vmodule file
The record cannot be copied and pasted after being started with mremoting software
CF591A Wizards‘ Duel
数据湖(十三):Spark与Iceberg整合DDL操作
Ribbon modify the default load balancing policy
Vs2019 MFC dynamically creates edit control, csliderctrl class member function create application creates slider control control [mfc dynamically creates control 4]
Demo using go JSON path
538. Transform binary search tree into cumulative tree DFS method
[unity3d] inputfield of ugui
Gesture recognition dataset: Jester dataset decompression
Rsync data synchronization script
阿里云(OSS)文件上传和删除
简单斐波那契(DAY 67)
rsync数据同步脚本