A.Food for Animals
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 2e5 + 10;
#define debug(a) cout << #a << ": " << a << '\n';
int t;
int n,m,k;
string s,ts;
int a,b,c,x,y;
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> t;
while(t--){
cin >> a >> b >> c >> x >> y;
x -= a;
y -= b;
if(x >= 0){
c -= x;
x = 0;
}
if(y >= 0){
c -= y;
y = 0;
}
if(c >= 0){
cout << "YES" << endl;
}else {
cout << "NO" << endl;
}
}
}
B.Make It Increasing
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 2e5 + 10;
#define debug(a) cout << #a << ": " << a << '\n';
int t;
int n,m,k;
string s,ts;
int a[N];
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> t;
while(t--){
bool f = 1;
cin >> n;
for(int i = 1;i <= n;i++){
cin >> a[i];
}
int res = 0;
for(int i = n - 1;i >= 1;i--){
if(a[i + 1] == 0){
f = 0;
break;
}
while(a[i] >= a[i + 1]){
a[i] /= 2;
res ++;
}
}
if(f)cout << res << endl;
else cout << -1 << endl;
}
}
C.Detective Task
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 2e5 + 10;
#define debug(a) cout << #a << ": " << a << '\n';
int t;
int n,m,k;
string s,ts;
int a[N];
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> t;
while(t--){
cin >> s;
int res = 0;
int posl = -1,posr = -1;
for(int i = 0;i < s.size();i++){
if(s[i] == '1')posl = i;
else if(s[i] == '0' && posr == -1)posr = i;
}
if(posl == posr && posr == -1){
cout << s.size() << endl;
}else if(posl == -1 && posr != -1){
cout << posr + 1 << endl;
}else if(posl != -1 && posr == -1){
cout << s.size() - posl << endl;
}else {
cout << posr - posl + 1 << endl;
}
}
}
D.Vertical Paths
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 2e5 + 10;
#define debug(a) cout << #a << ": " << a << '\n';
int t;
int n,m,k;
string s,ts;
int a[N];
bool vis[N];
bool leaf[N];
int res = 0;
void dfs(int x,int num)
{
vis[x] = 1;
if(vis[a[x]] || a[x] == x){
cout << num << endl;
cout << x << " ";
return ;
}
dfs(a[x],num + 1);
cout << x << " ";
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> t;
while(t--){
res = 0;
cin >> n;
int root = -1;
for(int i = 1;i <= n;i++){
vis[i] = 0;
}
for(int i = 1;i <= n;i++){
leaf[i] = 0;
}
for(int i = 1;i <= n;i++){
cin >> a[i];
if(a[i] == i)root = i;
else leaf[a[i]] = 1;
}
int sum = 0;
for(int i = 1;i <= n;i++){
if(!leaf[i])sum++;
}
cout << sum << endl;
for(int i = 1;i <= n;i++){
if(!leaf[i]){
dfs(i,1);
cout << endl;
}
}
cout << endl;
}
}
E.Replace With the Previous
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 2e5 + 10;
#define debug(a) cout << #a << ": " << a << '\n';
int t;
int n,m,k;
string s;
map<int,int> mp;
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> t;
while(t--){
mp.clear();
int maxn = -1;
cin >> n >> k;
cin >> s;
for(int i = 0;i < n;i++){
mp[s[i] - 'a']++;
}
for(int i = 0;i < 26;i++){
if(mp[i]){
maxn = max(maxn,i);
}
}
if(k >= maxn){
for(int i = 0;i < n;i++){
cout << 'a';
}
cout << endl;
continue;
}
int l = 0,r = 0;
for(int i = 0; i < n;i++){
int tmp = s[i] - 'a';
if(tmp <= l)continue;
if(tmp - l <= k){
k -= tmp - l;
l = tmp;
}else {
r = tmp - k;
break;
}
}
for(int i = 0;i < s.size();i++){
int tmp = s[i] - 'a';
if(tmp <= l)cout << 'a';
else if(tmp >= r && tmp <= r + k){
cout << (char)('a' + r);
}else {
cout << s[i];
}
}
cout << endl;
}
}
F.Vlad and Unfinished Business
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
typedef pair<int,int>PII;
const int N = 2e5 + 10;
int T;
int n,k;
bool vis[N];
int x,y,a[N];
vector<int>e[N],z[N];
void dfs(int u,int fa)
{
for(auto v:e[u]) {
if(v == fa)
continue;
dfs(v,u);
z[v].push_back(u);
}
}
int get(int u,int d)
{
if(vis[u])
return d;
vis[u] = 1;
for(auto v:z[u]) {
return get(v,d+1);
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie();
cin >> T;
while(T--) {
cin >> n >> k;
cin >> x >> y;
for(int i = 1; i <= n; i ++)
vis[i] = 0,e[i].clear(),z[i].clear();
for(int i = 1; i <= k; i++)cin >> a[i];
for(int i = 1; i < n; i++){
int u,v;
cin >> u >> v;
e[u].push_back(v);
e[v].push_back(u);
}
int ans = 0;
dfs(x,-1);
vis[x] = 1;
ans += get(y,0);
for(int i = 1; i <= k; i++) {
ans += get(a[i],0) * 2;
}
cout << ans << endl;
}
return 0;
}
G.Sorting Pancakes
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 550;
#define debug(a) cout << #a << ": " << a << '\n';
int t;
int n,m;
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
int a[n + 10] = {0};
int sum[n + 10] = {0};
int dp[n + 10][m + 10] = {0};
for(int i = 0;i < n;i++){
cin >> a[i];
sum[i + 1] = sum[i] + a[i];
}
memset(dp,0x3f,sizeof dp);
dp[0][0] = 0;
for(int i = m;i >= 0;i--){
for(int j = 0;j <= m - i;j++){
for(int k = 0;k < n && i * k <= j;k++){
dp[k + 1][j + i] = min(dp[k + 1][j + i],dp[k][j] + abs(j - sum[k]));
}
}
}
cout << dp[n][m] << endl;
}