A - Q老师与石头剪刀布
题目
每一个大人曾经都是一个小孩,Q老师 也一样。
为了回忆童年,Q老师 和 Monika 玩起了石头剪刀布的游戏,游戏一共 n 轮。无所不知的 Q老师 知道每一轮 Monika 的出招,然而作为限制, Q老师 在这 n 轮游戏中必须恰好出 a 次石头,b 次布和 c 次剪刀。
如果 Q老师 赢了 Monika n/2(上取整) 次,那么 Q老师就赢得了这场游戏,否则 Q老师 就输啦!
Q老师非常想赢,他想知道能否可以赢得这场游戏,如果可以的话,Q老师希望你能告诉他一种可以赢的出招顺序,任意一种都可以。
Input
第一行一个整数 t(1 ≤ t ≤ 100)表示测试数据组数。然后接下来的 t 组数据,每一组都有三个整数:
第一行一个整数 n(1 ≤ n ≤ 100)
第二行包含三个整数 a, b, c(0 ≤ a, b, c ≤ n)。保证 a+b+c=n
第三行包含一个长度为 n 的字符串 s,字符串 s 由且仅由 ‘R’, ‘P’, ‘S’ 这三个字母组成。第 i 个字母 s[i] 表示 Monika 在第 i 轮的出招。字母 ‘R’ 表示石头,字母 ‘P’ 表示布,字母 ‘S’ 表示剪刀
Output
对于每组数据:
如果 Q老师 不能赢,则在第一行输出 “NO”(不含引号)
否则在第一行输出 “YES”(不含引号),在第二行输出 Q老师 的出招序列 t。要求 t 的长度为 n 且仅由 ‘R’, ‘P’, ‘S’ 这三个字母构成。t 中需要正好包含 a 个 ‘R’,b 个 ‘P’ 和 c 个 ‘S’
“YES”/"NO"是大小写不敏感的,但是 ‘R’, ‘P’, ‘S’ 是大小写敏感的。
Example Input
2
3
1 1 1
RPS
3
3 0 0
RPS
Output
YES
PSR
NO
思路
遍历输入的字符串,根据a,b,c的值尽可能的多赢,并对赢的位置进行标记,记录赢的总次数。
若总次数大于n/2(上取整) 次,则根据标记输出每一局的结果,若有标记,输出赢的情况,否则,根据abc判断剩余的出招情况,任选未使用的一种出拳即可。
若小于n/2(上取整)次,则直接输出NO。
代码
#include <iostream>
#include <cmath>
#include <string.h>
#include <cstring>
#include <string>
using namespace std;
int t, a, b, c, res;
string s;
int ans[105];
double n;
void solve()
{
for (int i = 0; i < n; i++)
{
if (s[i] == 'R'&&b > 0)
{
b--, res++;
ans[i] = 2;
}
else if (s[i] == 'P'&&c > 0)
{
c--, res++;
ans[i] = 3;
}
else if (s[i] == 'S'&&a > 0)
{
a--, res++;
ans[i] = 1;
}
}
}
void print()
{
if (res >= ceil(n / 2))
{
printf("YES\n");
for (int i = 0; i < n; i++)
{
if (ans[i] == 0)
{
if (a > 0)
{
printf("R");
a--;
}
else if (b > 0)
{
printf("P");
b--;
}
else if (c > 0)
{
printf("S");
c--;
}
}
if (ans[i] == 1)
printf("R");
if (ans[i] == 2)
printf("P");
if (ans[i] == 3)
printf("S");
}
cout << endl;
}
else
printf("NO\n");
}
int main()
{
scanf("%d", &t);
while (t--)
{
res = 0;
memset(ans, 0, sizeof(ans));
cin >> n;
scanf("%d %d %d", &a, &b, &c);
cin >> s;
solve();
print();
}
return 0;
}
B - Q老师与十字叉
题目
Q老师 得到一张 n 行 m 列的网格图,上面每一个格子要么是白色的要么是黑色的。
Q老师认为失去了 十字叉 的网格图莫得灵魂. 一个十字叉可以用一个数对 x 和 y 来表示, 其中 1 ≤ x ≤ n 并且 1 ≤ y ≤ m, 满足在第 x 行中的所有格子以及在第 y 列的 所有格子都是黑色的
例如下面这5个网格图里都包含十字叉
第四个图有四个十字叉,分别在 (1, 3), (1, 5), (3, 3) 和 (3, 5).
下面的图里没有十字叉
Q老师 得到了一桶黑颜料,他想为这个网格图注入灵魂。 Q老师 每分钟可以选择一个白色的格子并且把它涂黑。现在他想知道要完成这个工作,最少需要几分钟?
Input
第一行包含一个整数 q (1 ≤ q ≤ 5 * 10^4) — 表示测试组数
对于每组数据:
第一行有两个整数 n 和 m (1 ≤ n, m ≤ 5 * 10^4, n * m ≤ 4 * 10^5) — 表示网格图的行数和列数
接下来的 n 行中每一行包含 m 个字符 — ‘.’ 表示这个格子是白色的, ‘*’ 表示这个格子是黑色的
保证 q 组数据中 n 的总和不超过 5 * 10^4, n*m 的总和不超过 4 * 10^5
Output
答案输出 q 行, 第 i 行包含一个整数 — 表示第 i 组数据的答案
Example Input
9
5 5
..*..
..*..
*****
..*..
..*..
3 4
****
.*..
.*..
4 3
***
*..
*..
*..
5 5
*****
*.*.*
*****
..*.*
..***
1 4
****
5 5
.....
..*..
.***.
..*..
.....
5 3
...
.*.
.*.
***
.*.
3 3
.*.
*.*
.*.
4 4
*.**
....
*.**
*.**
Output
0
0
0
0
0
4
1
1
2
思路
遍历网格图的每一个位置,对每一个位置求出它要组成十字叉所需要的方格数字,打上标记,遇到0则直接跳出循环输出即可,如果没有0则记录最小需要补全方格数的输出。
代码
#include <iostream>
#include <cmath>
#include <string.h>
#include <cstring>
#include <string>
using namespace std;
int q, n, m, res;
const int maxn = 4e5+5;
char pic[maxn];
int col[maxn],row[maxn];
void solve()
{
for(int i=0;i<n;i++)
{
row[i]=0;
for(int j=i*m;j<(i+1)*m;j++)
if(pic[j]=='.')
row[i]++;
}
for(int i=0;i<m;i++)
{
col[i]=0;
for(int j=i;j<n*m;j+=m)
if(pic[j]=='.')
col[i]++;
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
int temp=row[i]+col[j];
if(pic[i*m+j]=='.')
temp--;
res=min(res,temp);
}
}
printf("%d\n",res);
}
int main()
{
scanf("%d", &q);
while (q--)
{
res = 1e9;
scanf("%d %d", &n, &m);
for (int i = 0; i < n; i++)
scanf("%s",pic+i*m);
solve();
}
return 0;
}
C - Q老师的考验
题目
Q老师 对数列有一种非同一般的热爱,尤其是优美的斐波那契数列。
这一天,Q老师 为了增强大家对于斐波那契数列的理解,决定在斐波那契的基础上创建一个新的数列 f(x) 来考一考大家。数列 f(x) 定义如下:
当 x < 10 时,f(x) = x;
当 x ≥ 10 时,f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10),ai 只能为 0 或 1。
Q老师 将给定 a0~a9,以及两个正整数 k m,询问 f(k) % m 的数值大小。
聪明的你能通过 Q老师 的考验吗?
Input
输出文件包含多组测试用例,每组测试用例格式如下:
第一行给定两个正整数 k m。(k < 2e9, m < 1e5)
第二行给定十个整数,分别表示 a0~a9。
Output
对于每一组测试用例输出一行,表示 f(k) % m 的数值大小。
Sample Input
10 9999
1 1 1 1 1 1 1 1 1 1
20 500
1 0 1 0 1 0 1 0 1 0
Sample Output
45
104
思路
构造矩阵乘法的递推公式,使用矩阵快速幂来优化矩阵幂运算,需要注意的是首项的选择,是从第9项开始计算的。
代码
#include <iostream>
#include <string.h>
using namespace std;
int k, m;
const int N = 10;
struct Matrix
{
int x[N][N];
Matrix operator*(const Matrix& t) const
{
Matrix ret;
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
for (int k = 0; k < N; k++)
{
ret.x[i][j] += (x[i][k] * t.x[k][j]) % m;
ret.x[i][j] %= m;
}
}
}
return ret;
}
Matrix()
{
memset(x, 0, sizeof(x));
}
Matrix(const Matrix& t)
{
memcpy(x, t.x, sizeof(x));
}
};
Matrix quick_pow(Matrix a, int x)
{
Matrix ans;
for (int i = 0; i < N; i++)
ans.x[i][i] = 1;
while (x)
{
if (x & 1)
ans = ans * a;
a = a * a;
x >>= 1;
}
return ans;
}
int main()
{
while (scanf("%d %d", &k, &m) != EOF)
{
if (k <= 9)
{
printf("%d\n", k%m);
continue;
}
Matrix temp;
for (int i = 0; i < N; i++)
scanf("%d", &temp.x[0][i]);
for (int i = 1; i < N; i++)
temp.x[i][i - 1] = 1;
temp = quick_pow(temp, k - 9);
int ans = 0;
for (int i = 0; i < 10; i++)
{
ans += (temp.x[0][i] * (9 - i)) % m;
ans %= m;
}
printf("%d\n", ans);
}
return 0;
}