PAT A1133 Splitting A Linked List
作者:互联网
PAT A1133 Splitting A Linked List
Sample Input:
00100 9 10
23333 10 27777
00000 0 99999
00100 18 12309
68237 -6 23333
33218 -4 00000
48652 -2 -1
99999 5 68237
27777 11 48652
12309 7 33218
Sample Output:
33218 -4 68237
68237 -6 48652
48652 -2 12309
12309 7 00000
00000 0 99999
99999 5 23333
23333 10 00100
00100 18 27777
27777 11 -1
-
心态崩:
and all the values in [0, K] appear before all those greater than K.
:误把这个条件理解成了下标属于[0,k]并且比K小的元素,实际上就是:以k为分界,0到k直接的数,排在比k大的数前面 -
思路 1:
- 遍历链表,把所有节点用一个vector记录下来
- 对vector遍历三遍,分别筛选出符合3个条件的数:
- 条件1:x<0
- 条件2:0<=x<=m
- 条件3:m<x
- code 1:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn = 100010;
struct Node{
int data, add, next;
}node[maxn];
vector<Node> tmp, ans;
int main(){
int first, n, m, ta;
scanf("%d %d %d", &first, &n, &m);
for(int i = 0; i < n; ++i){
scanf("%d", &ta);
scanf("%d %d", &node[ta].data, &node[ta].next);
node[ta].add = ta;
}
while(first != -1){
tmp.push_back(node[first]);
first = node[first].next;
}
for(int i = 0; i < tmp.size(); ++i){
if(tmp[i].data < 0) ans.push_back(tmp[i]);
}
for(int i = 0; i <= m && i < tmp.size(); ++i){
if(tmp[i].data >= 0 && tmp[i].data <= m) ans.push_back(tmp[i]);
}
for(int i = 0; i <= m && i < tmp.size(); ++i){
if(tmp[i].data > m) ans.push_back(tmp[i]);
}
if(tmp.size() > m){
for(int i = m+1; i < tmp.size(); ++i){
if(tmp[i].data >= 0) ans.push_back(tmp[i]);
}
}
for(int i = 1; i < ans.size(); ++i){
printf("%05d %d %05d\n", ans[i-1].add, ans[i-1].data, ans[i].add);
}
printf("%05d %d -1\n", ans[ans.size()-1].add, ans[ans.size()-1].data);
return 0;
}
标签:tmp,node,PAT,int,Splitting,ans,A1133,data,size 来源: https://blog.csdn.net/qq_42347617/article/details/100176478