CSP2014-12

CSP201412-1 门禁系统

 

 

 

 

最近事情好多qwq,但还是要保持每天至少一题的节奏害……虽然是个超级无敌大水题(bushi

昨晚在学校OJ上做了两道往年cspT5 T4的题目qwq,果然毒瘤……

忙过下个周就冲冲冲

 1 //
 2 //  main.cpp
 3 //  CSP201412-1 门禁系统
 4 //
 5 //  Created by sylvia on 2021/10/17.
 6 //  Copyright © 2021 apple. All rights reserved.
 7 //
 8 #include <iostream>
 9 #include <string.h>
10 using namespace std;
11 int a[1000+2],n,tmp;
12 int main(){
13     cin>>n;
14     memset(a,0,sizeof(a));
15     for (int i=1;i<=n;i++){
16         cin>>tmp;
17         cout<<++a[tmp]<<' ';
18     }
19     cout<<endl;
20 }

 

  CSP201412-2 Z字形扫描

 

 

 

 

气!又是这种🌶️🐓模拟题,花了我俩小时,最后还是90,不想再查哪里❌了。。。。浪费我时间

每次遇到这种既不是大模拟,又不是小模拟的题都会调到吐血

orzzzzz还是太废物了

 1 //
 2 //  main.cpp
 3 //  CSP201412-2 Z字形扫描
 4 //
 5 //  Created by sylvia on 2021/11/1.
 6 //  Copyright © 2021 apple. All rights reserved.
 7 //
 8 
 9 #include <iostream>
10 #include <stdio.h>
11 #include <math.h>
12 #include <algorithm>
13 #include <string.h>
14 using namespace std;
15 #define M 500+5
16 int a[M][M];
17 int n;
18 int i=1,j=2;
19 
20 void leftdown(){
21     cout<<a[i][j]<<' ';
22     if (j==1||i>=n) return ;
23     i++;j--;
24     leftdown();
25     return;
26 
27 
28 
29 }
30 void rightup(){
31     cout<<a[i][j]<<' ';
32     if(i==1||j>=n) return;
33     i--;j++;
34     rightup();
35     return;
36 }
37 
38 
39 int main(){
40     cin>>n;
41     for (int i=1;i<=n;i++){
42         for (int j=1;j<=n;j++){
43             cin>>a[i][j];
44         }
45     }
46     
47     cout<<a[1][1]<<' ';
48     for (int k=1;k<=n-1;k++){
49         int t=k%2;
50         if (t){
51             leftdown();
52             i++;
53         }else {
54             rightup();
55             j++;
56         }
57     }
58     int flag=-1;
59     if (n%2) {  //leftdown
60         flag=1;
61         i=2;
62         j=n;
63     } else {  //rightup
64         flag=0;
65         i=n;
66         j=2;
67     }
68     for (int k=1;k<=n-2;k++){
69         if (k%2==flag){
70             leftdown();
71             j++;
72             
73         }else{
74             rightup();
75             i++;
76         }
77     }
78     cout<<a[n][n]<<endl;
79 }
View Code

 

 

 

  CSP201412-4 最优灌溉

 

 

 

 今日大无语,第2、3题看了半个小时发现没思路,结果一看第4题竟然是我会的……

第四题典型最小生成树板子题,下面代码就当以后要用的模板了。。。(kruscal&并查集)

原理戳这里

复杂度O(ElogE) 

 1 //
 2 //  main.cpp
 3 //  CSP201412-4 最优灌溉
 4 //
 5 //  Created by sylvia on 2021/10/30.
 6 //  Copyright © 2021 apple. All rights reserved.
 7 //
 8 //最小生成树板子题
 9 #include <iostream>
10 #include <stdio.h>
11 #include <math.h>
12 #include <algorithm>
13 #include <string.h>
14 using namespace std;
15 #define M 1000+5
16 #define N 100000+5
17 struct edge{
18     int u,v,value;
19 }a[N];
20 int cmp(edge x,edge y){
21     return x.value<y.value;
22 }
23 int V,E;  //顶点数与边数
24 int father[M],rankk[M];
25 void init(int n){//并查集初始化
26     for (int i=0;i<n;i++){
27         father[i]=i;
28         rankk[i]=0;
29     }
30 }
31 int find(int x){
32     int j,k,r;
33     r=x;
34     while (r!=father[r]) r=father[r];
35     k=x;
36     while (k!=r){
37         j=father[k];
38         father[k]=r;
39         k=j;
40     }
41     return r;
42 }
43 void unite(int x,int y){  //合并集合
44     x=find(x);
45     y=find(y);
46     if(x==y) return;
47     if (rankk[x]<rankk[y]) father[x]=y;
48     else {
49         father[y]=x;
50         if(rankk[x]==rankk[y]) rankk[x]++;
51     }
52 }
53 int same(int x,int y){  //判断是否在一个集合
54     return find(x)==find(y);
55 }
56 
57 int kruskal(){
58     sort(a,a+E,cmp);
59     init(V);
60     int ans=0;
61     for (int i=0;i<E;i++){
62         edge e=a[i];
63         if(!same(e.u,e.v)){
64             unite(e.u,e.v);
65             ans+=e.value;
66         }
67     }
68     return ans;
69 }
70 int main(){
71     cin>>V>>E;
72     for (int i=0;i<E;i++){
73         cin>>a[i].u>>a[i].v>>a[i].value;
74         
75     }
76     cout<<kruskal()<<endl;
77     return 0;
78 }

 

posted @ 2021-10-17 11:33  Sylvia_lee  阅读(37)  评论(0)    收藏  举报