資瓷點此閱讀QvQ
250
Description
題意:一個長度最多
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
一個有向圖,頂點標號
Solution
考慮類似於
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];
}
};