Educational Codeforces Round 167 (Rated for Div. 2)
Educational Codeforces Round 167 (Rated for Div. 2)
A. Catch the Coin
解题思路:
最终\(x\)一定会相等,我们考虑直接到下面接住他。
代码:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using i128 = __int128_t;
typedef pair<int, int> pii;
#define fi first
#define se second
const int N = 1e5 + 10;
void solve()
{
int a, b;
cin >> a >> b;
if (a == 0 && b == -1)
{
cout << "YES\n";
return;
}
int c = -abs(a);
b -= abs((abs(a) - 1));
if (b >= c)
{
cout << "YES\n";
}
else
{
cout << "NO\n";
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
cin >> t;
while (t--)
{
solve();
}
return 0;
}
B. Substring and Subsequence
解题思路:
双循环暴力查找最优添加法。
代码:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using i128 = __int128_t;
typedef pair<int, int> pii;
#define fi first
#define se second
const int N = 1e5 + 10;
void solve()
{
string a, b;
cin >> a >> b;
int ans = 0;
for (int i = 0; i < b.size(); i++)
{
int idx = i;
int cnt = 0;
for (int j = 0; j < a.size(); j++)
{
if (a[j] == b[idx])
{
idx++;
cnt++;
}
}
ans = max(ans, cnt);
}
int res = a.size() + (b.size() - ans);
cout << res << "\n";
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
cin >> t;
while (t--)
{
solve();
}
return 0;
}
C. Two Movies
解题思路:
分情况讨论。
如果\(a \neq b:\)选大的那个。
如果\(a = b:\)优先让小的变大,然后让大的变小,最后均分。
代码:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using i128 = __int128_t;
typedef pair<int, int> pii;
#define fi first
#define se second
const int N = 1e5 + 10;
void solve()
{
int n;
cin >> n;
vector<int> a(n + 1), b(n + 1);
for (int i = 1; i <= n; i++)
{
cin >> a[i];
}
for (int i = 1; i <= n; i++)
{
cin >> b[i];
}
vector<int> cnt(2), cur(2);
for (int i = 1; i <= n; i++)
{
if (a[i] == b[i])
{
if (a[i] == 1)
{
cnt[0]++;
}
else if (a[i] == -1)
{
cnt[1]++;
}
}
else
{
if (a[i] == 1)
{
cur[0]++;
}
else if (b[i] == 1)
{
cur[1]++;
}
}
}
// cout << cur[0] << ' ' << cur[1] << endl;
// cout << cnt[0] << ' ' << cnt[1] << endl;
if (cur[0] > cur[1])
{
swap(cur[0], cur[1]);
}
while (cur[0] < cur[1] && cnt[0] > 0)
{
cnt[0]--;
cur[0]++;
}
while (cur[0] < cur[1] && cnt[1] > 0)
{
cur[1]--;
cnt[1]--;
}
int res = cnt[0] - cnt[1];
// cout << cur[0] << ' ' << cur[1] << ' ';
// cout << res << endl;
if (res > 0)
{
res = res / 2;
if (cur[0] == cur[1])
{
cout << cur[0] + res << '\n';
}
else
{
cout << cur[0] << '\n';
}
}
else
{
res = (res - 1) / 2;
if (cur[0] == cur[1])
{
cout << cur[0] + res << '\n';
}
else
{
cout << cur[0] << '\n';
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
cin >> t;
while (t--)
{
solve();
}
return 0;
}
D. Smithing Skill
解题思路:
双关键词升序排序:第一关键字\(a_i - b_i\),第二关键字\(a_i\)。
\(f_i:花费i个万能金属能得到的最大经验值。\)
考虑数据的单调性,双指针预处理\(f_i\)。
对于\(c_i > 10^6\)要处理到\(10^6\)以下。
代码:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using i128 = __int128_t;
typedef pair<int, int> pii;
#define fi first
#define se second
const int N = 1e6;
void solve()
{
ll n, m;
cin >> n >> m;
vector<ll> a(n + 1), b(n + 1), c(m + 1);
for (int i = 1; i <= n; i++)
{
cin >> a[i];
}
for (int i = 1; i <= n; i++)
{
cin >> b[i];
}
for (int i = 1; i <= m; i++)
{
cin >> c[i];
}
vector<pii> v(1);
for (int i = 1; i <= n; i++)
{
ll t = a[i] - b[i];
v.push_back({t, a[i]});
// cout << t << ' ' << a[i] << endl;
}
sort(v.begin() + 1, v.end());
vector<pii> d(1);
for (int i = 1; i <= n; i++)
{
if (d.size() > 1)
{
if (v[i].se < d.back().se)
{
d.push_back(v[i]);
}
}
else
{
d.push_back(v[i]);
}
}
// for (int i = 1; i < d.size(); i++)
// {
// cout << d[i].fi << ' ' << d[i].se << endl;
// }
vector<int> f(N + 10);
for (int i = 1, j = d.size(); i <= N; i++)
{
while ((j > 1) && d[j - 1].se <= i)
{
j--;
}
if (j != (int)d.size())
{
auto [x, y] = d[j];
f[i] = f[i - x] + 1;
}
}
ll ans = 0;
for (int i = 1; i <= m; i++)
{
if (c[i] > N)
{
int k = (c[i] - d[1].se + (d[1].fi - 1)) / d[1].fi;
c[i] -= k * d[1].fi;
ans += k;
}
ans += f[c[i]];
}
cout << ans * 2 << '\n';
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin >> t;
while (t--)
{
solve();
}
return 0;
}

浙公网安备 33010602011771号