Codeforces Round 955 (Div. 2, with prizes from NEAR!)
Codeforces Round 955 (Div. 2, with prizes from NEAR!)
A. Soccer
解题思路:
代码:
#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, c, d;
cin >> a >> b >> c >> d;
if (a < b)
{
if (c >= d)
{
puts("NO");
}
else
{
puts("YES");
}
}
else if (a > b)
{
if (c <= d)
{
puts("NO");
}
else
{
puts("YES");
}
}
else
{
puts("YES");
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
cin >> t;
while (t--)
{
solve();
}
return 0;
}
B. Collatz Conjecture
解题思路:
每次
到达
代码:
#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()
{
ll x, y, k;
cin >> x >> y >> k;
ll a = y - (x % y);
while (k > 0)
{
if (k >= a)
{
k -= a;
}
else
{
x += k;
k = 0;
break;
}
x += a;
while (x % y == 0)
{
x /= y;
if (x == 1)
{
x += (k % (y - 1));
while (x % y == 0)
{
x /= y;
}
k = 0;
}
}
a = y - (x % y);
}
cout << x << "\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. Boring Day
解题思路:
-
在 处一定会将序列切断。 -
单独算一次贡献一定最优。 -
考虑往后接着凑 。-
指针右移。 -
如果凑出来的段在范围内,那么直接算一次贡献。 -
继续往后凑。
-
代码:
#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()
{
ll n, l, r;
cin >> n >> l >> r;
vector<int> a(n + 1);
for (int i = 1; i <= n; i++)
{
cin >> a[i];
}
ll s = 0;
int ans = 0;
for (int i = 1, j = 1; i <= n; i++)
{
while (s < l && j <= n)
{
if (a[j] > r)
{
s = 0;
i = j;
j = i + 1;
break;
}
if (a[j] >= l)
{
ans++;
i = j;
j = i + 1;
s = 0;
break;
}
if (a[j] < l)
{
if (s + a[j] > r)
{
s -= a[i];
break;
}
if (s + a[j] >= l && s + a[j] <= r)
{
s = 0;
ans++;
i = j;
j = i + 1;
break;
}
else if (s + a[j] < l)
{
s += a[j];
j++;
}
}
}
}
cout << ans << "\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. Beauty of the mountains
解题思路:
计算出不同山峰的总和差
如果子矩阵能凑出总和差的因子,那么就有解。
我们考虑每次子矩阵增加最小单位,求所有子矩阵可操作最小单位的公因数
若
代码:
#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;
ll gcd(ll a, ll b)
{
return b ? gcd(b, a % b) : a;
}
void solve()
{
int n, m, k;
cin >> n >> m >> k;
vector<vector<ll>> a(n + 1, vector<ll>(m + 1)), s(n + 1, vector<ll>(m + 1));
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
cin >> s[i][j];
}
}
vector<string> g(n + 1);
for (int i = 1; i <= n; i++)
{
cin >> g[i];
g[i] = ' ' + g[i];
}
ll dis = 0;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
if (g[i][j] == '1')
{
dis += s[i][j];
}
else
{
dis -= s[i][j];
}
}
}
dis = abs(dis);
if (dis == 0)
{
cout << "YES\n";
return;
}
map<ll, bool> vis;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
s[i][j] = (g[i][j] == '1' ? 1 : -1);
}
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
s[i][j] += s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1];
}
}
auto get = [&](int x1, int y1, int x2, int y2) -> ll
{
return s[x2][y2] + s[x1 - 1][y1 - 1] - s[x1 - 1][y2] - s[x2][y1 - 1];
};
set<ll> b;
for (int i = k; i <= n; i++)
{
for (int j = k; j <= m; j++)
{
ll res = get(i - k + 1, j - k + 1, i, j);
b.insert(res);
}
}
ll res = 0;
for (auto x : b)
{
res = gcd(res, x);
}
if (res == 0)
{
cout << "NO\n";
return;
}
if (dis % res == 0)
{
cout << "YES\n";
return;
}
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;
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步