Codeforces Round #677 (Div. 3) D-G

Problem - D - Codeforces

        (1)题目大意

                给你n个数,如果a[i] != a[j] 说明i->j可以构建一条边,问你是否可以把这个数组用n-1条边构建成一个连通块。

         (2)解题思路

                看到n-1条边,我们肯定要想到并查集这个东西,所以我们只需要暴力n^2枚举可以建边的点,然后看是否属于同一个集合即可,不属于的话就建边,然后弄个vector存答案就可以。

        (3)代码实现

// Problem: D. Districts Connection
// Contest: Codeforces - Codeforces Round #677 (Div. 3)
// URL: https://codeforces.com/contest/1433/problem/D
// Memory Limit: 256 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include "bits/stdc++.h"
#define rep(i, z, n) for (int i = z; i <= n; i++)
#define per(i, n, z) for (int i = n; i >= z; i--)
#define ll long long
#define db double
#define PII pair<int, int>
#define fi first
#define se second
#define vi vector<int>
#define yes cout << "YES" << endl;
#define no cout << "NO" << endl;
using namespace std;
const int N = 5010;
int a[N], f[N];
int find(int x)
{
    return x == f[x] ? x : f[x] = find(f[x]);
}
void solve()
{
    iota(f, f + N, 0);
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
    }
    vector<PII> ans;
    for (int i = 1; i <= n; i++)
    {
        for (int j = i + 1; j <= n; j++)
        {
            if (a[i] != a[j])
            {
                int fi = find(i), fj = find(j);
                if (fi != fj)
                {
                    ans.push_back({i, j});
                    f[fi] = fj;
                }
            }
        }
    }
    if (ans.size() != n - 1)
    {
        cout << "NO" << endl;
    }
    else
    {
        cout << "YES" << endl;
        for (auto x : ans)
            cout << x.fi << ' ' << x.se << endl;
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int T = 1;
    cin >> T;
    while (T--)
        solve();
    return 0;
}

Problem - E - Codeforces

        (1)题目大意

                给你一个n,有1-n个数,把这些数两两分成一组的方案数是多少?

         (2)解题思路

                (n - 1) ! / (n / 2)

         (3)代码实现

// Problem: E. Two Round Dances
// Contest: Codeforces - Codeforces Round #677 (Div. 3)
// URL: https://codeforces.com/contest/1433/problem/E
// Memory Limit: 256 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include "bits/stdc++.h"
#define rep(i, z, n) for (int i = z; i <= n; i++)
#define per(i, n, z) for (int i = n; i >= z; i--)
#define ll long long
#define db double
#define PII pair<int, int>
#define fi first
#define se second
#define vi vector<int>
#define yes cout << "YES" << endl;
#define no cout << "NO" << endl;
using namespace std;
const int N = 1e2 + 10;
void solve()
{
    int n;
    cin >> n;
    long long ans = 1;
    for (int i = 1; i < n; i++)
    {
        ans *= i;
    }
    cout << ans / (n / 2) << endl;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int T = 1;
    // cin >> T;
    while (T--)
        solve();
    return 0;
}

Problem - F - Codeforces

        (1)题目大意

                给你一个矩阵a有n*m行,每行你最多选m/2个数,问你最后选出来的数的和%k==0的最大值是多少?

         (2)解题思路

                考虑第一个dp预处理出第i行选c个数模k为m的最大和是多少

                转移方程为(注意这里c应该从大到小枚举,不然就会重复枚举)

                dp[i][c + 1][(m + a[i][j]) % k] = max(dp[i][c + 1][(m + a[i][j]) % k], dp[i][c][m] + a[i][j]);

                考虑dp2,表示前面i行中,模数和最大为j的最大值是多少

                转移方程为(这里用dp3是因为防止状态转移的时候出现重复转移)

                        dp3[(j + m) % k] = max(dp3[(j + m) % k], dp2[i][j] + dp2[i - 1][m]);
 

                最后答案就是dp2[n][0];

        (3)代码实现

// Problem: F. Zero Remainder Sum
// Contest: Codeforces - Codeforces Round #677 (Div. 3)
// URL: https://codeforces.com/contest/1433/problem/F
// Memory Limit: 256 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include "bits/stdc++.h"
#define rep(i, z, n) for (int i = z; i <= n; i++)
#define per(i, n, z) for (int i = n; i >= z; i--)
#define ll long long
#define db double
#define PII pair<int, int>
#define fi first
#define se second
#define vi vector<int>
#define yes cout << "YES" << endl;
#define no cout << "NO" << endl;
using namespace std;
const int N = 80;
ll dp[N][N / 2][N], a[N][N];
ll dp2[N][N];
void solve()
{
    int n, m, k;
    cin >> n >> m >> k;
    memset(dp, -0x3f, sizeof(dp));
    ll inf = dp[0][0][0];
    for (int i = 1; i <= n; i++)
    {
        dp[i][0][0] = 0;
    }
    int up = m / 2;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            cin >> a[i][j];
            for (int c = up - 1; c >= 0; c--)
            {
                for (int m = 0; m < k; m++)
                {
                    if (dp[i][c][m] != inf)
                    {
                        dp[i][c + 1][(m + a[i][j]) % k] = max(dp[i][c + 1][(m + a[i][j]) % k], dp[i][c][m] + a[i][j]);
                    }
                }
            }
        }
    }
    memset(dp2, -0x3f, sizeof(dp2));
    for (int i = 1; i <= n; i++)
    {
        dp2[i][0] = 0;
    }
    for (int i = 0; i < k; i++)
    {
        for (int d = 1; d <= up; d++)
        {
            dp2[1][i] = max(dp2[1][i], dp[1][d][i]);
        }
    }
    for (int i = 2; i <= n; i++)
    {
        vector<ll> dp3(k + 1, inf);
        for (int j = 0; j < k; j++)
        {
            for (int d = 1; d <= up; d++)
            {
                dp2[i][j] = max(dp2[i][j], dp[i][d][j]);
            }
        }
        for (int j = 0; j < k; j++)
        {
            for (int m = 0; m < k; m++)
            {
                if (dp2[i - 1][m] != inf)
                {
                    dp3[(j + m) % k] = max(dp3[(j + m) % k], dp2[i][j] + dp2[i - 1][m]);
                }
            }
        }
        for (int j = 0; j < k; j++)
        {
            dp2[i][j] = max(dp2[i][j], dp3[j]);
        }
    }
    ll ans = 0;
    for (int i = 1; i <= n; i++)
    {
        ans = max(dp2[i][0], ans);
    }
    cout << ans << endl;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int T = 1;
    // cin >> T;
    while (T--)
        solve();
    return 0;
}

Problem - G - Codeforces

        (1)题目大意

                给你一张图,m条双向边,每条边都有一个边权,有q个询问,每个询问独立,你可以去除一条边的价值,问你最后这q个询问所经过的总路径的最小价值是多少?

         (2)解题思路

                考虑对于每一个点都进行一遍djikstra,然后枚举改去除哪条边,枚举q个询问除了这条边,得到的最小价值和为多少,这里注意要分情况讨论,可能第i个询问不需要经过这条边,所以答案就是dis[st][ed],或者经过st-u,和v-ed这李答案就是dis[st][u] + dis[v][ed],或者经过st-v,ed-u,这里答案就是dis[ed][u] + dis[v][st]。

        (3)代码实现

// Problem: G. Reducing Delivery Cost
// Contest: Codeforces - Codeforces Round #677 (Div. 3)
// URL: https://codeforces.com/contest/1433/problem/G
// Memory Limit: 256 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include "bits/stdc++.h"
#define rep(i, z, n) for (int i = z; i <= n; i++)
#define per(i, n, z) for (int i = n; i >= z; i--)
#define ll long long
#define db double
#define PII pair<int, int>
#define fi first
#define se second
#define vi vector<int>
#define yes cout << "YES" << endl;
#define no cout << "NO" << endl;
using namespace std;
const int M = 2e6 + 10, N = 1010;
struct Edge
{
    int nt, to, wi;
} e[M];
int he[N], idx, dis[N][N], vis[N];
void add(int u, int v, int w)
{
    e[idx] = {he[u], v, w};
    he[u] = idx++;
}
void dij(int sta)
{
    memset(dis[sta], 0x3f, sizeof(dis[sta]));
    memset(vis, 0, sizeof(vis));
    queue<int> q;
    dis[sta][sta] = 0;
    q.push(sta);
    while (q.size())
    {
        int v = q.front();
        q.pop();
        vis[v] = false;
        for (int i = he[v]; ~i; i = e[i].nt)
        {
            int j = e[i].to, w = e[i].wi;
            if (dis[sta][j] > dis[sta][v] + w)
            {
                dis[sta][j] = dis[sta][v] + w;
                if (!vis[j])
                {
                    q.push(j);
                    vis[j] = true;
                }
            }
        }
    }
}
void solve()
{
    memset(he, -1, sizeof(he));
    int n, m, q;
    cin >> n >> m >> q;
    vector<int> ways;
    vector<PII> qrys;
    for (int i = 1; i <= m; i++)
    {
        int u, v, w;
        cin >> u >> v >> w;
        add(u, v, w);
        add(v, u, w);
        ways.push_back(2 * i - 1);
    }
    while (q--)
    {
        int st, ed;
        cin >> st >> ed;
        qrys.push_back({st, ed});
    }
    for (int i = 1; i <= n; i++)
    {
        dij(i);
    }
    ll ans = 1e18;
    for (int i = 0; i < ways.size(); i++)
    {
        int u = e[ways[i]].to, v = e[ways[i] ^ 1].to;
        ll sum = 0;
        for (int j = 0; j < qrys.size(); j++)
        {
            int st = qrys[j].fi, ed = qrys[j].se;
            sum += min({dis[st][ed], dis[u][st] + dis[ed][v], dis[u][ed] + dis[st][v]});
        }
        ans = min(ans, sum);
    }
    cout << ans << endl;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int T = 1;
    while (T--)
        solve();
    return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值