目录
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
求解思路
代码
#include<iostream>
#include<stdio.h>
using namespace std;
int a, b, c, n;
string str;
int state[105];
int coun;
void solve()
{
coun = 0;
for (int i = 0; i < 105; i++)
{
state[i] = 0;
}
for (int i = 0; i < n; i++)
{
if (str[i] == 'R' && b > 0)
{
b--; coun++;
state[i] = 1;//p=1
}
if (str[i] == 'P' && c > 0)
{
c--; coun++;
state[i] = 2;//s=2
}
if (str[i] == 'S' && a > 0)
{
a--; coun++;
state[i] = 3;//R=1
}
}
int tmp;
if (n % 2 > 0)
tmp = n / 2 + 1;
else
tmp = n / 2;
if (coun >= tmp)
{
cout << "YES" << endl;
for (int i = 0; i < n; i++)
{
if (state[i] == 0)
{
if (a > 0)
{
cout << 'R';
a--;
continue;
}
if (b > 0)
{
cout << 'P';
b--;
continue;
}
if (c > 0)
{
cout << 'S';
c--;
continue;
}
}
else if (state[i] == 1)
{
cout << 'P';
}
else if (state[i] == 2)
{
cout << 'S';
}
else if (state[i] == 3)
{
cout << 'R';
}
}
cout << endl;
}
else
{
cout << "NO" << endl;
}
}
int main()
{
int t;
cin >> t;
while (t--)
{
cin >> n >> a >> b >> c;
str.clear();
cin >> str;
solve();
}
}
B - Q老师与十字叉(必做)
题目要求
Q老师 得到一张 n 行 m 列的网格图,上面每一个格子要么是白色的要么是黑色的。
Q老师认为失去了 十字叉 的网格图莫得灵魂. 一个十字叉可以用一个数对 x 和 y 来表示, 其中 1 ≤ x ≤ n 并且 1 ≤ y ≤ m, 满足在第 x 行中的所有格子以及在第 y 列的 所有格子都是黑色的
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, nm 的总和不超过 4 * 10^5
Output
答案输出 q 行, 第 i 行包含一个整数 — 表示第 i 组数据的答案
求解思路
注意遍历查询vector用vector<int>::iterator re; re = find(gra[i].begin(), gra[i].end(), j);
手写for循环会TLE
代码
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<vector>
#define N 10
typedef long long ll;
using namespace std;
//B
int graphx[50005];
int graphy[50005];
//int state[50005];
vector<int> gra[50005];
int main()
{
int q;
cin >> q;
while (q--)
{
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
graphx[i] = 0;
gra[i].clear();
}
for (int j = 1; j <= m; j++)
graphy[j] = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
{
char c;
cin >> c;
if (c == '.')
{
graphx[i]++;
graphy[j]++;
gra[i].push_back(j);
}
}
int con = 60000;
int tag = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
{
int temp = graphx[i] + graphy[j];
vector<int>::iterator re;
re = find(gra[i].begin(), gra[i].end(), j);
if (re != gra[i].end())
{
temp--;
}
/* for(int k=0;k<gra[i].size();k++)
{
if(gra[i][k]==j)
{
temp--;
}
}
*/
if (temp == 0)
{
tag = 1; break;
}
if (temp < con)
{
con = temp;
}
}
if (tag == 1)
cout << 0 << endl;
else
cout << con << endl;
}
}
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
求解思路
数据可能很大,需要在过程中不断求模
代码
#include<iostream>
#include<stdio.h>
#include<cstring>
#define N 10
typedef long long ll;
using namespace std;
ll k, m;
struct Matrix
{
ll 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++)
{
ret.x[i][j] = 0;
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 arr;
void quick_pow(Matrix a, int x)
{
while (x)
{
if (x & 1)
arr = a*arr;
a = a * a;
x >>= 1;
}
}
int main()
{
while (cin >> k)
{
Matrix ap;
cin >> m;
for (int i = 0; i <= 9; i++)
{
cin >> ap.x[0][i];
if (i < 9)
{
ap.x[i + 1][i] = 1;
}
}
for (int p = 0; p <= 9; p++)
{
for (int j = 0; j <= 9; j++)
arr.x[p][j] = 0;
}
int tmp = 9;
for (int i = 0; i <= 9; i++)
{
arr.x[i][0] = tmp;
tmp--;
}
if (k < 10)
{
cout << k % m << endl;
}
else
{
quick_pow(ap, k - 9);
cout << arr.x[0][0] % m << endl;
}
}
}
D - Q老师染砖(选做)
题目要求
衣食无忧的 Q老师 有一天突发奇想,想要去感受一下劳动人民的艰苦生活。
具体工作是这样的,有 N 块砖排成一排染色,每一块砖需要涂上红、蓝、绿、黄这 4 种颜色中的其中 1 种。且当这 N 块砖中红色和绿色的块数均为偶数时,染色效果最佳。
为了使工作效率更高,Q老师 想要知道一共有多少种方案可以使染色效果最佳,你能帮帮他吗?
Input
第一行为 T,代表数据组数。(1 ≤ T ≤ 100)
接下来 T 行每行包括一个数字 N,代表有 N 块砖。(1 ≤ N ≤ 1e9)
Output
输出满足条件的方案数,答案模 10007。
Sample Input
2
1
2
Sample Output
2
6
求解思路
矩阵快速幂优化dp,需要构建a和ans初始两个矩阵。关键在于A、B、C三种状态的确定。以及将状态转移方程的转化为矩阵。
代码
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<vector>
#define N 3
typedef long long ll;
using namespace std;
//D
struct Matrix
{
ll 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++)
{
ret.x[i][j] = 0;
for (int k = 0; k < N; k++)
{
ret.x[i][j] += x[i][k] * t.x[k][j] % 10007;
ret.x[i][j] %= 10007;
}
}
}
return ret;
}
Matrix() { memset(x, 0, sizeof x); }
Matrix(const Matrix& t) { memcpy(x, t.x, sizeof x); }
};
Matrix arr;
void quick_pow(Matrix a, int x)
{
while (x)
{
if (x & 1)
arr = a * arr;
a = a * a;
x >>= 1;
}
}
int main()
{
int t;
cin >> t;
while (t--)
{
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
arr.x[i][j] = 0;
arr.x[0][0] = 2;
arr.x[1][0] = 0;
arr.x[2][0] = 2;
int n;
cin >> n;
Matrix ap;
ap.x[0][0] = 2;
ap.x[0][2] = 1;
ap.x[1][1] = 2;
ap.x[1][2] = 1;
ap.x[2][0] = 2;
ap.x[2][1] = 2;
ap.x[2][2] = 2;
quick_pow(ap, n - 1);
cout << arr.x[0][0] % 10007 << endl;
}
}
E - Q老师度假(选做)
题目要求
忙碌了一个学期的 Q老师 决定奖励自己 N 天假期。
假期中不同的穿衣方式会有不同的快乐值。
已知 Q老师 一共有 M 件衬衫,且如果昨天穿的是衬衫 A,今天穿的是衬衫 B,则 Q老师 今天可以获得 f[A][B] 快乐值。
在 N 天假期结束后,Q老师 最多可以获得多少快乐值?
Input
输入文件包含多组测试样例,每组测试样例格式描述如下:
第一行给出两个整数 N M,分别代表假期长度与 Q老师 的衬衫总数。(2 ≤ N ≤ 100000, 1 ≤ M ≤ 100)
接下来 M 行,每行给出 M 个整数,其中第 i 行的第 j 个整数,表示 f[i][j]。(1 ≤ f[i][j] ≤ 1000000)
测试样例组数不会超过 10。
Output
每组测试样例输出一行,表示 Q老师 可以获得的最大快乐值。
Sample Input
3 2
0 1
1 0
4 3
1 2 3
1 2 3
1 2 3
Sample Output
2
9
求解思路
矩阵快速幂优化dp,关键在与重写矩阵乘法,把原来的相乘累加变成相加求max
代码
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<algorithm>
#include<vector>
#define N 100
typedef long long ll;
using namespace std;
//E
struct Matrix
{
ll 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++)
{
ret.x[i][j] = 0;
for (int k = 0; k < N; k++)
{
ret.x[i][j] = max(ret.x[i][j], x[i][k] + t.x[k][j]);
}
}
}
return ret;
}
Matrix() { memset(x, 0, sizeof x); }
Matrix(const Matrix& t) { memcpy(x, t.x, sizeof x); }
};
Matrix arr;
int n, m;
void quick_pow(Matrix a, int x)
{
while (x)
{
if (x & 1)
arr = a * arr;
a = a * a;
x >>= 1;
}
}
int main()
{
while (cin>>n)
{
cin >> m;
Matrix ap;
for (int i = 0; i < m; i++)
for (int j = 0; j < m; j++)
{
cin >> ap.x[j][i];
arr.x[i][j] = 0;
}
quick_pow(ap, n - 1);
ll maxx = 0;
for (int i = 0; i < m; i++)
{
maxx = max(maxx, arr.x[i][0]);
}
cout << maxx << endl;
}
}