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

· · 个人记录

T4

P1806 跑步

思路

这个题直接按照题面描述不太好理解,我们可以来转化问题:

我们假设有一个大小为 n的箱子。另外还有一些小箱子,体积从1~n都各有1个。
题目其实就是用这些小箱子把n的箱子正好全部填满。

然后我们就可以用 01背包来写这道题啦~~

状态表示:dp[i][j] ]表示1~i任选,凑成j的方案数
答案:dp[n][n] - 1;
状态转移方程:
dp[i] = dp[i - 1][j] + dp[i - 1][j - i];
起始点(初始化):dp[i][0] = 1;

AC 代码如下

#include <bits/stdc++.h>
#define int long long
using namespace std;
int dp[505][505]; 
signed main()
{
    int n;
    cin >> n;
    for (int i = 0; i <= n; i++)
    {
        dp[i][0] = 1;
    }
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            if (j < i)
                dp[i][j] = dp[i - 1][j];
            else 
                dp[i][j] = dp[i - 1][j] + dp[i - 1][j - i];
        }
    }
    cout << dp[n][n] - 1;
    return 0;
}

T5

P1702 突击考试

思路

我们可以先来尝试暴力,那就需要枚举长度、左端点和 k,这肯定会超时。

正解是枚举 k 的值。毕竟 k 很小,这就不会超时了。

那么这样问题就变成了如果 a_i 和 b_i 中至少有一个k, 最多有多少个连续的满足要求。

那这就很简单了吖。。。

错误代码

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

错误原因

并没有枚举 k,而是遍历整个数组,去判断跟上一个相等的有多少个。但并没有取 cnt 的最大值。

AC 代码如下

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 5 , M = 1e4 + 5;
int a[N] , b[N];
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i] >> b[i];
    }
    int K = 100 , ans = 0;
    for (int k = 1; k <= 5; k++)
    {
        int cnt = 0;
        for (int i = 1; i <= n; i++)
        {
            if (a[i] == k || b[i] == k)
            {
                cnt++;
            }
            else
            {
                cnt = 0;
            }
            if (cnt > ans)
            {
                ans = cnt;
                K = k;
            }
        }
    }
    cout << ans << " " << K;
    return 0;
}

T6

P11005 [蓝桥杯 2024 省 Python B] 缴纳过路费

思路

题目分析

条件就是有一条路径,走过它的代价不是这条路径的边权和,而是这条路径上最大的边权。

然后找到有多少对满足条件的点对。

其实题目理解起来还是蛮简单的,也可以轻松看出来是最小生成树的题目。。。

实现方法

AC 代码如下

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 5 , M = 1e6 + 5;
struct stu
{
    int x , y , val;
}s[M];
int fa[N] , siz[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 ;
}
bool cmp(stu x , stu y)
{
    return x.val < y.val;
}
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n , m , l , r;
    cin >> n >> m >> l >> r;
    for (int i = 1; i <= n; i++)
    {
        fa[i] = i;
        siz[i] = 1;
    }
    for (int i = 1; i <= m; i++)
    {
        int u , v , w;
        cin >> u >> v >> w;
        s[i] = {u , v , w};
    }
    sort (s + 1 , s + m + 1 , cmp);
    int ans = 0;
    for (int i = 1 ; i <= m; i++)
    {
        int x = get(s[i].x) , y = get(s[i].y);
        if (x != y)
        {
            if (s[i].val >= l && s[i].val <= r)
            {
                ans += siz[x] * siz[y];
            }
            fa[x] = y;
            siz[y] += siz[x];
        }
    }
    cout << ans;
    return 0;
}