1650A - Deletions of Two Adjacent Letters
Идея: MikeMirzayanov
Разбор
Tutorial is loading...
Решение
#include <bits/stdc++.h>
using namespace std;
#define forn(i, n) for (int i = 0; i < int(n); i++)
int main() {
int t;
cin >> t;
forn(tt, t) {
string s, t;
cin >> s >> t;
bool yes = false;
forn(i, s.length())
if (s[i] == t[0] && i % 2 == 0)
yes = true;
cout << (yes ? "YES" : "NO") << endl;
}
}
Идея: Vladosiya
Разбор
Tutorial is loading...
Решение
#include <bits/stdc++.h>
//#define int long long
#define mp make_pair
#define x first
#define y second
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native")
#pragma GCC optimize("fast-math")
typedef long double ld;
typedef long long ll;
using namespace std;
mt19937 rnd(143);
const ll inf = 1e9;
const ll M = 1e9;
const ld pi = atan2(0, -1);
const ld eps = 1e-4;
void solve() {
int l, r, x;
cin >> l >> r >> x;
int ans = r / x + r % x;
int m = r / x * x - 1;
if(m >= l)ans = max(ans, m / x + m % x);
cout << ans;
}
bool multi = true;
signed main() {
//freopen("in.txt", "r", stdin);
//freopen("in.txt", "w", stdout);
int t = 1;
if (multi) {
cin >> t;
}
for (; t != 0; --t) {
solve();
cout << "\n";
}
return 0;
}
1650C - Weight of the System of Nested Segments
Идея: myav
Разбор
Tutorial is loading...
Решение
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define forn(i, n) for (int i = 0; i < int(n); i++)
struct point{
int weight, position, id;
};
void solve(){
int n, m;
cin >> n >> m;
vector<point>points(m);
forn(i, m) {
cin >> points[i].position >> points[i].weight;
points[i].id = i + 1;
}
sort(points.begin(), points.end(), [] (point a, point b){
return a.weight < b.weight;
});
int sum = 0;
forn(i, m){
if(i < 2 * n) sum += points[i].weight;
else points.pop_back();
}
sort(points.begin(), points.end(), [] (point a, point b){
return a.position < b.position;
});
cout << sum << endl;
forn(i, n){
cout << points[i].id << ' ' << points[2 * n - i - 1].id << endl;
}
}
int main() {
int t;
cin >> t;
while(t--){
solve();
}
return 0;
}
Идея: MikeMirzayanov
Разбор
Tutorial is loading...
Решение
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define forn(i, n) for (int i = 0; i < int(n); i++)
void solve() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
int ans[n];
for (int i = n; i > 0; --i) {
int ind = 0;
for (int j = 0; j < i; ++j) {
ind = a[j] == i ? j : ind;
}
int b[i];
for (int j = 0; j < i; ++j) {
b[(i - 1 - ind + j) % i] = a[j];
}
for (int j = 0; j < i; ++j) {
a[j] = b[j];
}
ans[i - 1] = i != 1 ? (ind + 1) % i : 0;
}
for (int i = 0; i < n; ++i) {
cout << ans[i] << ' ';
}
cout << '\n';
}
int main() {
int tests;
cin >> tests;
forn(tt, tests) {
solve();
}
}
Идея: senjougaharin
Разбор
Tutorial is loading...
Решение
#include <bits/stdc++.h>
#define int long long
#define mp make_pair
#define x first
#define y second
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
typedef long double ld;
typedef long long ll;
using namespace std;
mt19937 rnd(143);
const ll inf = 1e9;
const ll M = 998'244'353;
const ld pi = atan2(0, -1);
const ld eps = 1e-4;
int n, d;
int cnt(vector<int> &schedule){
int mx = 0, mn = inf;
for(int i = 1; i < n; ++i){
mx = max(mx, schedule[i] - schedule[i - 1] - 1);
mn = min(mn, schedule[i] - schedule[i - 1] - 1);
}
return min(mn, max(d - schedule.back() - 1, (mx - 1) / 2));
}
void solve(int test_case) {
cin >> n >> d;
vector<int> a(n + 1);
int mn = d, min_pos = 0;
for(int i = 1; i <= n; ++i){
cin >> a[i];
if(a[i] - a[i - 1] - 1 < mn){
mn = a[i] - a[i - 1] - 1;
min_pos = i;
}
}
vector<int> schedule;
for(int i = 0; i <= n; ++i){
if(i != min_pos){
schedule.push_back(a[i]);
}
}
int ans = cnt(schedule);
if(min_pos > 1){
schedule[min_pos - 1] = a[min_pos];
}
ans = max(ans, cnt(schedule));
cout << ans;
}
bool multi = true;
signed main() {
//freopen("in.txt", "r", stdin);
//freopen("in.txt", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t = 1;
if (multi) {
cin >> t;
}
for (int i = 1; i <= t; ++i) {
solve(i);
cout << "\n";
}
return 0;
}
1650F - Vitaly and Advanced Useless Algorithms
Идея: Aris
Разбор
Tutorial is loading...
Решение
#include <bits/stdc++.h>
using namespace std;
template<class T> bool ckmin(T &a, T b) {return a > b ? a=b, true : false;}
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define sz(v) (int)v.size()
#define all(v) v.begin(),v.end()
struct option {
int t, p, id;
option(int _t,int _p, int _id) : t(_t), p(_p), id(_id) {
}
};
const int INF = INT_MAX >> 1;
vector<int> get_ans(vector<option> &v) {
int n = sz(v);
vector<vector<int>> dp(101, vector<int>(n+1, INF));
dp[0][0] = 0;
for (int k = 1; k <= n; k++) {
auto [t,p,id] = v[k-1];
dp[0][k] = 0;
for (int i = 100; i > 0; i--) {
int prev = max(0,i - p);
dp[i][k] = dp[i][k-1];
ckmin(dp[i][k], dp[prev][k-1] + t);
}
}
vector<int> ans;
int t = dp[100][n];
if (t == INF) return {-1};
for (int i = 100, k = n; k >= 1; k--) {
if (dp[i][k] == dp[i][k-1]) {
continue;
}
ans.emplace_back(v[k-1].id);
i = max(0, i - v[k-1].p);
}
reverse(all(ans));
ans.emplace_back(t);
return ans;
}
void solve(bool flag=true) {
int n,m; cin >> n >> m;
vector<int> a(n);
forn(i, n) {
cin >> a[i];
}
for (int i = n-1; i > 0; i--) {
a[i] -= a[i-1];
}
vector<vector<option>> v(n);
forn(j, m) {
int e,t,p; cin >> e >> t >> p, e--;
v[e].emplace_back(t, p, j+1);
}
vector<int> ans;
forn(i, n) {
vector<int> cur = get_ans(v[i]);
if (sz(cur) == 1 && cur[0] == -1) {
cout << "-1\n";
return;
}
int t = cur.back();
if (t > a[i]) {
cout << "-1\n";
return;
}
cur.pop_back();
if (i+1 < n) a[i+1] += a[i] - t;
ans.insert(ans.end(), all(cur));
}
cout << sz(ans) << '\n';
for (auto e:ans) cout << e << ' ';
cout << '\n';
}
int main() {
int t;
cin >> t;
forn(tt, t) {
solve();
}
}
Идея: MikeMirzayanov
Разбор
Tutorial is loading...
Решение
#include <bits/stdc++.h>
using namespace std;
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define sz(v) (int)v.size()
#define eb emplace_back
#define mt make_tuple
const int INF = INT_MAX >> 1;
const int mod = 1e9 + 7;
void csum(int &a,int b) {
a = (a + b) % mod;
}
int s, t;
vector<int> us;
vector<int> dist;
vector<int> dp[2];
int bfs(vector<vector<int>> &g) {
queue<tuple<int,int,int>> q;
q.push(mt(s, 0, 0)); //[v, dist, count]
int ans = 0, mnd = INF;
us[s] = 1;
dp[0][s] = 1;
dist[s] = 0;
while(!q.empty()) {
auto [v,d, x] = q.front(); q.pop();
// cerr << v << ' ' << d << ' ' << dp[x][v] << endl;
if (v == t) {
if (mnd == INF) {
mnd = d;
}
csum(ans, dp[x][v]);
}
if (d == mnd + 1) continue;
for (int to : g[v]) if(d <= dist[to]) {
dist[to] = min(dist[to], d+1);
csum(dp[d - dist[to] + 1][to], dp[x][v]);
// cerr << "TO: " << to << ' ' << dist[to] << ' ' << d << endl;
if(us[to] == 0 || (us[to] == 1 && dist[to] == d)) q.push(mt(to, d+1, us[to]++));
}
}
return ans;
}
void solve() {
int n,m; cin >> n >> m;
cin >> s >> t;
us.resize(n+1);
dp[0].resize(n+1);
dp[1].resize(n+1);
dist.resize(n+1);
forn(i, n+1) {
us[i] = dp[0][i] = dp[1][i] = 0;
dist[i] = INF;
}
vector<vector<int>> g(n+1);
forn(i, m) {
int a,b; cin >> a >> b;
g[a].eb(b);
g[b].eb(a);
}
cout << bfs(g) << '\n';
}
int main() {
int t;
cin >> t;
forn(tt, t) {
solve();
}
}