feat: Add solutions for Codeforces problems 1997 A, B, C, and D

This commit is contained in:
ShazidMahsrafi 2024-08-01 04:42:14 +06:00
parent 2cbe3ba219
commit 70c8441a83
5 changed files with 255 additions and 0 deletions

View File

@ -0,0 +1,53 @@
#include <bits/stdc++.h>
using namespace std;
#ifdef ONLINE_JUDGE
#define dbg(...)
#else
#include "Assets/debug.h"
#endif
#define int long long
#define ll long long
#define endl '\n'
#define ff first
#define ss second
#define ins insert
#define pb push_back
#define ppb pop_back
#define sz(x) (int)(x).size()
#define all(x) x.begin(), x.end()
#define rep(i,a,b) for(int i=a; i<b; ++i)
#define rrep(i,a,b) for(int i=a; i>=b; --i)
#define yn(f) f? cout<<"YES\n":cout<<"NO\n"
#define FAST (ios_base::sync_with_stdio(false), cin.tie(nullptr));
ll pow(ll x,ll y,ll m=1e9+7) {ll ans=1;x%=m;while(y){if(y&1)ans=(ans*x)%m;x=(x*x)%m;y>>=1;}return ans;}
void solve()
{
string s;
cin>>s;
for(int i=1; i<s.size(); ++i)
{
if(s[i]==s[i-1])
{
if(s[i]=='a') s.insert(s.begin()+i,'b');
else s.insert(s.begin()+i,'a');
cout<<s<<endl;
return;
}
}
if(s.back()=='a') s.append("b");
else s.append("a");
cout<<s<<endl;
}
signed main()
{
FAST;
int TCS = 1;
cin >> TCS;
for (int TC = 1; TC <= TCS; ++TC)
{
// cout<<"Case "<<TC<<": ";
solve();
}
}

View File

@ -0,0 +1,65 @@
#include <bits/stdc++.h>
using namespace std;
#ifdef ONLINE_JUDGE
#define dbg(...)
#else
#include "Assets/debug.h"
#endif
#define int long long
#define ll long long
#define endl '\n'
#define ff first
#define ss second
#define ins insert
#define pb push_back
#define ppb pop_back
#define sz(x) (int)(x).size()
#define all(x) x.begin(), x.end()
#define rep(i,a,b) for(int i=a; i<b; ++i)
#define rrep(i,a,b) for(int i=a; i>=b; --i)
#define yn(f) f? cout<<"YES\n":cout<<"NO\n"
#define FAST (ios_base::sync_with_stdio(false), cin.tie(nullptr));
ll pow(ll x,ll y,ll m=1e9+7) {ll ans=1;x%=m;while(y){if(y&1)ans=(ans*x)%m;x=(x*x)%m;y>>=1;}return ans;}
void solve()
{
int n;
cin>>n;
vector<string>v(2);
for(auto &i:v) cin>>i;
dbg(n,v);
int ans=0;
rep(i,0,2)
{
rep(j,0,n)
{
int ct=0;
if(j+1<n && v[i][j+1]=='.') ct++;
if(j-1>=0 && v[i][j-1]=='.') ct++;
dbg(ct);
if(ct<2) continue;
if(i==0)
{
if(v[i+1][j]=='.' && v[i+1][j-1]=='x' && v[i+1][j+1]=='x') ans++;
}
else
{
if(v[i-1][j]=='.' && v[i-1][j-1]=='x' && v[i-1][j+1]=='x') ans++;
}
}
}
cout<<ans<<endl;
dbg(ans);
}
signed main()
{
FAST;
int TCS = 1;
cin >> TCS;
for (int TC = 1; TC <= TCS; ++TC)
{
// cout<<"Case "<<TC<<": ";
solve();
}
}

View File

@ -0,0 +1,72 @@
#include <bits/stdc++.h>
using namespace std;
#ifdef ONLINE_JUDGE
#define dbg(...)
#else
#include "Assets/debug.h"
#endif
#define int long long
#define ll long long
#define endl '\n'
#define ff first
#define ss second
#define ins insert
#define pb push_back
#define ppb pop_back
#define sz(x) (int)(x).size()
#define all(x) x.begin(), x.end()
#define rep(i,a,b) for(int i=a; i<b; ++i)
#define rrep(i,a,b) for(int i=a; i>=b; --i)
#define yn(f) f? cout<<"YES\n":cout<<"NO\n"
#define FAST (ios_base::sync_with_stdio(false), cin.tie(nullptr));
ll pow(ll x,ll y,ll m=1e9+7) {ll ans=1;x%=m;while(y){if(y&1)ans=(ans*x)%m;x=(x*x)%m;y>>=1;}return ans;}
void solve()
{
int n;
string s;
cin>>n>>s;
int ct=0,cost=0;
vector<int>v;
for(int i=0; i<n; ++i)
{
if(s[i]=='(')
{
ct++;
v.push_back(i);
}
else if(s[i]==')')
{
ct--;
cost += i-v.back();
v.pop_back();
}
else
{
if(ct)
{
ct--;
cost += i-v.back();
v.pop_back();
}
else
{
ct++;
v.push_back(i);
}
}
}
cout<<cost<<endl;
}
signed main()
{
FAST;
int TCS = 1;
cin >> TCS;
for (int TC = 1; TC <= TCS; ++TC)
{
// cout<<"Case "<<TC<<": ";
solve();
}
}

View File

@ -0,0 +1,61 @@
#include <bits/stdc++.h>
using namespace std;
#ifdef ONLINE_JUDGE
#define dbg(...)
#else
#include "Assets/debug.h"
#endif
#define int long long
#define ll long long
#define endl '\n'
#define ff first
#define ss second
#define ins insert
#define pb push_back
#define ppb pop_back
#define sz(x) (int)(x).size()
#define all(x) x.begin(), x.end()
#define rep(i,a,b) for(int i=a; i<b; ++i)
#define rrep(i,a,b) for(int i=a; i>=b; --i)
#define yn(f) f? cout<<"YES\n":cout<<"NO\n"
#define FAST (ios_base::sync_with_stdio(false), cin.tie(nullptr));
ll pow(ll x,ll y,ll m=1e9+7) {ll ans=1;x%=m;while(y){if(y&1)ans=(ans*x)%m;x=(x*x)%m;y>>=1;}return ans;}
void solve()
{
int n;
cin>>n;
vector<int>adj[n], v(n);
for(auto &i:v) cin>>i;
rep(i,1,n)
{
int p;
cin>>p;
p--;
adj[p].pb(i);
}
function<int (int node)> dfs = [&](int node)->int
{
int mn = INT_MAX;
for(auto child:adj[node])
mn = min(mn, dfs(child));
dbg(mn);
if(node == 0) return mn+v[0];
if(mn == INT_MAX) return v[node];
if(v[node] >= mn) return mn;
return (mn+v[node])/2;
};
cout<<dfs(0)<<endl;
}
signed main()
{
FAST;
int TCS = 1;
cin >> TCS;
for (int TC = 1; TC <= TCS; ++TC)
{
// cout<<"Case "<<TC<<": ";
solve();
}
}

View File

@ -377,6 +377,10 @@ This repository contains my solutions of Codeforces problems. They are in C++ la
| 365 | 1996 B | Scale | [Question](https://codeforces.com/problemset/problem/1996/B) | [Solution](https://github.com/ShazidMashrafi/Codeforces-Solutions/tree/master/Codes/1996%20B%20-%20Scale)
| 366 | 1996 C | Sort | [Question](https://codeforces.com/problemset/problem/1996/C) | [Solution](https://github.com/ShazidMashrafi/Codeforces-Solutions/tree/master/Codes/1996%20C%20-%20Sort)
| 367 | 1996 D | Fun | [Question](https://codeforces.com/problemset/problem/1996/D) | [Solution](https://github.com/ShazidMashrafi/Codeforces-Solutions/tree/master/Codes/1996%20D%20-%20Fun)
| 368 | 1997 A | Strong Password | [Question](https://codeforces.com/problemset/problem/1997/A) | [Solution](https://github.com/ShazidMashrafi/Codeforces-Solutions/tree/master/Codes/1997%20A%20-%20Strong%20Password)
| 369 | 1997 B | Make Three Regions | [Question](https://codeforces.com/problemset/problem/1997/B) | [Solution](https://github.com/ShazidMashrafi/Codeforces-Solutions/tree/master/Codes/1997%20B%20-%20Make%20Three%20Regions)
| 370 | 1997 C | Even Positions | [Question](https://codeforces.com/problemset/problem/1997/C) | [Solution](https://github.com/ShazidMashrafi/Codeforces-Solutions/tree/master/Codes/1997%20C%20-%20Even%20Positions)
| 371 | 1997 D | Maximize the Root | [Question](https://codeforces.com/problemset/problem/1997/D) | [Solution](https://github.com/ShazidMashrafi/Codeforces-Solutions/tree/master/Codes/1997%20D%20-%20Maximize%20the%20Root)