A. String Generation
水题,先输出m个a后在由c’b,a的顺序倒序输出即可。
#include<bits/stdc++.h>
using namespace std;
#define IO() ios::sync_with_stdio(false);\
cin.tie(NULL);\
cout.tie(NULL);
#define MULTICASE int _;cin >> _;while(_ --)
main(){
IO();
MULTICASE
{
int n, m;
cin >> n >> m;
cout << string(m, 'a');
char c = 'c';
for(int i = m + 1;i <= n;i ++)
{
cout << c ;
c = (c - 1);
if(c < 'a')c = 'c';
}
cout << endl;
}
}
B. Find the Spruce
这道题我简单的方法没想到,想到一个
O
(
n
3
)
O(n^3)
O(n3)的解法,大概要计算
50
0
3
=
1.25
∗
1
0
8
500^3=1.25*10^8
5003=1.25∗108次
对于每一行的输入,我们给连续的一段’*'染上相同的颜色,如下图
我们可以看到,一个合格的“spruce”图形,从第一行开始,其的最左边和最右边必须是同一种颜色且都不为零,于是,我们就可以据此编程。
#include<bits/stdc++.h>
using namespace std;
#define IO() ios::sync_with_stdio(false);\
cin.tie(NULL);\
cout.tie(NULL);
#define MULTICASE int _;cin >> _;while(_ --)
const int N = 505;
char s[N][N];
int ok[N][N];
int n, m;
int check(int x, int y)
{
int ans = 1;
int pos = 1;
for(int i = x + 1;i <= n;i ++)
{
if(y - pos <= 0 || y + pos > m)break;
if(ok[i][y - pos] != ok[i][y + pos] || ok[i][y - pos] == 0 || ok[i][y + pos] == 0)return ans;
ans ++;pos ++;
}
return ans;
}
main(){
IO();
MULTICASE
{
cin >> n >> m;
memset(ok, 0, sizeof ok);
for(int i = 1;i <= n;i ++)
cin >> (s[i] + 1);
int ans = 0;
for(int i = 1;i <= n;i ++)
{
int state = 0;
for(int j = 1;j <= m;j ++)
{
if(s[i][j] == '*')
{
state ++;
while(s[i][j] == '*')
ok[i][j] = state, j ++;
}
}
}
for(int i = 1;i <= n;i ++)
{
for(int j = 1;j <= m;j ++)
{
if(s[i][j] == '*')
ans += check(i, j);
}
}
cout << ans << '\n';
}
}
C. Random Events
自后向前找出最后无序的位置,并记为
x
x
x,后输入位置与概率
p
p
p,若当前输入的数
≥
x
≥x
≥x,则将总概率乘于
(
1
−
p
)
(1-p)
(1−p),最后让总概率
p
a
l
l
=
1
−
p
a
l
l
p_{all}=1-p_{all}
pall=1−pall即可。
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define show(x) cerr << #x << " = " << x << '\n'
#define IO() ios::sync_with_stdio(false);\
cin.tie(NULL);\
cout.tie(NULL);
#define MULTICASE int _;cin >> _;while(_ --)
const int N = 100100;
int c[N];
main(){
IO();
MULTICASE
{
int n, m;
cin >> n >> m;
for(int i = 1;i <= n;i ++)
cin >> c[i];
int x;
for(x = n;x;x --)
{
if(c[x] != x)break;
}
double ans = 1;
for(int i = 1;i <= m;i ++)
{
int a;
double b;
cin >> a >> b;
if(a >= x)
ans *= (1 - b);
}
if(x == 0)
{
cout << "1.000000000" << endl;
continue;
}
ans = 1 - ans;
cout << fixed << setprecision(9) << ans << endl;
}
}