Educational Codeforces Round 167 (Rated for Div. 2)解题报告(A-C)
A. Catch the Coin
链接:A. Catch the Coin
题目:
解题思路:
显然只需要y >= -1 即可收集到硬币
代码:
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, x, y;
cin >> n;
while (n--) {
cin >> x >> y;
if (y < -1) cout << "NO\n";
else cout << "YES\n";
}
return 0;
}
B - Substring and Subsequence
链接:B - Substring and Subsequence
题目:
解题思路:
b为字符串的连续子串,故a中的重复元素必须与b连续匹配,找出最长的匹配长度,在将两字符串长度相加减去匹配长度即为答案
代码:
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
while (n--) {
string a, b;
cin >> a >> b;
int begin = 0;
int n = a.size(), m = b.size();
int ans = n + m;
for (int i = 0; i < m; i++) {
int j = i;
for (auto c : a) {
if (b[j] == c) j++;
}
ans = min(ans, m+n-(j-i)); //m+n-(j-i)就是匹配长度
}
cout << ans << '\n';
}
return 0;
}
C - Two Movies
链接:C - Two Movies
题目:
解题思路:
先将更喜欢其中某部电影的人肯定对更喜欢的那部电影评分更有利,先记录下来,然后再对评价一样的人进行遍历讨论
代码:
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> a(n), b(n);
for (auto& x : a) cin >> x;
for (auto& x : b) cin >> x;
int x = 0, y = 0, neg = 0, pos = 0;
for (int i = 0; i < n; ++i) {
if (a[i] > b[i]) {
x += a[i];
} else if (a[i] < b[i]) {
y += b[i];
} else {
neg += (a[i] < 0); //两部都讨厌的人数
pos += (a[i] > 0); //两部都喜欢的人数
}
}
int ans = -1e9;
//遍历两边评价一样的人
for (int i = -neg; i <= pos; ++i)
//当i为负值时,都不喜欢的人中有|i|个不喜欢的人在第一部电影,有neg-|i|个在第二部电影,有pos个都喜欢的人在第二部电影处
//喜欢第一部评分减|i|,变为x-|i|,因为i为负值,|i| = -i,x+i
//喜欢第二部评分加pos,减neg-|i|,变为y + pos - (neg-|i|),因为i为负值,|i| = -i,y + (pos - neg - i)
//i为正值时,i代表变为都喜欢的人,同理,y - neg + (pos - i) == y + (pos - neg - i)
//此时,第一部评分为 x+i, 第二部评分为 y + (pos - neg - i)
ans = max(ans, min(x + i, y + (pos - neg - i))); //在所有情况中,选择"这两个值中最小值"最大的选项。
cout << ans << '\n';
}
}