15省赛题回顾

D.锐雯上单不给就送

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <cmath>
 6 using namespace std;
 7 struct Matrix
 8 {
 9     double mat[5][5];
10 }a;
11 Matrix mul(Matrix a,Matrix b)
12 {
13     Matrix c;
14     for(int i=0;i<5;i++)
15     {
16         for(int j=0;j<5;j++)
17         {
18             c.mat[i][j]=0;
19             for(int k=0;k<5;k++)
20                 c.mat[i][j]=(c.mat[i][j]+a.mat[i][k]*b.mat[k][j]);
21         }
22     }
23     return c;
24 }
25 Matrix mod_pow(Matrix p,int n)
26 {
27     Matrix res;
28     memset(res.mat,0,sizeof(res.mat));
29     for(int i=0;i<5;i++)
30         res.mat[i][i]=1;
31     while(n)
32     {
33         if(n&1)
34             res=mul(res,p);
35         p=mul(p,p);
36         n>>=1;
37     }
38     return res;
39 }
40 int main()
41 {
42     int T,n;
43     freopen("in.txt","r",stdin);
44     cin>>T;
45     while(T--)
46     {
47         cin>>n;
48         for(int i=0;i<5;i++)
49             for(int j=0;j<5;j++)
50                 cin>>a.mat[i][j];
51         int num;
52         cin>>num;
53         if(n==1)
54         {
55             cout<<num<<endl;
56             continue;
57         }
58         Matrix ans=mod_pow(a,n-1);
59         double maxn=0,sum=0,index=0;
60         for(int i=0;i<5;i++)
61         {
62             if((ans.mat[i][num-1]-maxn)>0.0000001)
63             {
64                 maxn=ans.mat[i][num-1];
65                 index=i;
66             }
67         }
68         cout<<index+1<<endl;
69     }
70     return 0;
71 }

I.梯田

 1 #include <cstring>
 2 #include <iostream>
 3 #include <algorithm>
 4 #include <cstdio>
 5 using namespace std;
 6 #define Max 1000005
 7 int map[105][105],vis[105][105];
 8 int n,m,p,q;
 9 int dx[]={0,0,-1,1},dy[]={-1,1,0,0};
10 int ans,h;
11 int dfs(int x,int y)
12 {
13     if(map[x][y]>h||x<0||y<0||x>n+1||y>m+1||vis[x][y]==1)
14         return 0;
15     ans++;
16     vis[x][y]=1;
17     for(int i=0;i<4;i++)
18     {
19         int x0=x+dx[i];
20         int y0=y+dy[i];
21         dfs(x0,y0);
22     }
23     return 0;
24 }
25 int main()
26 {
27     int T;
28     freopen("in.txt","r",stdin);
29     cin>>T;
30     while(T--)
31     {
32         int u=-1;
33         cin>>n>>m>>p>>q;
34         memset(map,0,sizeof(map));
35         memset(vis,0,sizeof(vis));
36         for(int i=1;i<=n;i++)
37             for(int j=1;j<=m;j++)
38                 cin>>map[i][j];
39         int l=0,r=Max,mid;
40         while(r-l>1)
41         {
42             memset(vis,0,sizeof(vis));
43             h=mid=(l+r)/2;
44             ans=0;
45             dfs(0,0);
46             ans-=(2*n+2*m+4);
47         //    cout<<mid<<" "<<ans<<endl;
48             if(ans<=q&&ans>=p)
49             {
50                 u=mid;
51                 r=mid;
52             }
53             else if(ans>q)
54                 r=mid;
55             else 
56                 l=mid;
57         }
58         cout<<u<<endl;
59     }
60 }

J.镜像树

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6 #define Max 105
 7 struct node
 8 {
 9     int l,r;
10 }tree[Max];
11 int val[Max];
12 bool flag;
13 int dfs(int x,int y)
14 {
15     if(flag)
16         return 0;
17     if(x==y&&x==0)
18         return 0;
19     if((x==0&&y!=0)||(x!=0&&y==0))
20     {
21         flag=1;
22         return 0;
23     }
24     if(val[x]!=val[y])
25     {
26         flag=1;
27         return 0;
28     }
29     dfs(tree[x].l,tree[y].r);
30     dfs(tree[x].r,tree[y].l);
31     return 0;
32 }
33 int main()
34 {    
35     int T,u,n;
36     freopen("in.txt","r",stdin);
37     cin>>T;
38     while(T--)
39     {
40         flag=0;
41         memset(tree,0,sizeof(tree));
42         memset(val,0,sizeof(val));
43         cin>>n;
44         for(int i=0;i<n;i++)
45         {
46             cin>>u;
47             cin>>tree[u].l>>tree[u].r;
48         }
49         for(int i=1;i<=n;i++)
50             cin>>val[i];
51         dfs(2,3);
52         if(flag)
53             cout<<"No"<<endl;
54         else
55             cout<<"Yes"<<endl;
56     }
57     return 0;
58 }
原文地址:https://www.cnblogs.com/a1225234/p/5464501.html