2018年东北农业大学春季校赛

本文解析了多个算法题目,包括矩阵计算、迷宫搜索、阶乘尾零计数、集合划分、三维状态搜索等问题,提供了C++实现代码。

 

B   wyh的矩阵 > 24573547

数对称、平均

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <vector>
 6 #include <set>
 7 #include <map>
 8 #include <algorithm>
 9 #include <iostream>
10 using namespace std;
11  
12 int main()
13 {
14     long t;
15     long long n,m;
16     scanf("%ld",&t);
17     while (t)
18     {
19         t--;
20         scanf("%lld",&n);
21         m=n*n;
22         printf("%lld\n",(1+m)/2 * (1+m)/2);
23     }
24     return 0;
25 }

 

D   wyh的迷宫 > 24572730

bfs

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <vector>
 6 #include <set>
 7 #include <map>
 8 #include <algorithm>
 9 #include <iostream>
10 using namespace std;
11  
12 char s[505][505];
13 long dx[4]={-1,0,0,1};
14 long dy[4]={0,-1,1,0};
15 long x[250005],y[250005];
16  
17 int main()
18 {
19     long t,n,m,i,j,p,q,head,tail,xx,yy;
20     char c;
21     scanf("%ld",&t);
22     while (t)
23     {
24         t--;
25         scanf("%ld%ld\n",&n,&m);
26         for (i=0;i<n;i++)
27         {
28             for (j=0;j<m;j++)
29             {
30                 scanf("%c",&s[i][j]);
31                 if (s[i][j]=='s')
32                 {
33                     x[1]=i;
34                     y[1]=j;
35                 }
36                 else if (s[i][j]=='t')
37                 {
38                     p=i;
39                     q=j;
40                 }
41             }
42             scanf("%c",&c);
43         }
44         s[x[1]][y[1]]='x';
45         head=0;
46         tail=1;
47         while (head<tail)
48         {
49             head++;
50             for (i=0;i<4;i++)
51             {
52                 xx=x[head]+dx[i];
53                 yy=y[head]+dy[i];
54                 if (xx>=0 && xx<n && yy>=0 && yy<m && s[xx][yy]!='x')
55                 {
56                     tail++;
57                     x[tail]=xx;
58                     y[tail]=yy;
59                     s[xx][yy]='x';
60                 }
61             }
62             if (s[p][q]=='x')
63                 break;
64         }
65         if (s[p][q]=='x')
66             printf("YES\n");
67         else
68             printf("NO\n");
69     }
70     return 0;
71 }

 

E   wyh的阶乘 > 24573338

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <vector>
 6 #include <set>
 7 #include <map>
 8 #include <algorithm>
 9 #include <iostream>
10 using namespace std;
11  
12 int main()
13 {
14     long x,a,t;
15     scanf("%ld",&t);
16     while (t)
17     {
18         t--;
19         x=0;
20         scanf("%ld",&a);
21         while (a)
22         {
23             a=a/5;
24             x+=a;
25         }
26         printf("%ld\n",x);
27     }
28     return 0;
29 }

 

F   wyh的集合 > 24573728

贪心 分一半

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <vector>
 6 #include <set>
 7 #include <map>
 8 #include <algorithm>
 9 #include <iostream>
10 using namespace std;
11  
12 int main()
13 {
14     long t;
15     long long n;
16     scanf("%ld",&t);
17     while (t)
18     {
19         t--;
20         scanf("%lld",&n);
21         printf("%lld\n",(n/2) * ((n+1)/2));
22     }
23     return 0;
24 }

 

H   wyh的吃鸡 > 24660519

三维数组记录状态(x,y,有车/没车)

时间小的先遍历,堆(可以不做)

剪枝:遇到时间大于等于最短时间的就停止继续扩展

用dfs不太好,纯bfs找到答案时不能直接退出

  1 #include <cstdio>
  2 #include <cstdlib>
  3 #include <cstring>
  4 #include <cmath>
  5 #include <vector>
  6 #include <set>
  7 #include <map>
  8 #include <queue>
  9 #include <algorithm>
 10 #include <iostream>
 11 using namespace std;
 12 #define inf 1000000001
 13 #define maxn 100+5
 14 #define maxlen 100*100+5
 15  
 16  
 17 struct node
 18 {
 19     long x,y,z;
 20 };
 21  
 22 char s[maxn][maxn];
 23 long ti[maxn][maxn][2];
 24 long dx[4]={-1,0,0,1};
 25 long dy[4]={0,-1,1,0};
 26 long n,zz;
 27 //1 +2
 28 //0 +1
 29  
 30 struct cmp
 31 {
 32     bool operator() (node a,node b)
 33     {
 34         return ti[a.x][a.y][a.z]>ti[b.x][b.y][b.z] || (ti[a.x][a.y][a.z]==ti[b.x][b.y][b.z] && a.z<b.z);
 35         //return ti[a.x][a.y][a.z]>ti[b.x][b.y][b.z];
 36     }
 37 };
 38 priority_queue<node,vector<node>,cmp> f;
 39  
 40 void bfs()
 41 {
 42     struct node p,pp;
 43     long i,x,y,z,mint=inf;
 44     while (!f.empty())
 45     {
 46         p=f.top();
 47         f.pop();
 48         if (p.z==1 && ti[p.x][p.y][0]<=ti[p.x][p.y][1])
 49             continue;
 50         for (i=0;i<4;i++)
 51         {
 52             x=p.x+dx[i];
 53             y=p.y+dy[i];
 54             if (x>=0 && x<n && y>=0 && y<n && s[x][y]!='O')
 55             {
 56                 if (p.z==0 || s[x][y]=='C')
 57                     z=0;
 58                 else
 59                     z=1;
 60                 if (ti[p.x][p.y][p.z]+p.z+1<ti[x][y][z])
 61                 {
 62                     pp.x=x;
 63                     pp.y=y;
 64                     pp.z=z;
 65                     f.push(pp);
 66                     ti[x][y][z]=ti[p.x][p.y][p.z]+p.z+1;
 67                     if (s[x][y]=='X')
 68                         mint=min(mint,ti[x][y][z]);
 69                 }
 70             }
 71         }
 72     }
 73     if (mint<=zz)
 74     {
 75         printf("YES\n");
 76         printf("%ld\n",mint);
 77     }
 78     else
 79         printf("NO\n");
 80 }
 81  
 82 int main()
 83 {
 84     struct node p;
 85     long t,i,j;
 86     char c;
 87     scanf("%ld",&t);
 88     while (t)
 89     {
 90         t--;
 91         scanf("%ld%ld",&n,&zz);
 92         for (i=0;i<n;i++)
 93         {
 94             scanf("%c",&c);
 95             for (j=0;j<n;j++)
 96             {
 97                 ti[i][j][0]=inf;
 98                 ti[i][j][1]=inf;               
 99                 scanf("%c",&s[i][j]);
100                 if (s[i][j]=='S')
101                 {
102                     p.x=i;
103                     p.y=j;
104                     p.z=1;
105                     ti[i][j][1]=0;
106                     f.push(p);
107                 }
108             }
109         }
110         bfs();     
111     }
112     return 0;
113 }

 

I   wyh的物品 > 24658961

对于固定的单位价值V0,求出V(Value)-V0*S(Space)的值,降序排序,取前k个求和(最优情况),若和大于等于0,则说明Sum(V)>=Sum(S)*V0,因此可以二分

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cmath>
 4 #include <cstring>
 5 #include <stdbool.h>
 6 #include <set>
 7 #include <vector>
 8 #include <map>
 9 #include <queue>
10 #include <algorithm>
11 #include <iostream>
12 using namespace std;
13 #define maxn 100000+5
14  
15 double a[maxn],b[maxn],c[maxn];
16  
17 int main()
18 {
19     long t,n,m,i;
20     double l,r,mid,sum;
21     scanf("%ld",&t);
22     while (t--)
23     {
24         scanf("%ld%ld",&n,&m);
25         for (i=1;i<=n;i++)
26             scanf("%lf%lf",&b[i],&a[i]);
27         l=0; r=100000.0;
28         while (r-l>0.001)
29         {
30             mid=(l+r)/2.0;
31             for (i=1;i<=n;i++)
32                 c[i]=a[i]-mid*b[i];
33             sort(c+1,c+n+1);
34             sum=0;
35             for (i=n;i>n-m;i--)
36                 sum+=c[i];
37             if (sum>=0)
38                 l=mid;
39             else
40                 r=mid;
41         }
42         printf("%.2lf\n",r);
43     }
44     return 0;
45 }

 

K   wyh的数列 > 24691132

unsigned long long 2^64

f(n)=f(n-1)+f(n-2)

前两个状态决定了当前状态,因此

找循环节:若condition(t),condtion(t+1)与之前某个状态(condtion(x),condtion(x+1))相同,则循环节为x~t-1,同时循环节不大于模(c)的平方

对于第a^b个数,分不在循环节 和 在循环节,若在循环节,找到这个数在循环节的位置

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <vector>
 6 #include <set>
 7 #include <map>
 8 #include <queue>
 9 #include <algorithm>
10 #include <iostream>
11 using namespace std;
12 #define maxn 1000+5
13 #define maxm 1000*1000+5
14   
15 long f[maxm];
16 long vis[maxn][maxn];
17   
18 int main()
19 {
20     long t,c,i,j,d,r; 21 unsigned long long a,b; 22 scanf("%ld",&t); 23 while (t--) 24  { 25 scanf("%lu%lu%ld",&a,&b,&c); 26 for (i=0;i<c;i++) 27 for (j=0;j<c;j++) 28 vis[i][j]=-1; 29 f[0]=0; 30 f[1]=1; 31 vis[1][0]=1; 32 for (i=2;i<=c*c;i++) 33  { 34 f[i]=(f[i-1]+f[i-2])%c; 35 if (vis[f[i]][f[i-1]]!=-1) 36 break; 37 else 38 vis[f[i]][f[i-1]]=i; 39  } 40 //[vis[p][q],i) 41 if (a==1 || b*log(a)<=log(i)) 42  { 43 printf("%ld\n",f[(long)pow(a,b)]); 44 continue; 45  } 46 47 d=i-vis[f[i]][f[i-1]]; 48 a=a%d; 49 r=1; 50 while (b) 51  { 52 if ((b & 1)==1) 53 r=r*a%d; 54 a=a*a%d; 55 b=b>>1; 56  } 57 r=((r-vis[f[i]][f[i-1]])%d+d)%d; 58 59 printf("%lu\n",f[vis[f[i]][f[i-1]]+r]); 60  } 61 return 0; 62 } 63 /* 64 100 65 1 1 2 66 2 3 1000 67 32122142412412142 124124124412124 123 68 2 10 1000 69 243 70 3 100 1005 71 72 */

 

M   wyh的数字 > 24573154

字符处理最方便

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <vector>
 6 #include <set>
 7 #include <map>
 8 #include <algorithm>
 9 #include <iostream>
10 using namespace std;
11  
12 int main()
13 {
14     long t,g;
15     char c;
16     scanf("%ld\n",&t);
17     while (t)
18     {
19         t--;
20         g=0;
21         while (1)
22         {
23             scanf("%c",&c);
24             if (c=='\n')
25                 break;
26             if (c=='7')
27                 g++;
28         }
29         printf("%ld\n",g);
30     }
31     /*
32     long x,a,t;
33     scanf("%ld",&t);
34     while (t)
35     {
36         x=0;
37         scanf("%ld",&a);
38         while (a)
39         {
40             a=a/5;
41             x+=a;
42         }
43         printf("%ld\n",x);
44     }
45     */
46     return 0;
47 }

 

转载于:https://www.cnblogs.com/cmyg/p/8877788.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值