标签:div ios nod amp 标记 math turn using cto
自己的算法没有AC,还差一个测试点答案错误,说明自己的算法还不够完美,自己的思路是标记链表每个数原来的顺序,之后为每个区间的数分类,当进行sort的时候,会以分类的类别和原来链表的顺序来进行排序。还是多看看柳神的代码吧。
#include<cstdio> #include<iostream> #include<cstring> #include<string> #include<algorithm> using namespace std; const int maxn = 100010; struct Node { int pos; int next; int data; int flag; int index; }; Node node[maxn]; int beg, n, k; bool cmp(Node a, Node b) { if (a.flag != b.flag) return a.flag > b.flag; else { return a.index < b.index; } } int main() { cin >> beg >> n >> k; for (int i = 0; i < n; i++) { int add, dat, nex; cin >> add >> dat >> nex; node[add].pos = add; node[add].data = dat; node[add].next = nex; if (dat < 0) { node[add].flag = 3; } else if (dat >= 0 && dat <= k) { node[add].flag = 2; } else { node[add].flag = 1; } } int t = beg; int math = 1; while (1) { node[t].index = math; t = node[t].next; math++; if (t == -1) { break; } } sort(node, node + maxn, cmp); for (int i = 0; i < n; i++) { if (i != n - 1) { printf("%05d %d %05d\n", node[i].pos, node[i].data, node[i + 1].pos); } else printf("%05d %d -1\n", node[i].pos, node[i].data); } return 0; }
柳神AC代码:
#include <iostream> #include <vector> using namespace std; struct node { int data, next; }list[100000]; vector<int> v[3]; int main() { int start, n, k, a; scanf("%d%d%d", &start, &n, &k); for (int i = 0; i < n; i++) { scanf("%d", &a); scanf("%d%d", &list[a].data, &list[a].next); } int p = start; while(p != -1) { int data = list[p].data; if (data < 0) v[0].push_back(p); else if (data >= 0 && data <= k) v[1].push_back(p); else v[2].push_back(p); p = list[p].next; } int flag = 0; for (int i = 0; i < 3; i++) { for (int j = 0; j < v[i].size(); j++) { if (flag == 0) { printf("%05d %d ", v[i][j], list[v[i][j]].data); flag = 1; } else { printf("%05d\n%05d %d ", v[i][j], v[i][j], list[v[i][j]].data); } } } printf("-1"); return 0; }
标签:div ios nod amp 标记 math turn using cto
原文地址:https://www.cnblogs.com/ZJU-LOSER/p/12774420.html