2021LZYZ元宵欢乐赛 の 题解
本场比赛共 题,总体感觉难度适中。
难度排序:洛克王国 < 二元运算符表达式 < 餐桌安排 算数问题 < 零食 < 卡牌游戏 < 序列 < 编辑器
题解将按照难度升序进行讲解。
特别感谢 ahawzlc 出色地完成了验题任务,并且提供了两道数据结构题 orz
A. zz 的洛克王国:
水题。
这道题我们只需要记录一下 (当前血量) , (初始血量, (已扣除的血量),进行简单的逻辑判断即可。
注意多组数据。
std:
#include <bits/stdc++.h>
using namespace std;
inline int read()
{
int x = 0, f = 1;
char ch = getchar();
while (!isdigit(ch))
{
if (ch == '-')
f = -1;
ch = getchar();
}
while (isdigit(ch))
{
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
int main()
{
int T = read();
while (T--)
{
int h = read();
int n = read();
int sum = 0;
int od = h;//初始血量。
bool flag = true;//表示是否存活。
for (int i = 1; i <= n; i++)
{
int x = read();
h -= x;
sum += x;
if (h <= 0)
{
if (flag)//这里用判断而非break因为后续读入不会结束,break会导致输入异常,加一层判断可以避免反复去世(
puts("NO");
flag = false;
}
if (sum % 4 == 0)//先扣血后加血
{
h = od;
sum = 0;
}
}
if (flag)
puts("YES");
}
return 0;
}B. zz 的二元运算符表达式
简单的字符串题。
你只需要会插入空格就行了(
下面有两种 std:
string 插入版:
#include <bits/stdc++.h>
using namespace std;
bool pd(char x)
{
if (x == '+' || x == '-' || x == '*' || x == '/')
return true;
return false;
}
int main()
{
string a;
cin >> a;
int len = a.length();
string k = " ";
for (int i = 0; i < len; i++)
{
if (pd(a[i]))
a.insert(i, k), a.insert(i + 2, k), i += 2, len += 2;
}
cout << a << endl;
return 0;
}其中 的作用是在字符串的第 个字符前插入一个字符串 ,是一个字符串题中很有用的函数。std 中定义了 为只有一个空格的串。
char 数组模拟版:
#include <bits/stdc++.h>
using namespace std;
bool pd(char x)
{
if (x == '+' || x == '-' || x == '*' || x == '/')
return true;
return false;
}
int main()
{
string str;
cin >> str;
int len = str.length();
for (int i = 0; i < len; i++)
{
if (pd(str[i]))
putchar(' '), putchar(str[i]), putchar(' ');
else
putchar(str[i]);
}
return 0;
}其中 函数会返回一个字符串的实际长度 。是一个几乎字符串题都会用到的函数。本题算作一个字符串入门题(
C. zz 的算数问题
原题来自 CF1406B
偶然乘兴,登录CF,就打了 ,这道题是我 FST(Failed System Test) 的一道。当场掉分。
这题是一个很好的分类讨论题。
思路:
要想让 个数的乘积最大,在考虑负数的情况下,有以下几种情况:
- 最大的 个数乘起来。
- 最小的 个负数和最大的 个乘起来。
- 最小的 个负数和最大的数乘起来。
只要负数足够多,负数的个数就只能是偶数个,要不然乘起来是负的还干什么。
这题要注意的问题:
- 开 。 理由:, 的最大到 大概 多一点,安详地去世。
- 用排序的方法取最大或最小
- 多组测试数据。
std:
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 5;
inline int read()
{
int x = 0, f = 1;
char ch = getchar();
while (!isdigit(ch))
{
if (ch == '-')
f = -1;
ch = getchar();
}
while (isdigit(ch))
x = (x << 1) + (x << 3) + (ch ^ '0'), ch = getchar();
return x * f;
}
int T, n, a[N], ans1, ans2, ans3, ans = -0x3f3f3f3f3f3f3f3f;//注意这里ans的初始值一定要赋很小,我的数据没卡你也许别的出题人就会了(
signed main()
{
T = read();
while (T--)
{
n = read();
for (int i = 1; i <= n; i++)
a[i] = read();
sort(a + 1, a + n + 1); //排序
ans1 = a[n] * a[n - 1] * a[n - 2] * a[n - 3] * a[n - 4]; //5个最大数
ans2 = a[1] * a[2] * a[n] * a[n - 1] * a[n - 2]; //2个最小数+3个最大数
ans3 = a[1] * a[2] * a[3] * a[4] * a[n]; //4个最小数+1个最大数
ans = max(ans1, max(ans2, ans3)); //取最大
printf("%lld\n", ans); //输出,用lld
}
return 0;
}D. zz 的餐桌安排
原题来自 CF22B,,CF 是一个很好的编程竞赛网站,只不过绝大部分比赛在 之后,所以出题人也几乎不打这玩意。
题意:给定一个 的 矩阵,求只包含 的 矩形 使其周长最大。
发现 ,果断想到暴力。(数据范围是算法的一个很好的提示)
我们只需要枚举这个矩形的四个端点。每次判断四条边是否都是 即可。
我们直接看代码,四重循环不是很好讲,但是很好写。
#include <bits/stdc++.h>
using namespace std;
bool a[26][26];
inline bool Check(int x1, int y1, int x2, int y2)
{
for (int i = x1; i <= x2; i++)
{
for (int j = y1; j <= y2; j++)
{
if (a[i][j] == true) //判断是否为1
return false;
}
}
return true;
}
int main()
{
int n, m;
int maxx = -1;
scanf("%d%d", &n, &m);
char x;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
{
cin >> x;
if (x == '1')
a[i][j] = true; //存矩阵。
else
a[i][j] = false;
}
//下面四重循环暴力大法好
for (int i = 1; i <= n; i++) //枚举矩形左上端点的x坐标
{
for (int j = 1; j <= m; j++) //枚举矩形左上端点的y坐标
{
for (int k = 1; k <= n; k++) //枚举矩形右下端点的x坐标
{
for (int l = 1; l <= m; l++) //枚举矩形右下端点的y坐标
{
if (Check(i, j, k, l) && i <= k && j <= l) //判断没有1而且是矩形
maxx = max(maxx, ((k - i + 1) + (l - j + 1)) * 2);
}
}
}
}
printf("%d\n", maxx); //输出答案
return 0;
}本题题解也可见我的洛谷 blog:题解 CF22B 【Bargaining Table】
E. zz 与零食
从这题开始有点知识含量了(
本题用到的数据结构:栈()。
栈就是一个支持插入和弹出元素的数组。但是不可以使用数组下标进行访问元素。
在 C++ 中,栈的定义如下:
stack<int> a;表示定义了一个 类型的栈,名称是 。
栈的用法(可以点开看一下)
本题用到的只有 四个操作。( 为压栈即插入。 则为弹栈即将栈顶删除。 则取出栈顶元素,返回一个数。 返回一个 ,表示栈是否为空,如果对于一个空栈进行 则会发生错误。)
问题来了,这道题的放入和吃掉就是 和 ,但是这个 显得尤其恶心。如果将栈中所有的元素一一出栈在进行比较,显然复杂度是 的。这个时候,我们可以另外开一个栈,叫 ,来表示最大值。这样每次求最大值时,我们可以直接调用 取出 的栈顶即为当前的最大值。复杂度为
发现了一个问题:当原数据的栈被 之后,如何判断 是否要 ?
很简单:只要每次 时判断一下要 的值是否等于 的栈顶即可。
具体实现可以看代码,算作一道 的入门题。
std:
#include <bits/stdc++.h>
using namespace std;
inline int read()
{
int x = 0, f = 1;
char ch = getchar();
while (!isdigit(ch))
{
if (ch == '-')
f = -1;
ch = getchar();
}
while (isdigit(ch))
x = (x << 1) + (x << 3) + (ch ^ '0'), ch = getchar();
return x * f;
}
stack<int> data;//原数据
stack<int> Max;//最大值
int getmax()
{
if (!Max.empty())
return Max.top();
return 0;//如果没有则返回0!
}
void push(int x)
{
if (Max.empty())
Max.push(x);
else if (x >= getmax())
Max.push(x);//如果当前要 push 的值大于当前 Max 的栈顶或者 Max 栈为空,则入 Max 栈
data.push(x);
}
void pop()
{
if (!data.empty())
{
int x = data.top();
data.pop();
if (x == getmax())
Max.pop();
}
}
int main()
{
int m = read();
for (int i = 1; i <= m; i++)
{
int opt = read();
if (opt == 1)
{
int x = read();
push(x);
}
if (opt == 2)
{
pop();
}
if (opt == 3)
{
int maxn = getmax();
printf("%d\n", maxn);
}
}
return 0;
}F. zz 的卡牌游戏
zz 的心血之一。
对于 的数据,你甚至可以排个序选前 个加起来即为答案。
对于 的数据:
发现 直接狂喜,上暴搜。(这场比赛暴搜分很多的)
复杂度大概是
实际上跑不到上限,很稳健。
暴搜的思路就是一张一张填卡组,等到卡组到了 张的时候就将各种战斗力加起来取 即可。
本题比较考验码力以及思维的清晰程度。
std:
#include <bits/stdc++.h>
using namespace std;
const int N = 101;
inline int read() //基本快读
{
int x = 0, f = 1;
char ch = getchar();
while (!isdigit(ch))
{
if (ch == '-')
f = -1;
ch = getchar();
}
while (isdigit(ch))
x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
return x * f;
}
int n, m, k;
int v[N], tg[N][N]; //单独和配合的战斗力
int ans; //答案
int deck[N]; //最终卡组
int now[N]; //当前的卡组
bool vis[N]; //当前选择情况
void check() //更新答案
{
int maxx = 0;
for (int i = 1; i <= m; i++)
{
maxx += v[now[i]];
for (int j = 1; j <= n; j++)
if (vis[j])
maxx += tg[now[i]][j]; //加上额外战斗力
}
if (maxx > ans)
{
ans = maxx;
for (int i = 1; i <= m; i++)
deck[i] = now[i]; //复制卡组
}
}
void dfs(int cnt)
{
if (cnt == m + 1 || cnt == n + 1)
check();
else
{
for (int i = now[cnt - 1]; i <= n; i++)
{
if (!vis[i])
{
vis[i] = true;
now[cnt] = i;
dfs(cnt + 1);
vis[i] = false;
}
}
}
}
int main()
{
n = read();
for (int i = 1; i <= n; i++)
v[i] = read();
k = read();
for (int i = 1; i <= k; i++)
{
int a = read(), b = read(), V = read();
tg[a][b] += V; //注意+=,因为有重复
}
m = read();
dfs(1); //慢慢填卡组
cout << ans << '\n';
sort(deck + 1, deck + m + 1); //最后升序输出
for (int i = 1; i <= m; i++)
cout << deck[i] << ' ';
return 0;
}G. zz 的序列
不用你们骂,我先骂,这题数据我出了 遍,十分煎熬。
zz 的心血之二。
标程换了 个,面向数据编程大成功。
对于 的数据, ,这是应杨老师的要求写的,给亿点部分分。
直接暴搜所有排列,一一比较即可。
程序中的 next_permutation 函数可以生成一个数组的下一个排列,应用不多,但是在全排列题目或者没有思路搞全排列的时候很有作用。
50pts:
#include <bits/stdc++.h>
using namespace std;
inline int read()
{
int x = 0, f = 1;
char ch = getchar();
while (!isdigit(ch))
{
if (ch == '-')
f = -1;
ch = getchar();
}
while (isdigit(ch))
x = (x << 1) + (x << 3) + (ch ^ '0'), ch = getchar();
return x * f;
}
int a[10005];
int main()
{
int T = read();
while (T--)
{
int n = read();
for (int i = 1; i <= n; i++)
a[i] = read();
int maxx = 0;
sort(a + 1, a + n + 1);
while (next_permutation(a + 1, a + n + 1))
{
int ans = 0;
for (int i = 1; i < n; i++)
ans += abs(a[i] - a[i + 1]);
maxx = max(maxx, ans);
}
cout << maxx << endl;
}
return 0;
}正解:
找规律题。
我们发现:要想求得最大的和一定是最大的和最小的放在一起。剩下的一大一小向外扩展即可。
模拟一下样例的 和 :
:
首先一大一小:
在 左边加上 , 的右边加上 :
注意最后一个数的判断:发现 放在 的右边比放在 的左边更优,于是:
这样答案为: ,可以证明是最优解。
:
首先一大一小:
在 的左边加上 ,在 的右边加上 :
在 的左边加上 ,在 的右边加上 :
在 的左边加上 ,在 的右边加上 :
此时的答案为: 。
总结思路:
先将数组排序,取出数组中的最大值和最小值,并计算出差值,然后再取出剩余数组中的最大值和最小值,将此时的最小值放在上一次最大值的旁边,此时的最大值放在上一次最小值的旁边,依次类推。
对于 为偶数的情况,就这样完事。
对于 为奇数的情况,需要判断最后一个数放在哪一端,取 即可。
std 有点二分风味,但是其本质是从两头往中间算,与二分无关。
std:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 5;
#define ll long long
int a[N];
inline int read()
{
int x = 0, f = 1;
char ch = getchar();
while (!isdigit(ch))
{
if (ch == '-')
f = -1;
ch = getchar();
}
while (isdigit(ch))
x = (x << 1) + (x << 3) + (ch ^ '0'), ch = getchar();
return x * f;
}
int main()
{
int T = read();
while (T--)
{
int n = read();
for (int i = 1; i <= n; i++)
a[i] = read();
sort(a + 1, a + n + 1);
int Max = a[n], Min = a[1], l = 2, r = n - 1;
ll sum = Max - Min;
while (l < r)
{
sum += (ll)(Max - a[l]) + (a[r] - Min);
Min = a[l++];
Max = a[r--];
}
sum += (ll)max(Max - a[l], a[r] - Min);
printf("%lld\n", sum);
}
return 0;
}H. zz的编辑器
思路:对顶栈。
根据 和 经典操作,想到用栈来处理。
审题发现 和 操作,使得一个栈无法处理本题。考虑使用对顶栈。对顶栈,顾名思义,就是定义 两个栈,从 A 中弹出的数值将被加入 B 中。
结合询问,可以得出以下一种做法:
定义两个栈 A、B,两个数组 表示前缀和, 表示最大前缀和。
- 对于 ,插入 到栈 A 中。同时更新
sum[top]=sum[top-1]+A[top];f[top]=max(f[top-1],sum[top]); - 对于 ,弹出栈 A 的栈顶。
- 对于 ,弹出栈 A 的栈顶,将其加入栈 B 中。
- 对于 ,弹出栈 B 的栈顶,将其加入栈 A 中。同时更新
sum[top]=sum[top-1]+A[top];f[top]=max(f[top-1],sum[top]); - 对于 ,输出 。
代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
inline int read()
{
int x = 0, f = 1;
char ch = getchar();
while (!isdigit(ch))
{
if (ch == '-')
f = -1;
ch = getchar();
}
while (isdigit(ch))
x = (x << 1) + (x << 3) + (ch ^ '0'), ch = getchar();
return x * f;
}
int f[N];
int main()
{
int m = read();
f[0] = -999999999;
int sum = 0;
stack<int> a, b;
for (int i = 1; i <= m; i++)
{
char opt;
cin >> opt;
if (opt == 'I')
{
int x = read();
a.push(x);
sum += x;
int siz = a.size();
f[siz] = max(sum, f[siz - 1]);
}
else if (opt == 'D')
{
if (a.size())
{
sum -= a.top();
a.pop();
}
}
else if (opt == 'L')
{
if (a.size())
{
sum -= a.top();
b.push(a.top());
a.pop();
}
}
else if (opt == 'R')
{
if (b.size())
{
a.push(b.top());
sum += b.top();
int siz = a.size();
f[siz] = max(sum, f[siz - 1]);
b.pop();
}
}
else if (opt == 'Q')
{
int k = read();
printf("%d\n", f[k]);
}
}
return 0;
}