当前位置:网站首页>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;
}
边栏推荐
- ES6 grammar summary -- Part I (basic)
- arduino获取随机数
- Mysql database interview questions
- Fashion Gen: the general fashion dataset and challenge paper interpretation & dataset introduction
- Pytorch-温度预测
- RT-Thread API参考手册
- RT-Thread的main线程“卡死”的一种可能原因及解决方案
- arduino获取数组的长度
- Missing value filling in data analysis (focus on multiple interpolation method, miseforest)
- Pytorch four commonly used optimizer tests
猜你喜欢
Arduino uno R3 register writing method (1) -- pin level state change
[esp32 learning-1] construction of Arduino esp32 development environment
Characteristics, task status and startup of UCOS III
MongoDB
Kaggle competition two Sigma connect: rental listing inquiries (xgboost)
Togglebutton realizes the effect of switching lights
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
电商数据分析--用户行为分析
ESP学习问题记录
MP3mini播放模块arduino<DFRobotDFPlayerMini.h>函数详解
随机推荐
Correspondence between STM32 model and contex M
Reading notes of difficult career creation
VSCode基础配置
Détails du Protocole Internet
高通&MTK&麒麟 手机平台USB3.0方案对比
关键字 inline (内联函数)用法解析【C语言】
Raspberry pie tap switch button to use
VIM command line notes
open-mmlab labelImg mmdetection
uCOS-III 的特点、任务状态、启动
There are three iPhone se 2022 models in the Eurasian Economic Commission database
Dead loop in FreeRTOS task function
Pytorch four commonly used optimizer tests
Arduino uno R3 register writing method (1) -- pin level state change
Esp8266 connects to onenet cloud platform (mqtt) through Arduino IDE
MySQL START SLAVE Syntax
List and set
ESP8266使用arduino连接阿里云物联网
Kaggle competition two Sigma connect: rental listing inquiries
IOT system framework learning