2021团体程序设计天梯赛】L2部分(PTA,L2-037到L2-040)题解代码&复盘

网友投稿 642 2022-05-29

都是模拟,没啥好说的。

T1注意轨道空了的时候按钮没反应的,考场一遍过了复盘的时候给忘了改了好久。

T2题目没说清楚可以从任意节点出发,以为只有0出发改了好久最后都没改出来,最后只拿了17分,不然就国二了,复盘的时候套个循环就A了,害。另外补充了一种并查集的解法。

T3用STL模拟,30行就可以敲出来,考场的时候因为忘记输出最开始的长度4,然后交上去一直全WA,想了好久没想出来qaq

T4就是建个图直接模拟就行,好像去年也有这种题。

L2-037 包装机 25 69 257 0.27

L2-038 病毒溯源 25 72 277 0.26

L2-039 清点代码库 25 47 487 0.10

L2-040 哲哲打游戏 25 39 223 0.17

#include using namespace std; const int maxn = 2e4+10; string ss[1010]; int f[1010]; int main(){ int n, m, s; cin>>n>>m>>s; for(int i = 1; i <= n; i++){ cin>>ss[i]; } stackstk; int x; while(cin>>x && x!=-1){ if(x==0){ if(stk.size()==0){ continue; } cout<=ss[x].size())continue; if(stk.size()==s){ cout<

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

#include using namespace std; const int maxn = 2e4+10; vectorG[maxn]; int ans = 0, ans2=0, root=0; void dfs(int u, int h){ ans = max(ans,h); for(int v:G[u]){ dfs(v,h+1); } } int ok = 0; vectorvc; void dfs2(int u,int h){ if(ok==1)return ; if(h==ans2 && ok==0){ ok = 1; for(int i = 0; i < ans2; i++){ if(i!=0)cout<<" "<>n; for(int i = 0; i < n; i++){ int k; cin>>k; for(int j = 0; j < k; j++){ int x; cin>>x; G[i].push_back(x); } if(G[i].size()!=0)sort(G[i].begin(),G[i].end()); } for(int i = 0; i < n; i++){ dfs(i,1); if(ans>ans2){ ans2 = ans; root = i; } } cout<

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

//给出一棵树,求最长链 //并查集不要路径压缩,统计每个点所在的链 ps. 可以不是从0开始 #include using namespace std; const int maxn = 1e5+10; int fa[maxn+10]; inline void init(int n){for(int i = 0; i < n; i++)fa[i]=i;} inline int find(int x){return x==fa[x]?x:find(fa[x]);} inline void merge(int x, int y){ if(x!=y)fa[y] = x;} int cc[maxn+10]; inline int size(int x){//TLE5 int ans = 1; while(x!=fa[x])x=fa[x],ans++; return ans; } bool cmp(vectora, vectorb){ for(int i = 0; i < a.size(); i++) if(a[i]!=b[i])return a[i]>n; init(n); for(int i = 0; i < n; i++){ int k; scanf("%d",&k); for(int j = 1; j <= k; j++){ int x; scanf("%d",&x); merge(i,x); } } //先找最长链 int len = 1; for(int i = 0; i < n; i++){ cc[i] = size(i); len = max(len, cc[i]); } //存储所有路径,排序输出最短 vector >ans; for(int i = 0; i < n; i++){ if(cc[i]!=len)continue; vectorvc; int x = i; while(x!=fa[x]){ vc.push_back(x); x = fa[x]; } vc.push_back(x); reverse(vc.begin(),vc.end()); ans.push_back(vc); } sort(ans.begin(),ans.end(),cmp); //cout<

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

#include using namespace std; const int maxn = 2e4+10; bool cmp(pair, int> a, pair, int> b){ if(a.second!=b.second)return a.second>b.second; for(int i = 0; i < a.first.size(); i++){ if(a.first[i]!=b.first[i]) return a.first[i]>n>>m; map, int>ma; for(int i = 1; i <= n; i++){ vectorvc; for(int j = 1; j <= m; j++){ int x; cin>>x; vc.push_back(x); } ma[vc]++; } vector, int> >vc(ma.begin(), ma.end()); sort(vc.begin(),vc.end(), cmp); cout<

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

#include using namespace std; const int maxn = 1e5+10; vectorG[maxn]; int back[110]; int main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, m; cin>>n>>m; for(int i = 1; i <= n; i++){ int k; cin>>k; for(int j = 1; j <= k; j++){ int x; cin>>x; G[i].push_back(x); } } int now = 1; for(int i = 1; i <= m; i++){ int op, x; cin>>op>>x; if(op==0){ now = G[now][x-1]; }else if(op==1){ back[x] = now; cout<

1

2

3

4

5

6

【2021团体程序设计天梯赛】L2部分(PTA,L2-037到L2-040)题解代码&amp;复盘

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

5G游戏

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Android 渐变圆环,圆形进度条效果实现
下一篇:认识云存储不可不知的5大优势
相关文章