5.6 周二下午训练 -- 东塘407 总结

· · 个人记录

成绩

T1 T2 T3 T4 T5 T6
100 30 0 100 60 0

T2

B3702 [语言月赛202301] 华小科的旅行开始了

思路

很简单的模拟。。。

我们找到结束的坐标 (i,j),然后 while 循环,每一次都先输出当前位置,然后更新当前位置。

错误代码

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e3 + 5;
int x[N][N] , y[N][N];
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n , m , sx , sy;
    cin >> m >> n >> sx >> sy;
    int ex , ey;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            cin >> x[i][j] >> y[i][j];
            if (x[i][j] == 0 && y[i][j] == 0)
            {
                ex = i;
                ey = j;
            }
        }
    }
//  cout << ex << " " << ey << endl;
    int nx = 1 , ny = 1;
    while (x[nx][ny] != 0 && y[nx][ny] != 0)
    {
        cout << nx << " " << ny << endl;
        int fx = nx , fy = ny;
        nx = x[fx][fy];
        ny = y[fx][fy];
//      cout << nx << " " << ny << " " << x[nx][ny] << " " << y[nx][ny] << endl;
    }
    cout << nx << " " << ny << endl;
    return 0;
}

错误原因

每一次都是从 (Sx , Sy) 开始旅行的,但我是从 (1 , 1) 开始的…痛失70分┭┮﹏┭┮

AC 代码如下

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e3 + 5;
int x[N][N] , y[N][N];
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n , m , sx , sy;
    cin >> m >> n >> sx >> sy;
    int ex , ey;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            cin >> x[i][j] >> y[i][j];
            if (x[i][j] == 0 && y[i][j] == 0) // 找到结束的点
            {
                ex = i;
                ey = j;
            }
        }
    }
//  cout << ex << " " << ey << endl;
    int nx = sx , ny = sy;
    while (x[nx][ny] != 0 && y[nx][ny] != 0)
    {
        cout << nx << " " << ny << endl; // 输出答案
        int fx = nx , fy = ny;
        nx = x[fx][fy];
        ny = y[fx][fy]; // 更新当前位置
//      cout << nx << " " << ny << " " << x[nx][ny] << " " << y[nx][ny] << endl;
    }
    cout << nx << " " << ny << endl;
    return 0;
}

T3

B3681 [语言月赛202211] Power Strip

思路

记忆化搜索……

AC 代码如下

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6 + 5;
int u[N] , a[N] , f[N];
vector <int> g[N];
int dfs(int step)
{
    f[step] = a[step]; // 当前点至少会有 a[step] 这么多
    int len = g[step].size();
    for (int i = 0; i < len; i++) // 遍历所有跟当前点有关联的点
    {
        int u = g[step][i];
        f[step] += dfs(u); // 累计答案
    }
    return f[step];
}
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    for (int i = 2; i <= n; i++)
    {
        cin >> u[i];
        g[u[i]].push_back(i); // 建边
    }
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
    }
    dfs(1);
    for (int i = 1; i <= n; i++)
    {
        cout << f[i] << " "; // 输出答案
    }
    return 0;
}

T5

P1609 最小回文数

思路

我们分多种情况来讨论:

No.1 这个字符串本身就是回文字符串

① 位数是偶数(以 ABCDEF 为例)

我们可以知道,如果样例给的是 ABCDEF,那么 ABCCBA 肯定最小回文数。但这里我们也要分几种情况:

我们就取出 ABC,然后将 C 加一,答案字符串就为 AB(C+1)(C+1)BA。

但是这里有一个特例,就是如果 C 为9,那 C 加一就是 :,就出错了。我们可以加上一个判断,但又不能只加一个,因为有可能 B 也为9。所以我们加上一个循环,只要当前字符是 9,我们就判断下一个字符,否则,当前字符加一,退出循环,我们将这个修改后的字符串称为 GHI。

最后答案就是 GHIIHG

② 位数是奇数(以 ABCDEFG 为例)

我们可以知道,如果样例给的是 ABCDEFG,那么 ABCDCBA 肯定最小回文数。但这里我们也要分几种情况:

No.2 这个字符串本身不是回文字符串

与上面的同理。

No.3还有一个特例

这个特例就是全是9的情况。如果全是9,我们就先输出一个1,然后输出 len - 1 个 0(len 为字符串长度),最后再输出一个1。(可以自己推导)

错误代码

#include <bits/stdc++.h>
#define int long long
using namespace std;
bool check(string s , int lens)
{
    string st = "";
    for (int i = lens - 1; i >= 0; i--)
    {
        st += s[i];
    }
    if (s == st)
    {
        return 1;
    }
    return 0;
}
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    string s;
    cin >> s;
    int lens = s.size();
    if (check(s , lens) == 1)
    {
        string t = s.substr(0 , lens / 2);
        int lent = t.size();
        if (lens % 2 == 0)
        {
            t[lent - 1]++;
            cout << t;
            string st = "";
            for (int i = lent - 1; i >= 0; i--)
            {
                st += t[i];
            }
            cout << st;
        }
        else
        {
            cout << t;
            cout << char(s[lent] + 1);
            string st = "";
            for (int i = lent - 1; i >= 0; i--)
            {
                st += t[i];
            }
            cout << st;
        }
    }
    else
    {
        string t = s.substr(0 , lens / 2);
        int lent = t.size();
        if (lens % 2 == 0)
        {
//          t[lent - 1]++;
            if (t[0] <= s[lens - 1])
            {
                t[lent - 1]++;
//              cout << s[lens - 1] << " " << t[0] << " ";
            }
            cout << t;
            string st = "";
            for (int i = lent - 1; i >= 0; i--)
            {
                st += t[i];
            }
            cout << st;
        }
        else
        {
            cout << t;
            cout << char(s[lent] + 1);
//          cout << s[lent + 1];
//          cout << lent;
            string st = "";
            for (int i = lent - 1; i >= 0; i--)
            {
                st += t[i];
            }
            cout << st;
        }
    }
    return 0;
}

错误原因

没有判断9的情况。

90 分代码

#include <bits/stdc++.h>
#define int long long
using namespace std;
bool check(string s , int lens)
{
    string st = "";
    for (int i = lens - 1; i >= 0; i--)
    {
        st += s[i];
    }
    if (s == st)
    {
        return 1;
    }
    return 0;
}
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    string s;
    cin >> s;
    if (s == "0" || s == "1" || s == "2" || s == "3"  || s == "4"  || s == "5"  || s == "6"  || s == "7"  || s == "8" || s == "9") 
    {
        cout << 11;
        return 0;
    }
    int lens = s.size();
    bool flag = 0;
    for (int i = 0; i < lens; i++)
    {
        if (s[i] != '9')
        {
            flag = 1;
        }
    }
    if (flag == 0)
    {
        cout << 1;
        for (int i = 1; i <= lens - 1; i++)
        {
            cout << 0;
        }
        cout << 1;
        return 0;
    }
    if (check(s , lens) == 1)
    {
        string t = s.substr(0 , lens / 2);
        int lent = t.size();
        if (lens % 2 == 0)
        {
            for (int i = lent - 1; i >= 0; i--)
            {
                if (t[i] == '9')
                {
                    t[i] = '0';
                }
                else
                {
                    t[i]++;
                    break;
                }
            }
            cout << t;
            string st = "";
            for (int i = lent - 1; i >= 0; i--)
            {
                st += t[i];
            }
            cout << st;
        }
        else
        {
            char x = s[lent];
            if (x == '9')
            {
                x = '0';
                for (int i = lent - 1; i >= 0; i--)
                {
                    if (t[i] == '9')
                    {
                        t[i] = '0';
                    }
                    else
                    {
                        t[i]++;
                        break;
                    }
                }
            }
            else
            {
                x++;
            }
            cout << t;
            cout << x;
            string st = "";
            for (int i = lent - 1; i >= 0; i--)
            {
                st += t[i];
            }
            cout << st;
        }
    }
    else
    {
        string t = s.substr(0 , lens / 2);
        int lent = t.size();
//      cout << s.substr(lens / 2 , lens / 2) << endl;
        if (lens % 2 == 0)
        {
//          t[lent - 1]++;
            string str = t;
            reverse(str.begin() , str.end());
            if (str <= s.substr(lens / 2 , lens / 2))
            {
                for (int i = lent - 1; i >= 0; i--)
                {
                    if (t[i] == '9')
                    {
                        t[i] = '0';
                    }
                    else
                    {
                        t[i]++;
                        break;
                    }
                }
//              cout << s[lens - 1] << " " << t[0] << " ";
            }
            cout << t;
            string st = "";
            for (int i = lent - 1; i >= 0; i--)
            {
                st += t[i];
            }
            cout << st;
        }
        else
        {
            char x = s[lent];
            if (x == '9')
            {
                x = '0';
                for (int i = lent - 1; i >= 0; i--)
                {
                    if (t[i] == '9')
                    {
                        t[i] = '0';
                    }
                    else
                    {
                        t[i]++;
                        break;
                    }
                }
            }
            else
            {
                x++;
            }
            cout << t;
            cout << x;
            string st = "";
            for (int i = lent - 1; i >= 0; i--)
            {
                st += t[i];
            }
            cout << st;
        }
    }
    return 0;
}

实在调不出来了awa

T6

P11486 「Cfz Round 5」Mata rainen

思路

这道题什么情况下会输出 No 呢?我们画图分析后可以得知,如果有环,就输出 No。

我们考虑没有环的情况。我们找到一个点对 (s_k , t_k),将它设为开头的,然后剩下的直接连边。此时我们就有若干个联通块,最后我们把这些联通块都串联起来。

AC 代码如下

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 3e5 + 5;
int x[N] , y[N];
int fa[N];
int get(int x)
{
    if (x == fa[x])
    {
        return x;
    }
    return fa[x] = get(fa[x]);
}
void unite(int x , int y)
{
    x = get(x);
    y = get(y);
    if (x != y)
    {
        fa[x] = y;
    }
}
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n , m;
    cin >> n >> m;
    for (int i = 1; i <= m; i++)
    {
        cin >> x[i] >> y[i];
    }
    for (int i = 1; i <= n; i++)
    {
        fa[i] = i;
    }
    for (int i = 1; i <= m; i++)
    {
        int nx = get(x[i]);
        int ny = get(y[i]);
        if (nx != ny)
        {
            unite(nx , ny);
        }
        else
        {
            cout << "No";
            return 0;
        }
    }
    cout << "Yes" << endl;
    for (int i = 1; i <= m - 1; i++)
    {
        cout << x[i] << " " << y[i] << endl;
    }
    int pre = x[m];
    for (int i = 1; i <= n; i++)
    {
        if (get(i) == i && get(i) != get(x[m]))
        {
            cout << pre << " " << i << endl;
            pre = i;
        }
    }
    cout << pre << " " << y[m];
    return 0;
}