echobfy

博客园 首页 新随笔 联系 订阅 管理
  19 随笔 :: 1 文章 :: 9 评论 :: 18991 阅读

模拟题,注意当k == 1 与 k == n时情况

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <map>
 
using namespace std;
 
const int N = 100005;
 
struct Node
{
    int pre;
    int value;
    int lat;
}node[N];
 
int order[N];
int size;
map<int, int> pre2idx;
 
void solve1(int fir, int n)
{
    map<int, int>::iterator it = pre2idx.find(fir);
    order[size++] = it->second;
 
    while (node[it->second].lat != -1)
    {
        it = pre2idx.find(node[it->second].lat);
        order[size++] = it->second;
    }
}
 
void solve2(int k, int n)
{
    int bound = n / k * k;
 
    int idx = k;
    while (idx <= bound)
    {
        int boundt = idx;
        int boundb = idx - k + 1;
        while (boundt >= boundb)
        {
            int next = 0;
            if (boundt > boundb) next = boundt - 1;
            else
            {
                if (idx < bound)
                    next = idx + k;
                else if (idx == bound)
                {
                    if (bound < n)
                        next = idx + 1;
                    else if (bound == n)
                    {
                        printf("%05d %d -1\n", node[order[boundt]].pre, node[order[boundt]].value);
                        break;
                    }
                }
            }
            printf("%05d %d %05d\n", node[order[boundt]].pre, node[order[boundt]].value, node[order[next]].pre);
            boundt--;
        }
        idx += k;
    }
    bound++;
    while (bound <= n)
    {
        if (node[order[bound]].lat != -1)
            printf("%05d %d %05d\n", node[order[bound]].pre, node[order[bound]].value, node[order[bound]].lat);
        else    printf("%05d %d -1\n", node[order[bound]].pre, node[order[bound]].value);
        bound++;
    }
     
}
 
int main()
{
    int fir, n, k;
 
    while (scanf("%d%d%d", &fir, &n, &k) != EOF)
    {
        pre2idx.clear(); size = 1;
        for (int i = 0; i < n; i++)
        {
            scanf("%d%d%d", &node[i].pre, &node[i].value, &node[i].lat);
            pre2idx.insert(make_pair(node[i].pre, i));
        }
 
        solve1(fir, n);
 
        solve2(k , size - 1);
 
    }
    return 0;
}
 
/*
00100 6 6
00000 4 99999
00100 1 -1
68237 6 -1
33218 3 00000
99999 5 68237
12309 2 33218
*/
 
 
 
 
 
 
     
 
 
    

  

posted on   小白哥哥啊  阅读(606)  评论(8编辑  收藏  举报
编辑推荐:
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
· .NET 9 new features-C#13新的锁类型和语义
· Linux系统下SQL Server数据库镜像配置全流程详解
· 现代计算机视觉入门之:什么是视频
阅读排行:
· 【译】我们最喜欢的2024年的 Visual Studio 新功能
· 个人数据保全计划:从印象笔记迁移到joplin
· Vue3.5常用特性整理
· 重拾 SSH:从基础到安全加固
· 为什么UNIX使用init进程启动其他进程?
点击右上角即可分享
微信分享提示