Codeforces Round #533 (Div. 2) ABCD 题解

题目链接

A. Salem and Sticks

分析

暴力就行,题目给的n<=1000,ai<=100,暴力枚举t,t从2枚举到98,复杂度是1e5,完全可行.

代码

 1 #include <cstdio>
 2 #include <cmath>
 3 #include <iostream>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <vector>
 7 #include <string>
 8 #include <utility>
 9 #include <queue>
10 #include <stack>
11 #include <map>
12 #include <set>
13 using namespace std;
14 const int INF=0x3f3f3f3f;
15
16 int a[1000];
17
18 int main()
19 {
20 //    freopen("in.txt","r",stdin);
21 //    freopen("out.txt","w",stdout);
22     int n;
23     cin>>n;
24     for(int i=0;i<n;i++) cin>>a[i];
25     int ans=INF,res;
26     for(int i=2;i<100;i++)
27     {
28         int sum=0;
29         for(int j=0;j<n;j++)
30         {
31             int x=a[j]-i;
32             if(x<0) x*=-1;
33             if(x>1) sum=sum+x-1;
34         }
35         if(ans>sum) ans=sum,res=i;
36     }
37     cout<<res<<‘ ‘<<ans<<endl;
38     return 0;
39 }

B. Zuhair and Strings

分析

暴力题,暴力查找k个相同字母组成的序列就行.

代码

 1 #include <cstdio>
 2 #include <cmath>
 3 #include <iostream>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <vector>
 7 #include <string>
 8 #include <utility>
 9 #include <queue>
10 #include <stack>
11 #include <map>
12 #include <set>
13 using namespace std;
14 const int INF=0x3f3f3f3f;
15
16 char a[200000];
17 int ans[26];
18
19 int main()
20 {
21 //    freopen("in.txt","r",stdin);
22 //    freopen("out.txt","w",stdout);
23     int n,k;
24     cin>>n>>k;
25     getchar();
26     for(int i=0;i<n;i++) scanf("%c",&a[i]);
27     int i=0;
28     while(i<n)
29     {
30         char s=a[i];
31         int j=0;
32         while(j<k&&i+j<n)
33         {
34             if(a[i+j]!=s) break;
35             j++;
36         }
37         if(j==k) ans[s-‘a‘]++;
38         i=i+j;
39     }
40     int res=0;
41     for(int i=0;i<26;i++)
42     res=max(res,ans[i]);
43     cout<<res<<endl;
44     return 0;
45 }

C. Ayoub and Lost Array

分析

计数dp,算出l到r区间中mod3是0的数a0,mod3是1的数a1,mod3是2的数a2,dp[i][j]表示,i个数的和mod3是j,dp[1][0]=a0,dp[1][1]=a1,dp[1][2]=a3,i从2开始更新,最后dp[n][0]就是答案.

代码

 1 #include <cstdio>
 2 #include <cmath>
 3 #include <iostream>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <vector>
 7 #include <string>
 8 #include <utility>
 9 #include <queue>
10 #include <stack>
11 #include <map>
12 #include <set>
13 using namespace std;
14 const int INF=0x3f3f3f3f;
15
16 const int mod=1000000007;
17 long long dp[200001][3];
18
19 int main()
20 {
21 //    freopen("in.txt","r",stdin);
22 //    freopen("out.txt","w",stdout);
23     int n,l,r;
24     cin>>n>>l>>r;
25     long long a0,a1,a2;
26     a0=r/3-(l-1)/3;
27     a1=(r+2)/3-(l+1)/3;
28     a2=(r+1)/3-l/3;
29     dp[1][0]=a0,dp[1][1]=a1,dp[1][2]=a2;
30     for(int i=2;i<=n;i++)
31     {
32         dp[i][0]=(dp[i-1][0]*a0+dp[i-1][1]*a2+dp[i-1][2]*a1)%mod;
33         dp[i][1]=(dp[i-1][0]*a1+dp[i-1][1]*a0+dp[i-1][2]*a2)%mod;
34         dp[i][2]=(dp[i-1][0]*a2+dp[i-1][1]*a1+dp[i-1][2]*a0)%mod;
35     }
36     cout<<dp[n][0]<<endl;
37     return 0;
38 }

D. Kilani and the Game

分析

bfs扩展题,这里的bfs需要控制步数且同时进行,每个玩家的speed就要求控制步数,同时进行是为了防止一个点扩展后把另一个点包住导致另一个点无法扩展,可以用优先队列设置优先级来完成这个bfs,结构体可以设置 a,b坐标,c步数(在一个回合内走了多少步),d回合数,e序数(第几个玩家),优先级设置是 回合数(从小到大)>序列数(从小到大)>步数(从小到大),需要注意的是优先队列的排序方式,优先队列是默认大的在前,小的在后.

代码

 1 #include <cstdio>
 2 #include <cmath>
 3 #include <iostream>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <vector>
 7 #include <string>
 8 #include <utility>
 9 #include <queue>
10 #include <stack>
11 #include <map>
12 #include <set>
13 using namespace std;
14 const int INF=0x3f3f3f3f;
15
16 struct N
17 {
18     int a,b,c,d,e;
19     N (){}
20     N (int x,int y,int s,int t,int u){a=x,b=y,c=s,d=t,e=u;}
21     bool operator < (const N &x)const
22     {
23         if(d!=x.d) return d>x.d;
24         else if(e!=x.e) return e>x.e;
25         else return c>x.c;
26     }
27 };
28
29 char mapp[1000][1000];
30 priority_queue<N> que;
31 int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1};
32 int ans[10];
33 int speed[10];
34 int n,m;
35
36 void bfs(void)
37 {
38     while(que.size())
39     {
40         N p=que.top();que.pop();
41         int x=p.a,y=p.b,step=p.c,turn=p.d,s=p.e;
42         for(int i=0;i<4;i++)
43         {
44             int nx=x+dx[i],ny=y+dy[i];
45             if(nx>=0&&nx<n&&ny>=0&&ny<m&&mapp[nx][ny]==‘.‘)
46             {
47                 mapp[nx][ny]=mapp[x][y];
48                 ans[s]++;
49                 if(step+1<speed[s]) que.push(N(nx,ny,step+1,turn,s));
50                 else que.push(N(nx,ny,0,turn+1,s));
51             }
52         }
53 /*        for(int i=0;i<n;i++)
54         {
55         for(int j=0;j<m;j++) cout<<mapp[i][j];
56         cout<<endl;
57         }
58 */    }
59 }
60
61 int main()
62 {
63 //    freopen("in.txt","r",stdin);
64 //    freopen("out.txt","w",stdout);
65
66     int p;
67     cin>>n>>m>>p;
68     for(int i=1;i<=p;i++) cin>>speed[i];
69     for(int i=0;i<n;i++)
70     for(int j=0;j<m;j++)
71     {
72         cin>>mapp[i][j];
73         if(mapp[i][j]!=‘#‘&&mapp[i][j]!=‘.‘)
74         {
75             int s=mapp[i][j]-‘1‘+1;
76             que.push(N(i,j,0,0,s)),ans[s]++;
77         }
78     }
79 /*    while(que.size())
80     {
81         N p=que.top();que.pop();
82         printf("(%d,%d) step:%d turn:%d i:%d\n",p.a,p.b,p.c,p.d,p.e);
83     }
84 */
85     bfs();
86     for(int i=1;i<p;i++) cout<<ans[i]<<‘ ‘;
87     cout<<ans[p]<<endl;
88     return 0;
89 }

原文地址:https://www.cnblogs.com/VBEL/p/10668054.html

时间: 2024-10-11 17:37:36

Codeforces Round #533 (Div. 2) ABCD 题解的相关文章

Codeforces Round #Pi (Div. 2) (ABCD题解)

比赛链接:http://codeforces.com/contest/567 听说Round #Pi的意思是Round #314... A. Lineland Mail time limit per test:3 seconds memory limit per test:256 megabytes All cities of Lineland are located on the Ox coordinate axis. Thus, each city is associated with it

Codeforces Round #250 (Div. 2) (ABCD题解)

比赛链接:http://codeforces.com/contest/433 A. Kitahara Haruki's Gift time limit per test:1 second memory limit per test:256 megabytes Kitahara Haruki has bought n apples for Touma Kazusa and Ogiso Setsuna. Now he wants to divide all the apples between th

Codeforces Round #249 (Div. 2) (ABCD题解)

比赛链接:http://codeforces.com/contest/435 A. Queue on Bus Stop time limit per test:1 second memory limit per test:256 megabytes It's that time of the year when the Russians flood their countryside summer cottages (dachas) and the bus stop has a lot of p

Codeforces Round #313 (Div. 2) (ABCD题解)

比赛链接:http://codeforces.com/contest/560 水笔场... A. Currency System in Geraldion time limit per test:2 seconds memory limit per test:256 megabytes A magic island Geraldion, where Gerald lives, has its own currency system. It uses banknotes of several va

Codeforces Round #302 (Div. 2) (ABCD题解)

比赛链接:http://codeforces.com/contest/544 A. Set of Strings time limit per test:1 second memory limit per test:256 megabytes You are given a string q. A sequence of k strings s1,?s2,?...,?sk is called beautiful, if the concatenation of these strings is

Codeforces Round #258 (Div. 2)[ABCD]

Codeforces Round #258 (Div. 2)[ABCD] ACM 题目地址:Codeforces Round #258 (Div. 2) A - Game With Sticks 题意: Akshat and Malvika两人玩一个游戏,横竖n,m根木棒排成#型,每次取走一个交点,交点相关的横竖两条木棒要去掉,Akshat先手,给出n,m问谁赢. 分析: 水题,很明显不管拿掉哪个点剩下的都是(n-1,m-1),最后状态是(0,x)或(x,0),也就是拿了min(n,m)-1次,

# Codeforces Round #529(Div.3)个人题解

Codeforces Round #529(Div.3)个人题解 前言: 闲来无事补了前天的cf,想着最近刷题有点点怠惰,就直接一场cf一场cf的刷算了,以后的题解也都会以每场的形式写出来 A. Repeating Cipher 传送门 题意:第一个字母写一次,第二个字母写两次,依次递推,求原字符串是什么 题解:1.2.3.4,非常明显的d=1的等差数列,所以预处理一个等差数列直接取等差数列的每一项即可 代码: #include<bits/stdc++.h> using namespace s

Codeforces Round #354 (Div. 2) ABCD

Codeforces Round #354 (Div. 2) Problems # Name     A Nicholas and Permutation standard input/output 1 s, 256 MB    x3384 B Pyramid of Glasses standard input/output 1 s, 256 MB    x1462 C Vasya and String standard input/output 1 s, 256 MB    x1393 D T

Codeforces Round #531 (Div. 3) ABCDE题解

Codeforces Round #531 (Div. 3) 题目总链接:https://codeforces.com/contest/1102 A. Integer Sequence Dividing 题意: 给一个数n,然后要求你把1,2.....n分为两个集合,使得两个集合里面元素的和的差的绝对值最小. 题解: 分析可以发现,当n%4==0 或者 n%3==0,答案为0:其余答案为1.之后输出一下就好了. 代码如下: #include <bits/stdc++.h> using name