srm 551

资瓷点此阅读QvQ

250


Description

题意:一个长度最多 50 的字符串,每次操作可以交换相邻的两个字符,问,经过最多 MaxSwaps 次交换之后,最多能让多少个相同的字符连起来

Solution

枚举一个字符,让其他字符往这里即可,取最小即可

Code

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define F first
#define S second
typedef long long LL;
typedef pair<int, int> pii;
struct ColorfulChocolates {
    int maximumSpread(string chocolates, int maxSwaps) {
        int ans = 0;
        int n = chocolates.size();
        for (int i = 0; i < n; ++i) {
            string s = chocolates;
            int now = 1;
            vector<int> a;
            for (int j = i - 1; j >= 0; --j)
                if (s[j] == s[i])   a.pb(i - j - now), ++now;
            now = 1;
            for (int j = i + 1; j < n; ++j)
                if (s[j] == s[i])   a.pb(j - i - now), ++now;
            sort(a.begin(), a.end());
            int cnt = maxSwaps;
            int t = 1;
            for (int i = 0; i < a.size(); ++i)
                if (cnt - a[i] >= 0) {
                    cnt -= a[i];
                    ++t;
                }
            ans = max(ans, t);
        }
        return ans;
    }
};

450


Description

一个有向图,顶点标号 0n1 ,每个点会选择优先走他能到达的编号最小的点,现在想通过去掉一些边使得可以从 0 走到n1,求最少要去掉的边数

Solution

考虑类似于 floyd 的做法, d[i][j] 表示 i 走到j需要最少删多少条边,直接 dp 即可

Code

#include <bits/stdc++.h>//dp
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define F first
#define S second
typedef long long LL;
typedef pair<int, int> pii;
const int N = 55, inf = 1e6;
int d[N][N];
struct ColorfulWolves {
    int getmin(vector <string> colormap) {
        int n = colormap.size();
        for (int i = 0; i < n; ++i) {
            int now = 0;
            for (int j = 0; j < n; ++j) {
                if (colormap[i][j] == 'Y')  d[i][j] = now++;
                else d[i][j] = inf;
            }
        }
        for (int k = 0; k < n; ++k)
            for (int i = 0; i < n; ++i)
                for (int j = 0; j < n; ++j)
                    d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
        return d[0][n - 1] == inf ? -1 : d[0][n - 1];
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值