Author: daha.002
Tutorial
Tutorial is loading...
Solution
#include <iostream>
#include <vector>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, m, k;
cin >> n >> m >> k;
int ans = 0;
vector<int> v1(n);
for (int i = 0; i < n; i++) {
cin >> v1[i];
}
int o;
for (int j = 0; j < m; j++) {
cin >> o;
for (int i = 0; i < n; i++) {
if ((o + v1[i]) <= k) ans++;
}
}
cout << ans << "\n";
}
return 0;
}
Author: Vladosiya
Tutorial
Tutorial is loading...
Solution
def solve():
n = int(input())
a = [int(x) for x in input().split()]
for i in range(n - 2):
if a[i] < 0:
print('NO')
return
op = a[i]
a[i] -= op
a[i + 1] -= 2 * op
a[i + 2] -= op
if a[-1] != 0 or a[-2] != 0:
print('NO')
else:
print('YES')
for _ in range(int(input())):
solve()
1941C - Rudolf and the Ugly String
Author: Mordvin13
Tutorial
Tutorial is loading...
Solution
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int main() {
long long t;
cin>>t;
for(long long c=0;c<t;c++){
long long n;
cin >> n;
string s;
cin >> s;
vector<long long> va;
for (string sul : {"mapie", "pie", "map"}) {
for (size_t pos = 0; (pos = s.find(sul, pos)) != string::npos;) {
s[pos + sul.length() / 2] = '?';
va.push_back(pos + sul.length() / 2);
}
}
cout << va.size() << endl;
}
return 0;
}
1941D - Rudolf and the Ball Game
Author: Alexey_Parsh
Tutorial
Tutorial is loading...
Solution
#include <iostream>
#include <set>
using namespace std;
int main()
{
int t; cin >> t;
while (t--) {
int n, m, a; cin >> n >> m >> a;
set <int> q[2];
int ix = 0;
q[ix].insert(a);
while (m--) {
int x; char ch; cin >> x >> ch;
while (!q[ix].empty()) {
int u = *(q[ix].begin());
q[ix].erase(u);
if (ch == '?' || ch == '0') {
q[ix ^ 1].insert((u + x - 1) % n + 1);
}
if (ch == '?' || ch == '1') {
q[ix ^ 1].insert((u - x - 1 + n) % n + 1);
}
}
ix ^= 1;
}
cout << q[ix].size() << '\n';
for (auto& x : q[ix]) {
cout << x << ' ';
}
cout << '\n';
}
return 0;
}
Author: t0rtik
Tutorial
Tutorial is loading...
Solution
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
int main() {
int t = 1;
cin >> t;
while (t--) {
int N, M, K, D;
cin >> N >> M >> K >> D;
vector<long long> a(N);
for (int i = 0; i < N; i++) {
vector<long long> dp(M, 1e9);
vector<int> v(M);
multiset<long long> mst = {1};
dp[0] = 1;
cin >> v[0];
for (int j = 1; j < M - 1; j++) {
cin >> v[j];
dp[j] = *mst.begin() + v[j] + 1;
if (j - D - 1 >= 0)
mst.erase(mst.find((dp[j - D - 1])));
mst.insert(dp[j]);
}
cin >> v.back();
dp.back() = 1 + *mst.begin();
a[i] = dp.back();
}
long long cur = 0;
for (int i = 0; i < K; i++)
cur += a[i];
long long mn = cur;
for (int i = K; i < N; i++) {
cur += a[i] - a[i - K];
mn = min(cur, mn);
}
cout << mn << endl;
}
return 0;
}
Author: Vladosiya
Tutorial
Tutorial is loading...
Solution
def solve():
n, m, k = map(int, input().split())
a = [int(x) for x in input().split()]
d = [int(x) for x in input().split()]
f = [int(x) for x in input().split()]
d.sort()
f.sort()
m1, m2 = 0, 0
ind = -1
for i in range(1, n):
e = a[i] - a[i - 1]
m2 = max(m2, e)
if m2 > m1:
m1, m2 = m2, m1
ind = i - 1
ans = m1
target = (a[ind] + a[ind + 1]) // 2
for model in d:
l, r = 0, k - 1
while r - l > 1:
mid = (r + l) // 2
if model + f[mid] <= target:
l = mid
else:
r = mid
ans = min(ans, max(m2, abs(model + f[l] - a[ind]), abs(model + f[l] - a[ind + 1])))
ans = min(ans, max(m2, abs(model + f[r] - a[ind]), abs(model + f[r] - a[ind + 1])))
print(ans)
for _ in range(int(input())):
solve()
Author: natalina
Tutorial
Tutorial is loading...
Solution
#include<bits/stdc++.h>
using LL = long long;
using ld = long double;
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cout << fixed << setprecision(10);
int _ = 0, __ = 1;
cin >> __;
for (int _ = 0; _ < __; ++_) {
int n, m;
cin >> n >> m;
vector<vector<pair<int, int>>> g(n + 1);
map<int, int> clrs;
int idx = n + 1;
for (int i = 0; i < m; ++i) {
int u, v, c;
cin >> u >> v >> c;
g[u].push_back({v, c});
g[v].push_back({u, c});
if (!clrs.count(c)) {
clrs[c] = idx;
idx++;
}
}
int s, t;
cin >> s >> t;
if (s == t)
{
cout << 0 << endl;
continue;
}
vector<set<int>> bg(n + clrs.size() + 3);
for (int i = 1; i <= n; ++i) {
for(auto &[to, c] : g[i])
{
int clr_v = clrs[c];
bg[i].insert(clr_v);
bg[clr_v].insert(i);
bg[to].insert(clr_v);
bg[clr_v].insert(to);
}
}
vector<int> used(bg.size());
vector<int> d(bg.size(), -1);
auto bfs = [&](int v)
{
queue<int> q;
q.push(v);
used[v] = 1;
d[v] = 0;
while (!q.empty())
{
auto from = q.front();
q.pop();
for (auto& to : bg[from])
{
if (!used[to])
{
q.push(to);
used[to] = 1;
d[to] = d[from] + 1;
}
}
}
};
bfs(s);
if (d[t] > 0) cout << d[t] / 2 << endl;
else cout << -1 << endl;
}
return 0;
}