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];
    }
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章