A. Mike and palindrome(Codeforces 798A)
思路
本題的入手點爲“恰好改變一個字符”。
因爲能且僅能改變一個字符,所以可以枚舉被改變的字符,接着嘗試將其改變成任意跟原字符不同的字符,然後判斷新的字符串是否是迴文串。
注意,每次判斷完新字符串是否是迴文串後要將字符串改回原串,這樣纔不會影響下次修改。
代碼
#include <bits/stdc++.h>
using namespace std;
string s, t;
int n;
// 判斷字符串是否是迴文串
bool isP(string s) {
n = s.size();
for(int i = 0; i < (n + 1) / 2; i++) {
if(s[i] != s[n - i - 1]) {
return false;
}
}
return true;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> s;
n = s.size();
t = s;
// 枚舉被改變的字符
for(int i = 0; i < n; i++) {
// 枚舉改變成的字符
for(int j = 0; j < 26; j++) {
if(s[i] == 'a' + j) {
continue;
}
t[i] = 'a' + j;
if(isP(t) == true) {
puts("YES");
return 0;
}
// 要改回去,不然影響下次判斷
t[i] = s[i];
}
}
puts("NO");
return 0;
}
B. Mike and strings(Codeforces 798B)
思路
本題的入手點是,給的字符串數量並不多,於是可以用暴力的方法處理。
可以證明,選定某個字符串,將其它字符串都改成它,這一定是最優的策略。那麼我們可以枚舉這個“模板”字符串
枚舉的複雜度是
如上述,可以用鏈表來將這個算法優化至
代碼
#include <bits/stdc++.h>
using namespace std;
const int maxn = 55, INF = 55 * 55;
string S[maxn], T[maxn];
int n, res, ans;
// 判斷其它字符串是否能變成S[idx]
int solve(int idx) {
int res = 0;
for(int i = 1; i <= n; i++) {
if(idx == i) {
continue;
}
T[i] = S[i];
int m = T[i].size();
bool ok = false;
// 反覆將首字符加到尾部
for(int j = 0; j < m; j++) {
if(T[i] == S[idx]) {
ok = true;
res += j;
break;
}
T[i].push_back(T[i][0]);
T[i] = T[i].substr(1, m);
}
if(ok == false) {
return -1;
}
}
return res;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> S[i];
T[i] = S[i];
}
ans = INF;
// 枚舉模板字符串
for(int i = 1; i <= n; i++) {
res = solve(i);
if(res < 0) {
cout << -1 << endl;
return 0;
}
ans = min(ans, res);
}
cout << ans << endl;
return 0;
}
C. Mike and gcd problem(Codeforces 798C)
思路
本題的入手點是,嘗試了若干數據後可以發現,問題的本質是奇偶性而不是最大公約數。
首先,對整個序列求一個
否則,先不考慮整個數組。考慮數組中的兩個相鄰元素
- 奇數
± 奇數 = 偶數 - 偶數
± 偶數 = 偶數 - 奇數
± 偶數 = 奇數 - 偶數
± 奇數 = 奇數
那麼我們有兩種變換策略:
- 若兩個相鄰的數是互質的且兩個數的奇偶性相同的話,則最多一次操作就可以讓他們的
gcd 變成2 。 - 若兩個相鄰的數是互質的且兩個數的奇偶性不同的話,則通過兩次操作(先變成兩個奇數,再變成兩個偶數)就可以讓他們的
gcd 變成2 。
到這裏,我們別無選擇,只能用這種兩種策略,來拼湊出一個解。爲什麼這樣是最優的呢?因爲找不出更優的策略了(哈哈~抱歉^^)。那麼問題就轉化成怎麼用這兩種策略來將數組中的所有數變成偶數。這裏同樣有兩種方法:
- 貪心。先將所有相鄰的奇數通過一次變換雙雙變成偶數,再將所有的相鄰的奇偶性不同的數通過一次變換雙雙變成偶數。
- 動態規劃。如果對貪心的算法不放心的話用動態規劃是最保險的。令
d[i][0] 表示當前考慮完第i 個數,之前的數(a[k],k<i )都被改成偶數,且a[i] 被改變成奇數時的最少修改次數。d[i][1] 同理,是a[i] 被改變成偶數的情況。狀態轉移的細節見代碼中的註釋。
代碼1(貪心)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 10, INF = 1e8;
ll x, y, n, g, ans, a[maxn];
ll gcd(ll x, ll y) {
return y ? gcd(y, x % y) : x;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> a[i];
}
g = a[1];
for(int i = 2; i <= n; i++) {
g = gcd(g, a[i]);
}
if(g > 1) {
cout << "YES" << endl << 0 << endl;
return 0;
}
// 處理相鄰兩個數都是奇數的情況
for(int i = 1; i < n; i++) {
if(a[i] % 2 != 0 && a[i + 1] % 2 != 0) {
x = a[i];
y = a[i + 1];
a[i] = x - y;
a[i + 1] = x + y;
ans++;
}
}
// 處理剩下的情況
for(int i = 1; i < n; i++) {
if(a[i] % 2 == 0 && a[i + 1] % 2 == 0) {
continue;
}
for(int j = 1; j <= 2; j++) {
x = a[i];
y = a[i + 1];
a[i] = x - y;
a[i + 1] = x + y;
ans++;
}
}
cout << "YES" << endl << ans << endl;
return 0;
}
代碼2(DP)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 10, INF = 1e8;
ll x, y, n, g, ans, a[maxn], d[maxn][2];
ll gcd(ll x, ll y) {
return y ? gcd(y, x % y) : x;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> a[i];
}
g = a[1];
for(int i = 2; i <= n; i++) {
g = gcd(g, a[i]);
}
if(g > 1) {
cout << "YES" << endl << 0 << endl;
return 0;
}
// DP的初始化
for(int i = 1; i <= n; i++) {
d[i][0] = d[i][1] = INF;
}
if(a[1] % 2 == 0) {
d[1][0] = 0;
}
else {
d[1][1] = 0;
}
for(int i = 2; i <= n; i++) {
if(a[i] % 2 == 0) {
// 當前a[i]是偶數且保持不變的情況
d[i][0] = min(d[i-1][0], d[i-1][1] + 2);
}
else {
// 當前a[i]是奇數且被修改成偶數時
d[i][0] = min(d[i-1][0] + 2, d[i-1][1] + 1);
// 當前a[i]是奇數且保持不變時
d[i][1] = d[i-1][0];
}
}
ans = d[n][0];
cout << "YES" << endl << ans << endl;
return 0;
}
D. Mike and distribution(Codeforces 798D)
思路
本題的入手點是,將條件簡化和排序後將問題簡化。
首先注意到我們應該在子集中包含儘可能多的數,所以構造的子集中最好拿滿
其次題目的條件“子集和的兩倍大於數組和”等價於“子集和大於子集的關於全集的補集的和”。
因爲題目給了兩個數組,同時考慮兩個數組求子集會比較難。因此可以用一個三元組數組
很容易證明,對於一個亂序的序列,當我們兩個兩個地遍歷數組時(假設當前遍歷到
回到問題中來。在我們對
代碼
#include <bits/stdc++.h>
using namespace std;
typedef tuple <int, int, int> t;
const int maxn = 1e5 + 5;
t ts[maxn];
vector <int> ans;
int n, a, b, c, d, idx, idx2;
int x[maxn], y[maxn];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> x[i];
}
for(int i = 1; i <= n; i++) {
cin >> y[i];
}
for(int i = 1; i <= n; i++) {
ts[i] = t(x[i], y[i], i);
}
ts[0] = t(0, 0, 0);
// 排序
sort(ts + 1, ts + n + 1);
// 將A最大的先加入子集
tie(a, b, idx) = ts[n];
ans.push_back(idx);
// 兩個兩個遍歷
for(int i = n - 1; i >= 1; i -= 2) {
tie(a, b, idx) = ts[i];
tie(c, d, idx2) = ts[i - 1];
if(b > d) {
ans.push_back(idx);
}
else {
ans.push_back(idx2);
}
}
cout << ans.size() << endl;
for(int val : ans) {
cout << val << ' ';
}
return 0;
}
(其它題目略)