4.1下午训练 -- 东塘407 总结

· · 个人记录

T2

P5638 【CSGRound2】光骓者的荣耀

思路

这就是一道前缀和的题目。

首先求出前缀和,其次我们判断如果在这个地方设传送门能够使得路程更短,那就在此地设置传送门。

AC 代码如下

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6 + 9;
int s[N] , a[N];
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n , k;
    cin >> n >> k;
    if (k >= n - 1)
    {
        cout << 0;
        return 0;
    }
    for (int i = 1; i < n; i++)
    {
        cin >> a[i];
        s[i] = s[i - 1] + a[i];
    }
    int maxn = s[k];
    for (int i = 2; i <= n - k; i++)
    {
        if (maxn < s[i + k - 1] - s[i - 1])
        {
            maxn = s[i + k - 1] - s[i - 1];
        }
    }
    cout << s[n - 1] - maxn;
    return 0;
}

T3

P11377 [GESP202412 七级] 武器购买

思路

一道背包题~~

完全背包问题,for 循环 i 从 1 ~ n,j 从 1 ~ q(因为总花费不能超过 q,j 是来枚举花费)

然后判断如果 c_i \le j,也就是当前武器的花费小于当前可用花费,dp[i][j] = max(dp[i - 1][j] , dp[i - 1][j - w[i]] + v[i])

否则,dp[i][j] = dp[i - 1][j]

最后遍历找出答案。

错误代码

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 105 , M = 5e4 + 5;
int w[N] , v[N];
int dp[N][M];
void sloved()
{
    int n , p , q;
    cin >> n >> p >> q;
    for (int i = 1; i <= n; i++)
    {
        cin >> w[i] >> v[i];
    }
    dp[1][1] = 1;
    for (int i = 1; i <= n; i++)
    {
        for (int j = w[i]; j >= p; j--)
        {
            dp[i][j] = max(dp[i - 1][j] , dp[i][j - w[i]] + v[i]);
//          cout << dp[i][j] << " ";
        }
//      cout << endl;
    }
    if (dp[n][p] <= q && dp[n][p] >= p)
    {
        cout << dp[n][p] << endl;
        return ;
    }
    cout << -1 << endl;
}
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while (t--)
    {
        sloved();
    }
    return 0;
}

错误原因

① 枚举花费的 j 范围不对。

② dp_{i , j} 求的不对。

③ 输出不对。

AC代码如下

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 105 , M = 5e4 + 5;
int w[N] , v[N] , dp[N][M];
void sloved()
{
    memset(dp , 0 , sizeof dp);
    int n , p , q;
    cin >> n >> p >> q;
    for (int i = 1; i <= n; i++)
    {
        cin >> v[i] >> w[i];
    }
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= q; j++)
        {
            if (w[i] <= j)
            {
                dp[i][j] = max(dp[i - 1][j] , dp[i - 1][j - w[i]] + v[i]);
            }
            else
            {
                dp[i][j] = dp[i - 1][j];
            }
        }
    }
    for (int i = 1; i <= q; i++)
    {
        if (dp[n][i] >= p)
        {
            cout << i << endl;
            return ;
        }
    }
    cout << -1 << endl;
    return ;
}
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while (t--)
    {
        sloved();
    }
    return 0;
}

T5

P1434 [SHOI2002] 滑雪

思路

这道题就是一个记忆化搜索~~

dfs 函数

首先加一个记忆化,如果 x,y 被执行过了,就直接返回答案,不用 dfs 了。

然后 for 循环 0 ~ 4,枚举 4 个方向。判断如果下一个地方 nx , ny 满足条件,就 dfs。

主函数

先是输入,然后枚举所有起点,然后取最大值。

错误代码

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 105;
int dx[] = {0 , 0 , 1 , -1};
int dy[] = {1 , -1 , 0 , 0};
struct stu
{
    int x , y , step;
};
int n , m;
bool vis[N][N];
int cnt = 0;
void bfs(int x , int y)
{
    queue <stu> qu;
    qu.push({x , y , 1});
    while (qu.empty() == 0)
    {
        cnt++;
        stu cur = qu.front();
        qu.pop();
        for (int i = 0; i < 4; i++)
        {
            int nx = cur.x + dx[i];
            int ny = cur.y + dy[i];
            if (nx >= 1 && ny >= 1 && nx <= n && ny <= m && vis[nx][ny] == 0)
            {
                vis[nx][ny] = 1;
                stu next = {nx , ny , cur.step + 1};
                qu.push(next);
            }
        }
    }
    stu dang = qu.front();
    qu.pop();
    cout << cnt - 1;
    return ;
}
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    int sx = 0 , sy = 0;
    int maxn = INT_MIN;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            int x;
            cin >> x;
            if (maxn < x)
            {
                sx = i;
                sy = j;
            }
        }
    }
    bfs(sx , sy);
    return 0;
}

错误原因

① 没有用记忆化。

② 没有枚举所有起点。

AC 代码如下

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 105;
int dx[] = {0 , 0 , 1 , -1};
int dy[] = {1 , -1 , 0 , 0};
int n , m;
int vis[N][N];
int a[N][N];
int dfs(int x , int y)
{
    if (vis[x][y] != 0)
    {
        return vis[x][y];
    }
    vis[x][y] = 1;
    for (int i = 0; i < 4; i++)
    {
        int nx = x + dx[i];
        int ny = y + dy[i];
        if (nx > 0 && ny > 0 && nx <= n && ny <= m && a[x][y] > a[nx][ny])
        {
            dfs(nx , ny);
            vis[x][y] = max(vis[x][y] , vis[nx][ny] + 1);
        }
    }
    return vis[x][y];
}
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    int maxn = 0;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            cin >> a[i][j];
//          maxn = max(maxn , a[i][j]);
        }
    }
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
//          cout << "{" << dfs(i , j) << " " << maxn << "}";
            maxn = max(maxn , dfs(i , j));
        }
//      cout << endl;
    }
    cout << maxn;
    return 0;
}

T6

P5429 [USACO19OPEN] Fence Planning S

思路

这就是一道并查集的题目了。

我们先根据题目输入,然后将有鸣叫关系的两头奶牛合并。

然后我们 for 循环 1 ~ n,找出 i 的根结点,然后找出 MINx、MAXx、MINy、MAXy(x 指当前奶牛的 x 坐标,y 指当前奶牛的 y 坐标)。

最后枚举 1 ~ n,如果根结点没变,说明这是一棵树,就计算出当前围栏的周长,然后取最小值。

AC 代码如下

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 5;
int fa[N];
int get(int x)
{
    if (fa[x] == 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;
    }
    return ;
}
struct stu
{
    int x , y;
}s[N];
int minx[N] , miny[N] , maxx[N] , maxy[N];
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n , m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        fa[i] = i;
    }
    for (int i = 1; i <= n; i++)
    {
        cin >> s[i].x >> s[i].y;
    }
    for (int i = 1; i <= n; i++)
    {
        minx[i] = 1e8;
        miny[i] = 1e8;
    }
    while (m--)
    {
        int u , v;
        cin >> u >> v;
        unite(u , v);
    }
    for (int i = 1; i <= n; i++)
    {
        int x = s[i].x , y = s[i].y;
        int geti = get(i);
        minx[geti] = min(minx[geti] , x);
        miny[geti] = min(miny[geti] , y);
        maxx[geti] = max(maxx[geti] , x);
        maxy[geti] = max(maxy[geti] , y);
    }
    int minn = INT_MAX;
    for (int i = 1; i <= n; i++)
    {
        if (fa[i] == i)
        {
            int ans = (maxx[i] - minx[i]) * 2 + (maxy[i] - miny[i]) * 2;
            minn = min(minn , ans);
        }
    }
    cout << minn;
    return 0;
}