Codeforces Round 929 (Div. 3)
A - Turtle Puzzle: Rearrange and Negate
代码:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<ll, ll>;
#define fi first
#define se second
using i128 = __int128_t;
using piii = pair<ll, pair<ll, ll>>;
void solve()
{
int n;
cin >> n;
ll ans = 0;
for (int i = 1; i <= n; i++)
{
int x;
cin >> x;
ans += abs(x);
}
cout << ans << endl;
}
int main()
{
int t = 1;
cin >> t;
while (t--)
{
solve();
}
return 0;
}
B - Turtle Math: Fast Three Task
解题思路:
如果当前数组总和为\(3\)的倍数,无需操作。
如果当前数组总和\(res \%3 = 1\),那么尝试减去一个模\(3\)等于\(1\)的元素,如果存在,那么操作数为\(1\);如果不存在,选择任意一个数加两次\(1\)即可。
代码:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<ll, ll>;
#define fi first
#define se second
using i128 = __int128_t;
using piii = pair<ll, pair<ll, ll>>;
void solve()
{
int n;
cin >> n;
ll res = 0;
vector<int> cnt(5);
for (int i = 1; i <= n; i++)
{
int x;
cin >> x;
res += x;
cnt[x % 3]++;
}
int t = res % 3;
if (t == 0)
{
cout << 0 << endl;
return;
}
if (t == 1)
{
if (cnt[1] > 0)
{
cout << 1 << endl;
}
else
{
cout << 2 << endl;
}
}
else
{
cout << 1 << endl;
}
}
int main()
{
int t = 1;
cin >> t;
while (t--)
{
solve();
}
return 0;
}
C - Turtle Fingers: Count the Values of k
解题思路:
\(a,b\)增长速度很快,枚举即可。
代码:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<ll, ll>;
#define fi first
#define se second
using i128 = __int128_t;
using piii = pair<ll, pair<ll, ll>>;
ll qmi(ll a, ll b)
{
ll res = 1;
while (b)
{
if (b & 1)
{
res = res * a;
}
a = a * a;
b >>= 1;
}
return res;
}
void solve()
{
ll a, b, l;
cin >> a >> b >> l;
map<ll, bool> cnt;
for (int i = 0; i <= 20; i++)
{
if (qmi(a, i) > l)
{
break;
}
for (int j = 0; j <= 20; j++)
{
if (qmi(b, j) > l)
{
break;
}
ll t = qmi(a, i) * qmi(b, j);
if (t > l)
{
break;
}
if (l % t == 0)
{
cnt[l / t] = true;
}
}
}
cout << cnt.size() << endl;
}
int main()
{
int t = 1;
cin >> t;
while (t--)
{
solve();
}
return 0;
}
D - Turtle Tenacity: Continual Mods
解题思路:
升序排序。
若最小数只有一个,模到最后得到的就是最小数。
有多个最小数,分情况讨论:
- 若最小数是\(1\),无解。
- 否则尝试用大数模最小数,看是否能得到小于当前最小数的余数,若能得到,有解;否则,说明后面的数都是最小数的倍数,最小数一定会和相同的自己模得到零,无解。
代码:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<ll, ll>;
#define fi first
#define se second
using i128 = __int128_t;
using piii = pair<ll, pair<ll, ll>>;
void solve()
{
int n;
cin >> n;
vector<int> a(n + 1);
map<int, int> cnt;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
cnt[a[i]]++;
}
sort(a.begin() + 1, a.end());
if (cnt.size() == n || a[1] != a[2])
{
puts("YES");
}
else
{
if (cnt[1] > 1)
{
puts("NO");
}
else
{
if (cnt[1] == 1)
{
puts("YES");
}
else
{
for (int i = 3; i <= n; i++)
{
if (a[i] % a[1])
{
puts("YES");
return;
}
}
puts("NO");
}
}
}
}
int main()
{
int t = 1;
cin >> t;
while (t--)
{
solve();
}
return 0;
}
E - Turtle vs. Rabbit Race: Optimal 训练
解题思路:
观察发现,随着\(r\)从左到右遍历,成绩先增大后减小。具有凸性。三分求最优答案。
代码:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<ll, ll>;
#define fi first
#define se second
using i128 = __int128_t;
using piii = pair<ll, pair<ll, ll>>;
void solve()
{
int n;
cin >> n;
vector<ll> a(n + 10);
for (int i = 1; i <= n; i++)
{
cin >> a[i];
a[i] += a[i - 1];
}
int q;
cin >> q;
while (q--)
{
ll tl, u;
cin >> tl >> u;
ll l = tl - 1;
ll r = n + 1;
auto f = [&](ll mid) -> ll
{
if (mid < tl)
{
return -1e18;
}
ll len = a[mid] - a[tl - 1];
ll st = u;
ll ed = u - len + 1;
ll res = (st + ed) * len / 2;
return res;
};
while (l + 1 < r)
{
ll mid = (r - l) / 3;
ll ml = l + mid;
ll mr = r - mid;
if (f(mr) > f(ml))
{
l = ml;
}
else
{
r = mr;
}
if (mid == 0)
{
break;
}
}
ll ta = f(l);
ll tb = f(l + 1);
ll tc = f(l + 2);
if (l == n)
{
cout << l << ' ';
}
else if (l == n - 1)
{
if (ta >= tb)
{
cout << l << ' ';
}
else
{
cout << l + 1 << ' ';
}
}
else if (l <= n - 2)
{
if (ta >= tb && ta >= tc)
{
cout << l << ' ';
}
else if (tb >= tc)
{
cout << l + 1 << ' ';
}
else
{
cout << tc << ' ';
}
}
}
cout << endl;
}
int main()
{
int t = 1;
cin >> t;
while (t--)
{
solve();
}
return 0;
}
F - Turtle Mission: Robot and the Earthquake
解题思路:
考虑和石头的相对位置变化。向下走等于相对向下走两步,向右走等于向右下走一步,向上等于不动。
所以,如果可以先向下或者向右,然后在向上。我们一定也可以先向上,然后向下或者向右。
考虑先通过向下向右走到最后一列,最后通过向上调整位置走到终点。
最短路\(bfs\)。
注意:若根据相对位置移动,由于每次都有一个向下的相对偏移量,在最后一列向上移动之前,记得要减回去。
代码:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<ll, ll>;
#define fi first
#define se second
using i128 = __int128_t;
using piii = pair<ll, pair<ll, ll>>;
void solve()
{
int n, m;
cin >> n >> m;
vector<vector<int>> g(n + 1, vector<int>(m + 1, 0));
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cin >> g[i][j];
}
}
queue<pii> q;
q.push({0, 0});
vector<vector<int>> dis(n + 1, vector<int>(m + 1, 0));
int ans = 1e9;
while (q.size())
{
auto [x, y] = q.front();
q.pop();
// 在最后一列向上走到终点
if (y == m - 1)
{
// 由于每次都有一个向下的相对偏移量,这里记得要减回去
ans = min(1ll * ans, dis[x][y] + ((x - (n - 1 + dis[x][y]) % n + n) % n));
}
int nx = (x + 1) % n;
int ny = y + 1;
// 右下走一步
if (ny <= m - 1 && g[nx][ny] == 0 && !dis[nx][ny])
{
dis[nx][ny] = dis[x][y] + 1;
q.push({nx, ny});
}
nx = (x + 2) % n;
ny = y;
// 向下走两步
if (g[(x + 1) % n][y] != 1 && g[(x + 2) % n][y] != 1 && !dis[nx][ny])
{
dis[nx][ny] = dis[x][y] + 1;
q.push({nx, ny});
}
}
if (ans == 1e9)
{
puts("-1");
}
else
{
cout << ans << endl;
}
}
int main()
{
int t = 1;
cin >> t;
while (t--)
{
solve();
}
return 0;
}
G - Turtle Magic: Royal Turtle Shell Pattern
解题思路:
横着标准图:
\[0011\\1100\\0011\\1100 \]竖着标准图:
\[0101 \\0101\\ 1010\\ 1010 \]横图分别选择第\(1, 2, 3,4\)列作为第一列,就是四种情况。竖图分别选择第\(1, 2, 3,4\)行作为第一行,就是四种情况。
总共有八种情况。
对于加入的每个字符,我们一次判断是否符合八种情况即可。
代码:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<ll, ll>;
#define fi first
#define se second
using i128 = __int128_t;
using piii = pair<ll, pair<ll, ll>>;
void solve()
{
int n, m, q;
cin >> n >> m >> q;
cout << 8 << endl;
int ans = 8;
vector<bool> vis(10, false);
while (q--)
{
int x, y;
cin >> x >> y;
string s;
cin >> s;
auto find = [&](int x, int y, int k)
{
// 偏移到标准图上
// 标准图:0011
// 标准图:1100
if (k < 4)
{
y += k;
// 在 0011
if (x & 1)
{
return y % 4 == 3 || y % 4 == 0;
}
// 在 1100
else
{
return y % 4 == 1 || y % 4 == 2;
}
}
// 标准图:01
// 标准图:01
// 标准图:10
// 标准图:10
else
{
x += k;
// 竖着 0011
if (y & 1)
{
return x % 4 == 3 || x % 4 == 0;
}
// 竖着 1100
else
{
return x % 4 == 1 || x % 4 == 2;
}
}
};
if (s[0] == 'c')
{
int t = 0;
for (int i = 0; i < 8; i++)
{
if (!vis[i])
{
if (find(x, y, i) != t)
{
vis[i] = true;
ans--;
}
}
}
}
else
{
int t = 1;
for (int i = 0; i < 8; i++)
{
if (!vis[i])
{
if (find(x, y, i) != t)
{
vis[i] = true;
ans--;
}
}
}
}
cout << ans << endl;
}
}
int main()
{
int t = 1;
cin >> t;
while (t--)
{
solve();
}
return 0;
}
标签:return,int,ll,Codeforces,long,929,using,Div,define
From: https://www.cnblogs.com/value0/p/18042604