img

牛客周赛 Round 1

A

按要求输出

掌握string(n,'*)

贴代码的

观察+string函数即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <bits/stdc++.h>
using namespace std;

int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
for (int i = 0; i < n * 3; i += 1) {
cout << string(n, '*');
cout << string(2 * n, '.');
cout << string(n, '*');
cout << "\n";
}
for (int i = 1; i <= n; i += 1) {
cout << string(i, '.');
cout << string(n, '*');
cout << string(2 * (n - i), '.');
cout << string(n, '*');
cout << string(i, '.');
cout << "\n";
}
return 0;
}

B

会用map即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//by Totoro
//直接map即可
//一种思路是用0和1代表颜色
//然后就是pair map的使用
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define frep(i,a,n) for(int i=a;i>=n;i--)
#define int long long
const int N=1e5+100;
#define PII pair<int,int>
map<PII,int>mp;
int a[N];
int b[N];
signed main()
{
int n;
cin>>n;
rep(i,1,n)
{
cin>>a[i];
}
rep(i,1,n)
{
char s;
cin>>s;
if(s=='B')
{
mp[{1,a[i]}]++;
}
else
{
mp[{0,a[i]}]++;
}
}
int ans=0;
for(auto& [P,cnt]:mp)
{
auto [x,y]=P;
ans+=mp[{1-x,y}]*mp[{x,y}];
}
cout<<ans/2;
return 0;
}

C

思维题目

考虑两种情况

  • 1010101

  • 0101010

    直接用ans1,ans2存贡献开始计算

    一个细节是1多肯定1放第一个

    0多0放第一个

    不然取最小

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//by Totoro
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define frep(i,a,n) for(int i=a;i>=n;i--)
#define int long long
const int N=1e5+100;
#define PII pair<int,int>
const int mod=1e9+7;
int dp[20];
int d[20];
signed main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
//取消同步流
string s;
cin>>s;
int n=s.length();
s=" "+s;
int cnt1=1,cnt2=2;
int cnt=0;
int ans1=0;
int ans2=0;
rep(i,1,n)
{
if(s[i]=='1')
{
cnt++;
ans1+=abs(cnt1-i);
ans2+=abs(cnt2-i);
cnt1+=2;
cnt2+=2;
}
}
if(cnt*2==n)
{
cout<<min(ans1,ans2);
}
else if(cnt*2>n)
{
cout<<ans1;
}
else
{
cout<<ans2;
}
return 0;
}

D

以取模为状态的dp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//by Totoro
//取模dp?
//状态dp
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define frep(i,a,n) for(int i=a;i>=n;i--)
#define int long long
const int N=1e5+100;
#define PII pair<int,int>
const int mod=1e9+7;
int dp[20];
int d[20];
signed main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
//取消同步流
string s;
cin>>s;
int n=s.length();
s=" "+s;
dp[0]=1;
//初始化
rep(i,1,n)
{
int x=s[i]-'0';
x%=9;
memcpy(d,dp,sizeof dp);
//上一层承接
rep(j,0,8)
{
int y=(j+x)%9;
dp[y]=(dp[y]+d[j])%mod;
//状态转移
}
}
cout<<dp[0]-1<<'\n';
return 0;
}
img