当前位置:网站首页>Pat 1097 duplication on a linked list (25 points)
Pat 1097 duplication on a linked list (25 points)
2022-07-06 12:08:00 【Python ml】
#include <iostream>
#include <vector>
#include <set>
using namespace std;
struct node{
int address,val;
};
int Data[100005],Next[100005];
int main() {
int first,n,temp;
cin>>first>>n;
vector<node>a(n),a1,a2;
for(int i=0;i<n;i++){
scanf("%d",&temp);
scanf("%d %d",&Data[temp],&Next[temp]);
}
int i=0;
while(first!=-1){
a[i++]={
first,Data[first]};
first=Next[first];
}
set<int>s;
for(int i=0;i<n;i++){
if(s.find(abs(a[i].val))==s.end()){
s.insert(abs(a[i].val));
a1.push_back({
a[i].address,a[i].val});
}else{
a2.push_back({
a[i].address,a[i].val});
}
}
for(int i=0;i<a1.size();i++){
printf("%05d %d ",a1[i].address,a1[i].val);
if(i!=a1.size()-1) printf("%05d\n",a1[i+1].address);
else cout<<-1<<endl;
}
for(int i=0;i<a2.size();i++){
printf("%05d %d ",a2[i].address,a2[i].val);
if(i!=a2.size()-1) printf("%05d\n",a2[i+1].address);
else cout<<-1<<endl;
}
system("pause");
return 0;
}

#include <cstdio>
#include <stdlib.h>
#include <algorithm>
using namespace std;
const int maxn = 100000;
struct NODE {
int address, key, next, num = 2 * maxn;
}node[maxn];
bool exist[maxn];
int cmp1(NODE a, NODE b){
return a.num < b.num;
}
int main() {
int begin, n, cnt1 = 0, cnt2 = 0, a;
scanf("%d%d", &begin, &n);
for(int i = 0; i < n; i++) {
scanf("%d", &a);
scanf("%d%d", &node[a].key, &node[a].next);
node[a].address = a;
}
for(int i = begin; i != -1; i = node[i].next) {
if(exist[abs(node[i].key)] == false) {
exist[abs(node[i].key)] = true;
node[i].num = cnt1;
cnt1++;
}
else {
node[i].num = maxn + cnt2;
cnt2++;
}
}
sort(node, node + maxn, cmp1);
int cnt = cnt1 + cnt2;
for(int i = 0; i < cnt; i++) {
if(i != cnt1 - 1 && i != cnt - 1) {
printf("%05d %d %05d\n", node[i].address, node[i].key, node[i+1].address);
} else {
printf("%05d %d -1\n", node[i].address, node[i].key);
}
}
return 0;
}

边栏推荐
- . elf . map . list . Hex file
- E-commerce data analysis -- salary prediction (linear regression)
- I2C总线时序详解
- [template] KMP string matching
- Priority inversion and deadlock
- Inline detailed explanation [C language]
- ESP8266通过arduino IED连接巴法云(TCP创客云)
- 几个关于指针的声明【C语言】
- Oppo vooc fast charging circuit and protocol
- STM32 how to locate the code segment that causes hard fault
猜你喜欢
随机推荐
arduino UNO R3的寄存器写法(1)-----引脚电平状态变化
Analysis of charging architecture of glory magic 3pro
VIM command line notes
Machine learning -- decision tree (sklearn)
C language callback function [C language]
Oppo vooc fast charging circuit and protocol
Encodermappreduce notes
FTP file upload file implementation, regularly scan folders to upload files in the specified format to the server, C language to realize FTP file upload details and code case implementation
Apprentissage automatique - - régression linéaire (sklearn)
MP3mini播放模块arduino<DFRobotDFPlayerMini.h>函数详解
. elf . map . list . Hex file
Rough analysis of map file
MySQL START SLAVE Syntax
Arduino gets the length of the array
Contiki source code + principle + function + programming + transplantation + drive + network (turn)
Pytorch实现简单线性回归Demo
Missing value filling in data analysis (focus on multiple interpolation method, miseforest)
Reno7 60W super flash charging architecture
Dependency in dependencymanagement cannot be downloaded and red is reported
列表的使用









