跳到正文

个人记录

2021LZYZ寒假元宵欢乐赛 の 题解

zhzzhzzzz约 28 分钟
原文
文章信息与版本

文章信息

发布
2021年2月25日
收录
2026年2月12日
更新
2026年11月9日
篇幅
约 28 分钟

版本

  1. v2当前
  2. v1

2021LZYZ元宵欢乐赛 の 题解

本场比赛共 88 题,总体感觉难度适中。

难度排序:洛克王国 < 二元运算符表达式 < 餐桌安排 ≈\approx 算数问题 < 零食 < 卡牌游戏 < 序列 < 编辑器

题解将按照难度升序进行讲解。

特别感谢 ahawzlc 出色地完成了验题任务,并且提供了两道数据结构题 orz

A. zz 的洛克王国:

水题。

这道题我们只需要记录一下 nownow (当前血量) , odod (初始血量, sumsum (已扣除的血量),进行简单的逻辑判断即可。

注意多组数据。

std:

C++
#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 插入版:

C++
#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;
}

其中 a.insert(n,k)a.insert(n,k) 的作用是在字符串的第 ii 个字符前插入一个字符串 kk ,是一个字符串题中很有用的函数。std 中定义了 kk 为只有一个空格的串。

char 数组模拟版:

C++
#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;
}

其中 str.length()str.length() 函数会返回一个字符串的实际长度 −1-1 。是一个几乎字符串题都会用到的函数。本题算作一个字符串入门题(

C. zz 的算数问题

原题来自 CF1406B

偶然乘兴,登录CF,就打了 14061406 ,这道题是我 FST(Failed System Test) 的一道。当场掉分。

这题是一个很好的分类讨论题。

思路:

要想让 55 个数的乘积最大,在考虑负数的情况下,有以下几种情况:

  • 最大的 55 个数乘起来。
  • 最小的 22 个负数和最大的 33 个乘起来。
  • 最小的 44 个负数和最大的数乘起来。

只要负数足够多,负数的个数就只能是偶数个,要不然乘起来是负的还干什么。

这题要注意的问题:

  • 开 long longlong\space long 。 理由:(3×103)5=243×1015=2.43×1017(3\times10^3)^5=243\times10^{15}=2.43\times10^{17},intint 的最大到 21474836472147483647 大概 2×1092\times10^9 多一点,安详地去世。
  • 用排序的方法取最大或最小
  • 多组测试数据。

std:

C++
#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 是一个很好的编程竞赛网站,只不过绝大部分比赛在 22:3522:35 之后,所以出题人也几乎不打这玩意。

题意:给定一个 n×mn\times m 的 0101 矩阵,求只包含 00 的 矩形 使其周长最大。

发现 1≤n,m≤251\le n,m\le25 ,果断想到暴力。(数据范围是算法的一个很好的提示)

我们只需要枚举这个矩形的四个端点。每次判断四条边是否都是 00 即可。

我们直接看代码,四重循环不是很好讲,但是很好写。

C++
#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 与零食

从这题开始有点知识含量了(

本题用到的数据结构:栈(stackstack)。

栈就是一个支持插入和弹出元素的数组。但是不可以使用数组下标进行访问元素。

在 C++ 中,栈的定义如下:

C++
stack<int> a;

表示定义了一个 intint 类型的栈,名称是 aa 。

栈的用法(可以点开看一下)

本题用到的只有 push(),pop(),top(),empty()push(),pop(),top(),empty() 四个操作。(push()push() 为压栈即插入。pop()pop() 则为弹栈即将栈顶删除。 top()top() 则取出栈顶元素,返回一个数。empty()empty() 返回一个 boolbool ,表示栈是否为空,如果对于一个空栈进行 pop()pop() 则会发生错误。)

问题来了,这道题的放入和吃掉就是 push()push() 和 pop()pop() ,但是这个 getmaxgetmax 显得尤其恶心。如果将栈中所有的元素一一出栈在进行比较,显然复杂度是 Θ(n)\Theta(n) 的。这个时候,我们可以另外开一个栈,叫 MaxMax ,来表示最大值。这样每次求最大值时,我们可以直接调用 Max.top()Max.top() 取出 MaxMax 的栈顶即为当前的最大值。复杂度为 O(1)O(1)

发现了一个问题:当原数据的栈被 pop()pop() 之后,如何判断 MaxMax 是否要 poppop ?

很简单:只要每次 pop()pop() 时判断一下要 poppop 的值是否等于 MaxMax 的栈顶即可。

具体实现可以看代码,算作一道 stackstack 的入门题。

std:

C++
#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 的心血之一。

对于 30%30\% 的数据,你甚至可以排个序选前 mm 个加起来即为答案。

对于 100%100\% 的数据:

发现 m≤6,n≤30m\le 6,n\le30 直接狂喜,上暴搜。(这场比赛暴搜分很多的)

复杂度大概是

Θ(nm×C306)=30×6×30!6!×(30−6)!=180×593775≈108\Theta(nm\times C_{30}^{6})=30\times6\times\frac{30!}{6!\times(30-6)!}=180\times593775\approx10^8

实际上跑不到上限,很稳健。

暴搜的思路就是一张一张填卡组,等到卡组到了 mm 张的时候就将各种战斗力加起来取 max⁡\max 即可。

本题比较考验码力以及思维的清晰程度。

std:

C++
#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 的序列

不用你们骂,我先骂,这题数据我出了 66 遍,十分煎熬。

zz 的心血之二。

标程换了 33 个,面向数据编程大成功。

对于 50%50\% 的数据, T=1,n=5T=1,n=5 ,这是应杨老师的要求写的,给亿点部分分。

直接暴搜所有排列,一一比较即可。

程序中的 next_permutation 函数可以生成一个数组的下一个排列,应用不多,但是在全排列题目或者没有思路搞全排列的时候很有作用。

50pts:

C++
#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;
}

正解:

找规律题。

我们发现:要想求得最大的和一定是最大的和最小的放在一起。剩下的一大一小向外扩展即可。

模拟一下样例的 11 和 33 :

11:

首先一大一小:

7,17,1

在 77 左边加上 22 , 11 的右边加上 55 :

2,7,1,52,7,1,5

注意最后一个数的判断:发现 33 放在 55 的右边比放在 22 的左边更优,于是:

2,7,1,5,32,7,1,5,3

这样答案为: 5+6+4+2=175+6+4+2=17 ,可以证明是最优解。

33:

首先一大一小:

10,110,1

在 1010 的左边加上 11 ,在 11 的右边加上 99 :

1,10,1,91,10,1,9

在 11 的左边加上 66 ,在 99 的右边加上 22 :

6,1,10,1,9,26,1,10,1,9,2

在 66 的左边加上 33 ,在 22 的右边加上 44 :

3,6,1,10,1,9,2,43,6,1,10,1,9,2,4

此时的答案为:3+5+9+9+8+7+2=433+5+9+9+8+7+2=43 。

总结思路:

先将数组排序,取出数组中的最大值和最小值,并计算出差值,然后再取出剩余数组中的最大值和最小值,将此时的最小值放在上一次最大值的旁边,此时的最大值放在上一次最小值的旁边,依次类推。

对于 nn 为偶数的情况,就这样完事。

对于 nn 为奇数的情况,需要判断最后一个数放在哪一端,取 max⁡\max 即可。

std 有点二分风味,但是其本质是从两头往中间算,与二分无关。

std:

C++
#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的编辑器

思路:对顶栈。


根据 II 和 DD 经典操作,想到用栈来处理。

审题发现 LL 和 RR 操作,使得一个栈无法处理本题。考虑使用对顶栈。对顶栈,顾名思义,就是定义 AA BB 两个栈,从 A 中弹出的数值将被加入 B 中。

结合询问,可以得出以下一种做法:

定义两个栈 A、B,两个数组 sum[i]sum[i] 表示前缀和,f[i]f[i] 表示最大前缀和。

  1. 对于 II ,插入 xx 到栈 A 中。同时更新 sum[top]=sum[top-1]+A[top];f[top]=max(f[top-1],sum[top]);
  2. 对于 DD ,弹出栈 A 的栈顶。
  3. 对于 LL ,弹出栈 A 的栈顶,将其加入栈 B 中。
  4. 对于 RR ,弹出栈 B 的栈顶,将其加入栈 A 中。同时更新sum[top]=sum[top-1]+A[top];f[top]=max(f[top-1],sum[top]);
  5. 对于 QQ ,输出 f[k]f[k]。

代码:

C++
#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;
}
原文
存档评论2 条抓取于