Given a singly linked list, you are supposed to rearrange its elements so that all the negative values appear before all of the non-negatives, and all the values in [0, K] appear before all those greater than K. The order of the elements inside each class must not be changed. For example, given the list being 18→7→-4→0→5→-6→10→11→-2 and K being 10, you must output -4→-6→-2→7→0→5→10→18→11.
Input Specification:
Each input file contains one test case. For each case, the first line contains the address of the first node, a positive N (≤10^5) which is the total number of nodes, and a positive K (≤10^3). The address of a node is a 5-digit nonnegative integer, and NULL is represented by −1.
Then N lines follow, each describes a node in the format:
Address Data Next
where Address
is the position of the node, Data
is an integer in [−10^5,10^5], and Next
is the position of the next node. It is guaranteed that the list is not empty.
Output Specification:
For each case, output in order (from beginning to the end of the list) the resulting linked list. Each node occupies a line, and is printed in the same format as in the input.
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
题目大意:给出一个有 n 个节点的静态链表和整数 k,将节点值小于 0 的结点放到链表的最前面,再将节点值位于 0~k 区间的结点放到中间,最后将节点值大于 k 的结点放到最后面。要求每部分的节点处于原来的相对次序,输出调整后的静态链表。
分析:用三个数组分别存储这三个部分的链表节点。实际上链表的后一位是什么并不是特别重要,重点是把它们存到对应链表的顺序。
cpp
#include<algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <cstdio>
#include <queue>
#include <stack>
#include <ctime>
#include <cmath>
#include <map>
#include <set>
#define INF 0xffffffff
#define db1(x) cout<<#x<<"="<<(x)<<endl
#define db2(x,y) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<endl
#define db3(x,y,z) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<endl
#define db4(x,y,z,r) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#r<<"="<<(r)<<endl
#define db5(x,y,z,r,w) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#r<<"="<<(r)<<", "<<#w<<"="<<(w)<<endl
using namespace std;
typedef struct node
{
int val,id,next;
}node;
node num[100010];
int main(void)
{
#ifdef test
freopen("in.txt","r",stdin);
//freopen("in.txt","w",stdout);
clock_t start=clock();
#endif //test
int head,n,k;scanf("%d%d%d",&head,&n,&k);
int cnt_pos=0,cnt_lessk=0,cnt_bigk=0;
for(int i=0;i<n;++i)
{
int a,b,c;scanf("%d%d%d",&a,&b,&c);
num[a].val=b,num[a].id=a,num[a].next=c;
if(b<0)cnt_pos++;
else if(b>k)cnt_bigk++;
else cnt_lessk++;
}
int np,nlk,nbk;np=nlk=nbk=0;
node pos[cnt_pos+5],lessk[cnt_lessk+5],bigk[cnt_bigk+5];
int temp=head;
while(temp!=-1)
{
if(num[temp].val<0)pos[np++]=num[temp];
else if(num[temp].val>k)bigk[nbk++]=num[temp];
else lessk[nlk++]=num[temp];
temp=num[temp].next;
}
for(int i=0;i<np;++i)
{
if(i==0)printf("%05d %d",pos[i].id,pos[i].val);
else printf(" %05d\n%05d %d",pos[i].id,pos[i].id,pos[i].val);
}
for(int i=0;i<nlk;++i)
{
if(i==0)
{
if(np==0)printf("%05d %d",lessk[i].id,lessk[i].val);
else printf(" %05d\n%05d %d",lessk[i].id,lessk[i].id,lessk[i].val);
}
else printf(" %05d\n%05d %d",lessk[i].id,lessk[i].id,lessk[i].val);
}
for(int i=0;i<nbk;++i)
{
if(i==0)
{
if(np==0&&nlk==0)printf("%05d %d",bigk[i].id,bigk[i].val);
else printf(" %05d\n%05d %d",bigk[i].id,bigk[i].id,bigk[i].val);
}
else printf(" %05d\n%05d %d",bigk[i].id,bigk[i].id,bigk[i].val);
}
printf(" -1\n");
#ifdef test
clockid_t end=clock();
double endtime=(double)(end-start)/CLOCKS_PER_SEC;
printf("\n\n\n\n\n");
cout<<"Total time:"<<endtime<<"s"<<endl; //s为单位
cout<<"Total time:"<<endtime*1000<<"ms"<<endl; //ms为单位
#endif //test
return 0;
}