2019冬季PAT甲级第二题

 1 #define HAVE_STRUCT_TIMESPEC
 2 #include<bits/stdc++.h>
 3 using namespace std;
 4 typedef struct{
 5     int add,data,nex;
 6 }Node;
 7 Node node[100007],ans[100007],ans2[100007];
 8 map<int,int>mp;
 9 int main(){
10     //ios::sync_with_stdio(false);
11     //cin.tie(NULL);
12     //cout.tie(NULL);
13     int s,n,k;
14     scanf("%d%d%d",&s,&n,&k);
15     for(int i=1;i<=n;++i){
16         scanf("%d%d%d",&node[i].add,&node[i].data,&node[i].nex);
17         mp[node[i].add]=i;
18     }
19     int cnt=0;
20     while(s!=-1){
21         int num=mp[s];
22         ans[++cnt].add=node[num].add;
23         ans[cnt].data=node[num].data;
24         ans[cnt].nex=node[num].nex;
25         s=ans[cnt].nex;
26     }
27     int cnt2=0;
28     int x=cnt/k;
29     if(cnt%k)
30         ++x;
31     for(int i=x;i;--i){
32         for(int j=(i-1)*k+1;j<=min(cnt,i*k);++j){
33             ans2[++cnt2].add=ans[j].add;
34             ans2[cnt2].data=ans[j].data;
35             ans2[cnt2].nex=ans[j].nex;
36         }
37         if(i==1)
38             ans2[cnt2].nex=-1;
39         else
40             ans2[cnt2].nex=ans[(i-2)*k+1].add;
41     }
42     for(int i=1;i<=cnt2;++i){
43         if(i<cnt2)
44             printf("%05d %d %05d\n",ans2[i].add,ans2[i].data,ans2[i].nex);
45         else
46             printf("%05d %d -1",ans2[i].add,ans2[i].data);
47     }
48     return 0;
49 }

原文地址:https://www.cnblogs.com/ldudxy/p/12255723.html

时间: 2024-08-30 12:30:42

2019冬季PAT甲级第二题的相关文章

2019冬季PAT甲级第一题

1 #define HAVE_STRUCT_TIMESPEC 2 #include<bits/stdc++.h> 3 using namespace std; 4 string s[30][10]; 5 int ans[1007][1007]; 6 int num[1007]; 7 string t; 8 int main(){ 9 ios::sync_with_stdio(false); 10 cin.tie(NULL); 11 cout.tie(NULL); 12 for(int i=1;

PAT甲级第二次真题练习

1005 Spell It Right (20)(20 分)提问 Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output every digit of the sum in English. Input Specification: Each input file contains one test case. Each case occupies one

1085. Perfect Sequence (25)-PAT甲级真题

1085. Perfect Sequence (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CAO, Peng Given a sequence of positive integers and another positive integer p. The sequence is said to be a "perfect sequence" if M <= m * p where M and m

PAT甲题题解-1111. Online Map (30)-PAT甲级真题(模板题,两次Dijkstra,同时记下最短路径)

题意:给了图,以及s和t,让你求s到t花费的最短路程.最短时间,以及输出对应的路径.   对于最短路程,如果路程一样,输出时间最少的. 对于最短时间,如果时间一样,输出节点数最少的.   如果最短路程和最短时间路径一样,合并输出一次即可. 纯粹就是练习dijkstra,没什么难的. 第一次dijkstra求最短路程,记录下每个节点的路程和时间. 第二次dijkstra求最短时间,记录下每个节点的时间和经过的节点数. pre数组用来存储前驱节点,保存路径 #include <iostream>

PAT甲级刷题实录——1004

原题链接 https://pintia.cn/problem-sets/994805342720868352/problems/994805521431773184 思路 很明显这题需要用到树这个数据结构,问题是怎么来存.一开始我是这样想的:因为它只问了每一层叶子结点数,所以最简单的情况下我只需要两个数据就行,一个是结点的所在的层级,另一个是结点是否含有子结点.所有的结点都存储在vector中,另外创建一个存储每一层叶子结点个数的数组用于最后输出结果.遍历vector,记录每一层不含有子结点的个

PAT甲级刷题实录——1011

原题链接 https://pintia.cn/problem-sets/994805342720868352/problems/994805504927186944 思路 这题就很简单了,每行输入的时候找出最大的记录下来,同时记录下标.输入完毕后根据下标转换成结果(W,T,L)并储存起来,再根据每行的最大值计算profit.最后输出结果和profit即可,代码如下. 代码 #include <iostream> #include <vector> using namespace s

PAT甲级刷题实录——1010

原题链接 https://pintia.cn/problem-sets/994805342720868352/problems/994805507225665536 思路 这题是到目前为止比较难的一题,评测系统的通过率也只有 0.11. 首先需要理解基本题意.题目的要求是给一个已知进制的数,求能不能找出一个进制使得另一个未知进制的数在该进制下和已知进制的数数值相等.大部分人应该都会想到将两个数的数值都转换为十进制后做比较. 在理解了基本题意之后,做的过程中发现这题还有不少坑. 进制是没有上限的.

PAT甲级刷题实录——1013

原题链接 https://pintia.cn/problem-sets/994805342720868352/problems/994805500414115840 思路 题目大意是说一些城市之间有路相通,假设其中一个城市被敌方占领了,计算需要新修多少条路才能让剩下的城市全部联通.首先这是一个典型的图论问题,我们可以用邻接矩阵去存城市之间的联通关系.可以用深度遍历的思想去解决这个问题.思路大意如下:建立一个数组存储哪些城市已经被联通,1代表已联通,0代表未联通:定义一个变量记录需要新修的路的数量

PAT甲级刷题实录——1014

原题链接 https://pintia.cn/problem-sets/994805342720868352/problems/994805498207911936 思路 这题需要用到队列,而且不止一条.首先是每个等待窗口各需要一条,另外在黄线外的等待顾客需要一条.C++提供了现成了现成的队列类型,只要引用头文件queue即可. 算法基本运行过程是:在输入顾客等待时间时依次填满每条队列,超出队列容量的,即编号大于N*M+1的顾客,则push进黄线外的等待队列中.当有窗口有顾客处理完毕后,则将该顾