PAT 1133 Splitting A Linked List
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
#include<iostream> //偏简单, 看清题目
#include<vector>
using namespace std;
struct node{
int val;
int next;
};
int main(){
int First, n, k, t=-1, cnt=0;
cin>>First>>n>>k;
vector<node> data(100000);
vector<int> negative, posl, posr, ans;
for(int i=0; i<n; i++){
int d, v, next;
cin>>d;
cin>>data[d].val>>data[d].next;
}
while(First!=-1){
if(data[First].val<0)
negative.push_back(First);
else if(data[First].val<=k)
posl.push_back(First);
else if(data[First].val>k)
posr.push_back(First);
First=data[First].next;
}
for(int i=0; i<negative.size(); i++)
ans.push_back(negative[i]);
for(int i=0; i<posl.size(); i++)
ans.push_back(posl[i]);
for(int i=0; i<posr.size(); i++)
ans.push_back(posr[i]);
for(int i=0; i<ans.size(); i++)
if(i!=ans.size()-1)
printf("%05d %d %05d\n", ans[i], data[ans[i]].val, ans[i+1]);
else
printf("%05d %d -1", ans[i], data[ans[i]].val);
return 0;
}
PAT 1133 Splitting A Linked List的更多相关文章
- PAT 1133 Splitting A Linked List[链表][简单]
1133 Splitting A Linked List(25 分) Given a singly linked list, you are supposed to rearrange its ele ...
- PAT A1133 Splitting A Linked List (25 分)——链表
Given a singly linked list, you are supposed to rearrange its elements so that all the negative valu ...
- 1133 Splitting A Linked List
题意:把链表按规则调整,使小于0的先输出,然后输出键值在[0,k]的,最后输出键值大于k的. 思路:利用vector<Node> v,v1,v2,v3.遍历链表,把小于0的push到v1中 ...
- PAT A1133 Splitting A Linked List (25) [链表]
题目 Given a singly linked list, you are supposed to rearrange its elements so that all the negative v ...
- PAT1133:Splitting A Linked List
1133. Splitting A Linked List (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- PAT_A1133#Splitting A Linked List
Source: PAT A1133 Splitting A Linked List (25 分) Description: Given a singly linked list, you are su ...
- PAT-1133(Splitting A Linked List)vector的应用+链表+思维
Splitting A Linked List PAT-1133 本题一开始我是完全按照构建链表的数据结构来模拟的,后来发现可以完全使用两个vector来解决 一个重要的性质就是位置是相对不变的. # ...
- PAT甲级——A1133 Splitting A Linked List【25】
Given a singly linked list, you are supposed to rearrange its elements so that all the negative valu ...
- PAT甲级——A1074 Reversing Linked List
Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem ...
随机推荐
- bzoj1047
二维单调队列 rmq很明显会超时,如果这个序列是一维的,很明显就是个单调队列,现在就是把一维的单调队列转换为二维单调队列. 先求出每一列的窗口极值,然后对于每一行做单调队列,值就是之前求出每个位置结尾 ...
- TI BLE:SCAN
主机会运行SCAN来搜寻广播中的设备 运行函数: GAPCentralRole_StartDiscovery( DEFAULT_DISCOVERY_MODE, DEFAULT_DISCOVERY_AC ...
- hdu1213 并查集不压缩
题意:题意:一个人请人吃饭,相互认识的朋友在一张桌子,相互认识的朋友的意思是如果A认识B,B认识C,那么A.B.C是朋友,对于每组输入输出桌子的张数. Sample Input 2 5 3 1 2 2 ...
- 手机访问PC网站自动跳转到手机网站代码
方法一: <script type="text/javascript"> try { var urlhash = window.location.hash; if (! ...
- 10.11NOIP模拟题(3)
/* 可以看出,对于一段区间[L,R]如果统计了答案 若a[L]<a[R],那么当右端点往左移时答案不会更优,a[R]>a[L]同理 所以两个指针分别从头尾往中间扫那边小移哪边即可. */ ...
- AMD的规范使用
1.解决命名冲突 2.解决繁琐的文件依赖 3. 可读性.可依赖性 参考这里 // foobar.js // 私有变量 var test = 123: // 公有方法 function foot ...
- HTML文档中class的命名规则以及命名规范
1.采用英文字母.数字以及“-”和“_”命名. 2.以小写字母开头,不能以数字和“-”.“_”开头. 3.命名形式:单字,连字符,下划线和驼峰. 4.使用有意义命名. 其中(3).(4)条规定主要是便 ...
- python/shell脚本报异常^M: bad interpreter: No such file or directory
问题:在Windows写了一python脚本,上传Linux服务器执行,报异常*****^M: bad interpreter: No such file or directory 原因:window ...
- 【STM32H7教程】第22章 STM32H7的SysTick实现多组软件定时器
完整教程下载地址:http://forum.armfly.com/forum.php?mod=viewthread&tid=86980 第22章 STM32H7的SysTick实现 ...
- Lind.DDD.DynamicModules动态模块化的设计
回到目录 在Lind.DDD框架里有Module,主要用于全局自动添加的模块,它类似于ABP系统里的Module,但有时过于自动化了可能使系统太死板,而有时将需要的模块手动载入可能对我们更合适,所以大 ...