pat A 1074

不愧废物如我,竟然能写出这么蝇营狗苟的代码,只能在pat用,所有链表题都能这么做

#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
struct node {
	int addr, val, ne;
	node(int a, int b, int c) {
		addr = a; val = b; ne = c;
	}
};
vector linklist;
vector realist;
unordered_map ma;
int main()
{
	//freopen("test.txt", "r", stdin);
	int s, n, k;
	scanf("%d%d%d", &s, &n, &k);
	for (int i = 0; i < n; ++i)
	{
		int addr, val, ne;
		scanf("%d%d%d", &addr, &val, &ne);
		node tmp(addr, val, ne);
		linklist.push_back(tmp);
		ma[addr] = i;
	}
	int cur = s;
	while (cur != -1) {
		int index = ma[cur];
		realist.push_back(linklist[index]);
		cur = linklist[index].ne;
	}
	for (int i = 0; i < realist.size(); i+=k)
	{
		int j = i + k - 1;
		if (j >= realist.size())break;
		for (int m=0; m < k / 2; ++m) {
			swap(realist[m+i], realist[j - m]);
		}
	}
	for (int i = 0; i < realist.size() - 1; ++i) {
		printf("%05d %d %05d\n", realist[i].addr, realist[i].val, realist[i+1].addr);
	}
	printf("%05d %d -1\n", realist[realist.size() - 1].addr, realist[realist.size() - 1].val);
	return 0;
}

 

你可能感兴趣的:(pat)