#include <bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while(t--){
string n;
cin >> n;
long long initial_sum = 0;
int c2 = 0, c3 = 0;
for(char ch : n){
int digit = ch - '0';
initial_sum += digit;
if(digit == 2) c2++;
if(digit == 3) c3++;
}
long long target = (9 - (initial_sum %9)) %9;
bool possible = false;
// Iterate through possible b (number of 3s to replace)
for(int b=0; b<=c3; b++){
// Calculate the residual needed after replacing b 3s
long long temp = (target - 6LL * b) % 9;
if(temp < 0) temp += 9;
// Solve 2*a ≡ temp mod9
// The inverse of 2 mod9 is 5 since 2*5=10≡1 mod9
long long a = (temp * 5) % 9;
// Now, a can be a + 9k where a + 9k <= c2
// Since c2 <=1e5 and a <9, check if a <=c2
if(a <= c2){
// Additionally, check if a + 9k <=c2 for some k
// Since a <=c2, it's possible to have a <=c2
possible = true;
break;
}
}
if(possible){
cout << "YES\n";
}
else{
cout << "NO\n";
}
}
}
Codeforces C sol
CF 🔥🔥
C DONE ✅✅
C. done ✅ ✅
D1 DONE ✅ ✅
D1. DONE ✅ ✅
D1. DONE ✅✅
D1. DONE ✅✅
DMM ⏩
Any code upto d1 in 15
Any code upto d1 in 15
@codercpp001
#include<bits/stdc++.h>
#include<fstream>
using namespace std;
const int mxn = 1e5 + 5, inf = 1e9 + 7;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
int n;
int a[mxn + 1];
void solve(){
cin >> n;
for(int i = 1; i <= n; i++)cin >> a[i];
int ans = inf;
for(int i = 1; i <= n; i++){
int cnt = 0;
for(int j = i + 1; j <= n; j++){
cnt += (a[j] > a[i]);
}
ans = min(ans, cnt + (i - 1));
}
cout << ans << "\n";
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int tt; cin >> tt;
while(tt--){
solve();
}
return(0);
}
B sol
const int MAXN = 1e5 + 5;
int numCases;
int width[MAXN + 1], height[MAXN + 1];
void calculatePerimeter() {
cin >> numCases;
for (int i = 1; i <= numCases; i++) cin >> width[i] >> height[i];
cout << (*max_element(width + 1, width + numCases + 1) + *max_element(height + 1, height + numCases + 1)) * 2 << "\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int testCases;
cin >> testCases;
while (testCases--) {
calculatePerimeter();
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
using i64=long long;
string getmax(string &d,vector<string>&v){
string ans;
for(int i=0;i<v.size();i++)
{
ans+=v[i];
if(i<v.size()-1){
ans+=d;
}
}
return ans;
}
string another(int m,vector<int>&a,int qt,vector<int>&q){
unordered_map<int,int>mpp;
for(int i=1;i<=m;i++){
int raj=(i-1)*(m-i-1)+(m-i);
mpp[raj]++;
if(i<m){
int dog=a[i]-a[i-1]-1;
if(raj>0){
int ravi=i*(m-i);
mpp[ravi]+=raj;
}}
}
vector<string> res;
for(auto it: q){
res.push_back(to_string(mpp[it]));
}
return getmax("",res);
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin>>n;
vector<string>v;
for(int i=0;i<n;i++){
int p,int q;
cin>>p>>q;
vector<int>arr(p);
for(int j=0;j<p;j++){
cin>>arr[j];
}
vector<int>qu(q);
for(int j=0;j<q;j++){
cin>>qu[j];
}
string rahul=another(p,arr,q,qu);
v.push_back(rahul);
}
cout<<getmax("\n",v)<<endl;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while(t--){
string s;
cin >> s;
// Convert string to a mutable vector of integers
vector<int> digits(s.size());
for(int i=0; i<s.size(); i++) digits[i] = s[i] - '0';
// Iterate from left to right
for(int i=1; i<digits.size(); i++){
// While current digit can be swapped left to increase the left digit
while(i >=1 && digits[i] > digits[i-1] +1 && digits[i] >0){
// Perform the operation: swap digits[i] and digits[i-1], after decreasing digits[i] by 1
int temp = digits[i];
digits[i] = digits[i-1];
digits[i-1] = temp -1;
// Move one position to the left to check for further possible operations
if(i >1) i--;
else break;
}
}
// Convert digits back to string
string res = "";
for(int d : digits) res += to_string(d);
cout << res << "\n";
}
}
Codeforces D sol
CODEFORCES DIV 3 ️🔥️🔥
DM FAST
D. DONE ✅✅
D. DONE ✅✅
E. DONE ✅✅
E. DONE ✅✅
E. DONE ✅✅
DM FAST @codercpp001
CF 🔥‼️
A DONE ✅✅
B DONE ✅✅
B DONE ✅✅
C DONE ✅ ✅
C DONE ✅ ✅
C DONE ✅ ✅
Dmm @codercpp001
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<pair<long long, int>> p(n + 1);
for (int i = 1; i <= n; i++) {
cin >> p[i].first;
p[i].first += i - 1;
p[i].second = 0;
}
p[1].first = 0;
sort(p.begin(), p.end());
map<long long, bool> m;
m[n] = true;
for (int i = 2; i <= n; i++) {
if (m[p[i].first])
m[p[i].first + p[i].second] = true;
}
long long r = 0;
for (const auto& [x, y] : m)
if (y)
r = x;
cout << r << "\n";
}
}
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
int mod = 1e9 + 7;
void solution() {
int t;
cin>>t;
while(t--){
int n;
cin>>n;
std::vector<int>arr(n) ;
for(int i=0; i<n; i++){
cin>>arr[i];
}
int bpnt;
bool fl = 0;
for(int i=1; i<n; i++){
if(arr[i] < arr[i-1]){
bpnt =i-1;
fl=1;
break;
}
}
int cnt = bpnt;
while(upper_bound(arr.begin() , arr.end() , arr[bpnt]) != arr.end()){
int k = upper_bound(arr.begin() , arr.end() , arr[bpnt]) - arr.begin();
arr[k] = -1;
cnt++;
}
cout<<cnt<<'\n';
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
solution();
return 0;
}
#include <bits/stdc++.h>
using namespace std;
void solve(){
int n;
cin >> n;
vector<pair<long long, int> > a(n + 1);
for (int i = 1; i <= n; i ++){
cin >> a[i].first;
a[i].first += i - 1;
a[i].second = i - 1;
}
a[1].first = 0;
sort(a.begin(), a.end());
map<long long, bool> dp;
dp[n] = true;
for (int i = 2; i <= n; i ++){
if (dp[a[i].first])
dp[a[i].first + a[i].second] = true;
}
long long ans = 0;
for (auto [x, y] : dp)
if (y)
ans = x;
cout << ans << "\n";
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int tests;
cin >> tests;
while (tests --)
solve();
return 0;
}
CF 🔥🔥
B. DONE ✅✅
C. done ✅ ✅
D1 DONE ✅ ✅
D1. DONE ✅ ✅
D1. DONE ✅✅
D1. DONE ✅✅
DMM ⏩
@codercpp001
#include <bits/stdc++.h>
using namespace std;
void solve(){
int n;
cin >> n;
vector<pair<long long, int> > a(n + 1);
for (int i = 1; i <= n; i ++){
cin >> a[i].first;
a[i].first += i - 1;
a[i].second = i - 1;
}
a[1].first = 0;
sort(a.begin(), a.end());
map<long long, bool> dp;
dp[n] = true;
for (int i = 2; i <= n; i ++){
if (dp[a[i].first])
dp[a[i].first + a[i].second] = true;
}
long long ans = 0;
for (auto [x, y] : dp)
if (y)
ans = x;
cout << ans << "\n";
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int tests;
cin >> tests;
while (tests --)
solve();
return 0;
}
#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
#include <map>
using namespace std;
typedef long long ll;
int t;
ll n;
vector<ll> arr;
unordered_map<ll, vector<ll>> mp;
map<ll, ll> dp;
ll solve(ll s) {
if (dp.count(s)) return dp[s];
ll ans = s;
if (mp.count(s)) {
for (ll i : mp[s]) {
ll s_new = s + i - 1;
ans = max(ans, solve(s_new));
}
}
return dp[s] = ans;
}
int main() {
cin >> t;
while (t--) {
cin >> n;
arr.resize(n + 1); // 1-based indexing
mp.clear();
dp.clear();
for (ll i = 1; i <= n; ++i) {
cin >> arr[i];
}
for (ll i = 2; i <= n; ++i) {
ll s_i = arr[i] + i - 1;
mp[s_i].push_back(i);
}
ll result = solve(n);
cout << result << '\n';
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int tc; cin >> tc;
while (tc--) {
int n;
cin >> n;
vector<int>a(n+1);
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 2; i <= n ; i++) {
if (a[i] == a[i - 1] || a[n - i + 1] == a[n - i + 2])
swap(a[i], a[n - i + 1]);
}
int ans = 0;
for (int i = 1; i < n; i++) {
if (a[i] == a[i + 1])
ans++;
}
cout << ans << '\n';
}
}