当前位置:网站首页>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;
}
边栏推荐
- Variable parameter principle of C language function: VA_ start、va_ Arg and VA_ end
- The first simple case of GNN: Cora classification
- 树莓派 轻触开关 按键使用
- C语言函数之可变参数原理:va_start、va_arg及va_end
- Contiki source code + principle + function + programming + transplantation + drive + network (turn)
- ESP8266通过arduino IED连接巴法云(TCP创客云)
- Linux Yum install MySQL
- C language callback function [C language]
- VSCode基础配置
- Keyword inline (inline function) usage analysis [C language]
猜你喜欢
Arduino JSON data information parsing
Esp8266 uses Arduino to connect Alibaba cloud Internet of things
共用体(union)详解【C语言】
C语言回调函数【C语言】
ESP8266使用arduino连接阿里云物联网
RT-Thread 线程的时间片轮询调度
Togglebutton realizes the effect of switching lights
ESP学习问题记录
ES6语法总结--下篇(进阶篇 ES6~ES11)
ES6 grammar summary -- Part 2 (advanced part es6~es11)
随机推荐
Redis interview questions
JS变量类型以及常用类型转换
互联网协议详解
Gallery之图片浏览、组件学习
C语言回调函数【C语言】
Programmers can make mistakes. Basic pointers and arrays of C language
Détails du Protocole Internet
FreeRTOS 任务函数里面的死循环
ES6语法总结--下篇(进阶篇 ES6~ES11)
E-commerce data analysis -- User Behavior Analysis
Kaggle competition two Sigma connect: rental listing inquiries
Kaggle竞赛-Two Sigma Connect: Rental Listing Inquiries
List and set
树莓派 轻触开关 按键使用
arduino获取数组的长度
XML file explanation: what is XML, XML configuration file, XML data file, XML file parsing tutorial
Pytoch temperature prediction
History object
Time slice polling scheduling of RT thread threads
Machine learning -- linear regression (sklearn)