1052. Linked List Sorting (25)
来源:互联网 发布:hbuilder for mac下载 编辑:程序博客网 时间:2024/04/27 13:01
1052. Linked List Sorting (25)
时间限制400 ms内存限制65536 kB代码长度限制16000 B判题程序Standard作者CHEN, YueA linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integer key and a Next pointer to the next structure. Now given a linked list, you are supposed to sort the structures according to their key values in increasing order.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive N (< 105) and an address of the head node, where N is the total number of nodes in memory and the address of a node is a 5-digit positive integer. NULL is represented by -1.
Then N lines follow, each describes a node in the format:
Address Key Next
where Address is the address of the node in memory, Key is an integer in [-105, 105], and Next is the address of the next node. It is guaranteed that all the keys are distinct and there is no cycle in the linked list starting from the head node.
Output Specification:
For each test case, the output format is the same as that of the input, where N is the total number of nodes in the list and all the nodes must be sorted order.
Sample Input:5 0000111111 100 -100001 0 2222233333 100000 1111112345 -1 3333322222 1000 12345Sample Output:5 1234512345 -1 0000100001 0 1111111111 100 2222222222 1000 3333333333 100000 -1首先给出N个地址,再给出第一个地址
接着每个地址 地址存放值 下一个地址
……
目的:通过第一个地址获得这条所带有的全部存储单元,在把这些单元通过存储的值从小到大排序。输出地址注意格式
用到
#include<vector>动态数组#include<algorithm>排序#include<iomanip>设置格式
评测结果
时间 结果 得分 题目 语言 用时(ms) 内存(kB) 用户 8月12日 15:04答案正确251052C++ (g++ 4.7.2)2793752datrilla测试点
测试点 结果 用时(ms) 内存(kB) 得分/满分 0答案正确130815/151答案正确13083/32答案正确13083/33答案正确27937523/34答案正确13081/1
#include<iostream> #include<vector>#include<algorithm>#include<iomanip>#define MAX 100000using namespace std; struct Links{ int key; int next; Links(){} Links(int k,int n):key(k),next(n){}};void readln(Links*H, int N){ Links temp; int addr; while (N--) { cin >> addr >> temp.key >> temp.next; H[addr] = temp; }}void Together(Links*H, int Headaddr, vector< Links>*sortH){ while (Headaddr != -1) { sortH->push_back(Links(H[Headaddr].key, Headaddr)); Headaddr = H[Headaddr].next; }}bool Hcmp(const Links &A, const Links &B){ return A.key < B.key;}void format(int a, int b, int c){ cout << setw(5) << setfill('0') << a <<" "<< b << " "<<setw(5) << setfill('0') << c << endl;}int main(){ Links H[MAX]; int N, Headaddr; vector< Links>sortH; cin >> N >> Headaddr; readln(H, N); Together(H, Headaddr, &sortH); sort(sortH.begin(), sortH.end(), Hcmp); Headaddr=sortH.size(); if (Headaddr == 0) { cout << "0 -1" << endl; } else { cout << Headaddr-- << " " << setw(5) << setfill('0') << sortH[0].next << endl; for (N = 0; N < Headaddr; N++) format(sortH[N].next, sortH[N].key, sortH[N + 1].next); cout << setw(5) << setfill('0')<< sortH[N].next << " " << sortH[N].key << " -1" << endl; } system("pause"); return 0;}
- 1052. Linked List Sorting (25)
- 1052. Linked List Sorting (25)
- 1052. Linked List Sorting (25)
- 1052. Linked List Sorting (25)
- 1052. Linked List Sorting (25)
- 1052. Linked List Sorting (25)
- 1052. Linked List Sorting (25)
- 1052. Linked List Sorting (25)
- 1052. Linked List Sorting (25)
- 1052. Linked List Sorting (25)
- 1052. Linked List Sorting (25)
- 1052. Linked List Sorting (25)
- 1052. Linked List Sorting (25)
- 1052. Linked List Sorting (25)
- 1052. Linked List Sorting (25)
- 1052. Linked List Sorting (25)
- 1052. Linked List Sorting (25)
- 1052. Linked List Sorting (25)
- Android应用程序资源的编译和打包过程分析 (转自老罗的博客)
- 【Linux】grep的简单用法
- 修改u-boot的开机logo及显示过程
- Windows Android Studio 打开失败
- HTML5图片拖拽
- 1052. Linked List Sorting (25)
- mybatis--mapper映射文件配置之select,resultMap
- java反射,获得Class是否为基本数据类型,是否其他类的父类。获得泛型。
- Burp suite的安装教程
- 架构师应该掌握什么技术?
- selenim java判断图片有没有加载完成
- 华为OJ(名字漂亮度)
- 关于android中broadcast和BroadcastReceiver浅谈
- [LeetCode] Populating Next Right Pointers in Each Node II