Codeforces Round #707 (Div. 2, based on Moscow Open Olympiad in Informatics)
Codeforces Round #707 (Div. 2, based on Moscow Open Olympiad in Informatics)
A - Alexey and Train
阅读理解
int main() {
IOS;
for (cin >> _; _; --_) {
cin >> n; int cur = 0;
rep (i ,1, n) cin >> a[i] >> b[i];
rep (i, 1, n) {
cin >> m;
cur += (a[i] - b[i - 1] + m);
if(i == n) break;
cur += b[i] - a[i] + 1 >> 1;
if (cur < b[i]) cur = b[i];
}
cout << cur << '\n';
}
return 0;
}
B - Napoleon Cake
int main() {
IOS;
for (cin >> _; _; --_) {
cin >> n;
rep (i, 1, n) pre[i] = 0;
rep (i, 1, n) {
cin >> m; ++pre[max(1, i - m + 1)];
--pre[i + 1];
}
rep (i, 1, n) pre[i] += pre[i - 1], cout << !!pre[i] << ' ';
cout << '\n';
}
return 0;
}
C - Going Home
假设找不到答案意味着, 最长的a[i] 为
1, 2(1 + 1), 4(2 + 2), 7(4 + 3), 11(7 + 5), 17(11 + 6), ....最长也就2237, 即当n > 2237 必定有解, 暴力判断就行
int n, m, _, k, cas;
int a[N];
unordered_map<int, PII> st;
int main() {
IOS; cin >> n; rep (i ,1, n) cin >> a[i];
rep (i, 1, n) rep (j, i + 1, n) {
auto it = st.find(a[i] + a[j]);
if (it == st.end()) st[a[i] + a[j]] = {i , j};
else if(it->se.fi != i && it->se.fi != j && it->se.se != i && it->se.se != j)
return cout << "YES\n" << i << " " << j << " " << it->se.fi << " " << it->se.se << '\n', 0;
}
cout << "NO\n";
return 0;
}
D - Two chandeliers
设 n < m, 预处理当c[i] 表示从a串的第i个位置开始匹配b串, 匹配到b串最后有多少个位置相同
在暴力找 n, m 的公倍数, 同时计算一圈公倍数能出多少错误, 直接取膜 k, 最后剩下的k, 在一圈公倍数内必定有解
在暴力跑一圈, 故复杂度为 O(n) 常数很大
const int N = 5e5 + 5;
ll n, m, _, k, cas;
int a[N], b[N], h[N << 1];
ll c[N];
int solve(int n, int m, int* a, int* b, int x, int k) {
rep(i, 0, m - 1) {
if (a[(x + i) % n] != b[i]) --k;
if (!k) return i + 1;
}
return 0;
}
void work(int n, int m, int* a, int* b) {
memset(h, -1, sizeof h);
rep(i, 0, n - 1) h[a[i]] = i; ll d = 0, lcm = 0;
rep(i, 0, m - 1) if (h[b[i]] != -1) ++c[((h[b[i]] - i) % n + n) % n];
for (int be = 0; ;) {
d += m - c[be]; be = (be + m % n) % n; lcm += m;
if (!be) break;
}
ll ans = (k - 1) / d * lcm; k = k % d ? k % d : d;
int be = 0;
rep(i, 0, n - 1) {
if (k > m - c[be]) k -= m - c[be], be = (be + m % n) % n, ans += m;
else { cout << ans + solve(n, m, a, b, be, k) << '\n'; return; }
}
}
int main() {
IOS; cin >> n >> m >> k;
rep(i, 0, n - 1) cin >> a[i];
rep(i, 0, m - 1) cin >> b[i];
if (n <= m) work(n, m, a, b);
else work(m, n, b, a);
return 0;
}