1. 程式人生 > >西南民族大學第十屆校賽(同步賽)

西南民族大學第十屆校賽(同步賽)

可AK場,題目非常基礎,可惜比賽時太困,沒來得及AK...由於一開始選了C++14,導致寫B題時用gets函式一直編譯報錯,用getline就一直T到飛...賽後改C++11就過了,最終沒來得及看非常easy的J題QAQ~

A.dreamstart的催促:籤道題。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 const LL mod=10000019;
 5 const int maxn=1e5+5;
 6 int n;LL ans,x;
 7
LL quick_mod(LL a,LL b){ 8 LL res=1LL; 9 while(b){ 10 if(b&1)res=res*a%mod; 11 a=a*a%mod; 12 b>>=1; 13 } 14 return res; 15 } 16 int main(){ 17 while(~scanf("%d",&n)){ 18 ans=0; 19 for(int i=1;i<=n;++i)scanf("%lld
",&x),ans=(ans+quick_mod(x,i))%mod; 20 printf("%lld\n",ans); 21 } 22 return 0; 23 }
View Code

B.TRDD got lost again:基礎的bfs,注意讀入的方式。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 const int maxn=6005;
 5 int n,m,a,b,sx,sy;char
mp[maxn][maxn],mo[maxn];bool vis[maxn][maxn]; 6 int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};///上下,左右 7 struct node{int x,y,step;}nod,tmp; 8 queue<node> que; 9 int bfs(int x,int y){ 10 while(!que.empty())que.pop(); 11 vis[nod.x=x][nod.y=y]=true,nod.step=1; 12 que.push(nod); 13 while(!que.empty()){ 14 nod=que.front(),que.pop(); 15 int nx=nod.x,ny=nod.y,nstep=nod.step; 16 if(mp[nx][ny]=='T')return nstep+1; 17 for(int i=0;i<2;++i){///上下 18 int gx=nx+dir[i][0],gy=ny+dir[i][1],gstep=nstep+1; 19 if(gx>=0&&gx<a&&gy>=0&&gy<b&&!vis[gx][gy]&&mp[gx][gy]!='-'&&mp[gx][gy]!='+'){ 20 tmp.x=gx,tmp.y=gy,tmp.step=gstep,vis[gx][gy]=true; 21 que.push(tmp); 22 } 23 } 24 for(int i=2;i<4;++i){///左右 25 int gx=nx+dir[i][0],gy=ny+dir[i][1],gstep=nstep+1; 26 if(gx>=0&&gx<a&&gy>=0&&gy<b&&!vis[gx][gy]&&mp[gx][gy]!='|'&&mp[gx][gy]!='+'){ 27 tmp.x=gx,tmp.y=gy,tmp.step=gstep,vis[gx][gy]=true; 28 que.push(tmp); 29 } 30 } 31 } 32 return -1; 33 } 34 int main(){ 35 while(~scanf("%d%d",&n,&m)){ 36 a=2*n+1,b=2*m+1;getchar();///注意讀走回車符 37 memset(vis,false,sizeof(vis)); 38 for(int i=0;i<a;++i)gets(mp[i]); 39 for(int i=0;i<n;++i){ 40 for(int j=0;j<m;++j){ 41 if(mp[2*i+1][2*j+1]=='S'){sx=2*i+1,sy=2*j+1;break;} 42 } 43 } 44 int ans=bfs(sx,sy); 45 if(ans!=-1)printf("%d\n",ans/2); 46 else puts("TRDD Got lost...TAT"); 47 } 48 return 0; 49 }
View Code

C.Company:先預處理一下每個節點的權值,再dfs跑一下即可。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 const int maxn=200005;
 5 int n,k,x,u,v,w[maxn];vector<int> vec[maxn];
 6 void dfs(int now,int per){
 7     for(size_t i=0;i<vec[now].size();++i){
 8         if(per^vec[now][i]){
 9             dfs(vec[now][i],now);
10             w[now]+=w[vec[now][i]];
11         }
12     }
13 }
14 int main(){
15     while(cin>>n>>k){
16         for(int i=1;i<=n;++i)cin>>x,w[i]=x>k?0:1,vec[i].clear();
17         for(int i=1;i<n;++i){
18             cin>>u>>v;
19             vec[u].push_back(v);
20             vec[v].push_back(u);
21         }
22         dfs(1,0);
23         for(int i=1;i<=n;++i)cout<<w[i]<<(i==n?'\n':' ');
24     }
25     return 0;
26 }
View Code

D.>A->B->C-:-_-三角戀。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 const LL mod=10000019;
 5 const int maxn=5005;
 6 int n,love[maxn],b,c;bool flag;
 7 int main(){
 8     while(cin>>n){
 9         flag=false;memset(love,-1,sizeof(love));
10         for(int i=1;i<=n;++i)cin>>love[i];
11         for(int i=1;i<=n;++i){
12             b=love[i],c=love[b];
13             if(love[c]==i){flag=true;break;}
14         }
15         puts(flag?"YES":"NO");
16     }
17     return 0;
18 }
View Code

E.PPY的字串:用字串暴力模擬一下即可。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 int n,cnt;string per,now;char tmp;
 5 int main(){
 6     while(cin>>per>>n){
 7         for(int j=2;j<=n;++j){
 8             now="";
 9             for(int i=0;per[i];++i){
10                 cnt=0,tmp=per[i];
11                 while(per[i]&&tmp==per[i])i++,cnt++;
12                 now+=cnt+'0';
13                 now+=tmp;
14                 --i;
15             }
16             per=now;
17         }
18         cout<<per.size()<<' '<<per<<endl;
19     }
20     return 0;
21 }
View Code

F.集訓隊脫單大法:這是一道只能由學姐我自己出資料的水題:用優選佇列最大堆維護當前序列的最大值,用set容器維護後面序列的最大值,掃一遍即可。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 const LL mod=10000019;
 5 const int maxn=1e5+5;
 6 int n,a[maxn],ans;set<int> st;map<int,int> mp;set<int>::iterator it;
 7 priority_queue<int> que;
 8 int main(){
 9     while(cin>>n){
10         st.clear(),mp.clear();ans=0;
11         while(!que.empty())que.pop();
12         for(int i=0;i<n;++i)cin>>a[i],mp[a[i]]++,st.insert(a[i]);
13         for(int i=0;i<n-1;++i){
14             que.push(a[i]);
15             mp[a[i]]--;
16             if(!mp[a[i]])st.erase(a[i]);
17             it=st.end();--it;
18             ans=max(ans,abs(*it-que.top()));
19         }
20         cout<<ans<<endl;
21     }
22     return 0;
23 }
View Code

G.不想再WA了:入門dp。定義dp[i][0~2:0表示'A';1表示'C';2表示'W']表示當前組成長度為i且以字元j結尾的字串方案數。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 const LL mod=10000019;
 5 int n,T;LL dp[15][3],ans[15];
 6 int main(){
 7     memset(dp,0,sizeof(dp));
 8     memset(ans,0,sizeof(ans));
 9     dp[0][0]=1;
10     for(int i=1;i<=10;++i){
11         dp[i][0]+=dp[i-1][0]+dp[i-1][1];///A
12         dp[i][1]+=dp[i-1][0]+dp[i-1][1]+dp[i-1][2];///C
13         dp[i][2]+=dp[i-1][0]+dp[i-1][1]+dp[i-1][2];///w
14     }
15     for(int i=1;i<=10;++i)ans[i]=dp[i][0]+dp[i][1]+dp[i][2];///預處理答案
16     while(cin>>T){
17         while(T--){
18             cin>>n;
19             cout<<ans[n]<<endl;
20         }
21     }
22     return 0;
23 }
View Code

H.Ricky’s RealDan’s Ricky:簡單博弈,模擬一下玩法可知當且僅當只有一個偶數時,先手必贏,其餘情況後手必贏。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 int T,n,x,odd,even;
 5 int main(){
 6     while(cin>>T){
 7         while(T--){
 8             cin>>n,odd=even=0;
 9             for(int i=0;i<n;++i){
10                 cin>>x;
11                 if(x&1)odd++;
12                 else even++;
13             }
14             if(n==1&&even)puts("Ricky is Winner");
15             else puts("RealDan is Winner");
16         }
17     }
18     return 0;
19 }
View Code

I.小A的期末作業

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 const LL mod=10000019;
 5 const int maxn=5005;
 6 int n;
 7 int main(){
 8     while(cin>>n){
 9         for(int i=1;i<=n;++i)printf("*");
10         puts("");
11         for(int i=1;i<n;++i){
12             for(int j=1;j<=i;++j)printf(" ");
13             for(int j=1;j<=n;++j)printf("*");
14             puts("");
15         }
16         for(int i=n-2;i>=0;--i){
17             for(int j=1;j<=i;++j)printf(" ");
18             for(int j=1;j<=n;++j)printf("*");
19             puts("");
20         }
21     }
22     return 0;
23 }
View Code

J.怪盜基德 & 月之瞳寶石:離散化處理,先對能源體的位置進行排序,對於每個星體,找最近的能源體,最後取個最大的距離即可。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 const int maxn=1e5+5;
 5 int n,m;LL a[maxn],b[maxn],ans,pos;
 6 int main(){
 7     while(cin>>n>>m){
 8         for(int i=0;i<n;++i)cin>>a[i];
 9         for(int i=0;i<m;++i)cin>>b[i];
10         sort(b,b+m);ans=0;
11         for(int i=0;i<n;++i){
12             pos=upper_bound(b,b+m,a[i])-b;
13             if(!pos)ans=max(ans,b[pos]-a[i]);
14             else if(pos==m)ans=max(ans,abs(a[i]-b[pos-1]));
15             else ans=max(ans,min(abs(b[pos]-a[i]),abs(a[i]-b[pos-1])));
16         }
17         cout<<ans<<endl;
18     }
19     return 0;
20 }
View Code

K.正方體:簡單處理一下即可。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 const LL mod=10000019;
 5 const int maxn=1e5+5;
 6 int T,x,mo[7],cnt,flag;
 7 int main(){
 8     while(cin>>T){
 9         flag=0;
10         while(T--){
11             memset(mo,0,sizeof(mo)),cnt=0;flag++;
12             for(int i=1;i<=4;++i){cin>>x;if(x!=0)mo[1]=x;}
13             for(int i=2;i<=5;++i)cin>>mo[i];
14             for(int i=1;i<=4;++i){cin>>x;if(x!=0)mo[6]=x;}
15             cnt+=(mo[1]==mo[6]);
16             cnt+=(mo[2]==mo[4]);
17             cnt+=(mo[3]==mo[5]);
18             if(cnt==3)puts("Yes!");
19             else puts("No!");
20             if(flag%50==0)puts("");
21         }
22     }
23     return 0;
24 }
View Code

L.簡單的分數:常規寫法。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 const LL mod=10000019;
 5 const int maxn=5005;
 6 int T,op,a,b,c,d,ans1,ans2,tmp,flag;
 7 int gcd(int a,int b){return b?gcd(b,a%b):a;}
 8 int main(){
 9     while(cin>>T){
10         while(T--){
11             cin>>op>>a>>b>>c>>d;flag=0;
12             ans1=a*d+(!op?-1:1)*b*c;
13             ans2=b*d;
14             if(ans1<0)flag++,ans1*=-1;
15             if(ans2<0)flag++,ans2*=-1;
16             tmp=gcd(ans1,ans2);
17             ans1/=tmp,ans2/=tmp;
18             if(flag&1)printf("-");
19             printf("%d/%d\n",ans1,ans2);
20         }
21     }
22     return 0;
23 }
View Code

M.HJ澆花:差分標記,再暴力一下求字首和即可。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 const LL mod=10000019;
 5 const int maxn=1e6+5;
 6 int n,l,r,xfen[maxn];map<int,int> mp;
 7 int main(){
 8     while(cin>>n){
 9         memset(xfen,0,sizeof(xfen));
10         mp.clear();
11         for(int i=0;i<n;++i){
12             cin>>l>>r;
13             xfen[l]++,xfen[r+1]--;
14         }
15         mp[xfen[0]]++;
16         for(int i=1;i<=1000000;++i){
17             xfen[i]+=xfen[i-1];
18             mp[xfen[i]]++;
19         }
20         for(int i=1;i<=n;++i)cout<<mp[i]<<(i==n?'\n':' ');
21     }
22  
23     return 0;
24 }
View Code