Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Top C2j ladder problems #22

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions B_2_Wonderful_Coloring_-_2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define ff first
#define ss second
#define PI 3.141592653589793238462
#define set_bits __builtin_popcountll
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define loop for(int i = 0; i < n; i++)
using namespace std;

int countDigit(long long n){
return floor(log10(n) + 1);
}
//-----------------------------------------------------------------------------
void solve(){
int n, k; cin >> n >> k;
int a[n];
for(int i = 0; i < n; i++){
cin >> a[i];
}
map<int, int> colorct;
map<int, int> m;
vector<vector<int>> v;
for(int i = 0; i < n; i++){
v.push_back({a[i], i, 0});
m[a[i]]++;
}
sort(v.begin(), v.end());
int ct = 0;
for(auto &it: m){
ct += min(k, it.second);
}
ct = ct/k;

// cout << "the v" << endl;
// for(int i = 0; i < n; i++){
// cout << v[i][0] << " " << v[i][1] << " " << v[i][2] << endl;
// }

// cout << " theh count " << ct << endl;

int color = 1;
v[0][2] = color;
int vict = 1;
colorct[color]++;
for(int i = 1; i < n; i++){
if(v[i][0] == v[i-1][0]){
vict++;
if(vict > k) continue;
}
else vict = 1;
if(vict > k) i++;
color++;
if(color > k) color = 1;
if(i<n){
if(colorct[color] < ct) v[i][2] = color;
colorct[color]++;
}
}

// cout << "the v agAIN " << endl;
// for(int i = 0; i < n; i++){
// cout << v[i][0] << " " << v[i][1] << " " << v[i][2] << endl;
// }

vector<pair<int, int>> lol;
for(int i = 0; i < n; i++){
lol.push_back({v[i][1], v[i][2]});
}
sort(lol.begin(), lol.end());

for(int i = 0; i < n; i++){
cout << lol[i].second << " ";
}
cout << endl;
}
//----------------------------------------------------------------------------
int32_t main(){
int t; cin >> t;
while(t--){
solve();
}
return 0;
}
44 changes: 44 additions & 0 deletions B_Fun_with_Even_Subarrays.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define ff first
#define ss second
#define PI 3.141592653589793238462
#define set_bits __builtin_popcountll
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define loop for(int i = 0; i < n; i++)
using namespace std;

int countDigit(long long n){
return floor(log10(n) + 1);
}
//-----------------------------------------------------------------------------
void solve(){
int n; cin >> n;
int a[n];
for(int i = 0; i < n; i++){
cin >> a[i];
}
int cnt = 0, x = a[n-1], i = n-1, step = 0;
while(i >= 0){
while(a[i]==x && i >= 0){
cnt++, i--;
}
if(i<0) break;
step++;
i -= cnt;
cnt = cnt*2;
}
cout << step << endl;
}
//----------------------------------------------------------------------------
int32_t main(){
int t; cin >> t;
while(t--){
solve();
}
return 0;
}
35 changes: 35 additions & 0 deletions B_Funny_Permutation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define ff first
#define ss second
#define PI 3.141592653589793238462
#define set_bits __builtin_popcountll
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define loop for(int i = 0; i < n; i++)
using namespace std;

int countDigit(long long n){
return floor(log10(n) + 1);
}
//-----------------------------------------------------------------------------
void solve(){
int n; cin >> n;
if(n==3) cout << -1 << endl;
else{
cout << n << " " << n-1 << " ";
for(int i = 1; i <= n-2; i++) cout << i << " ";
cout << endl;
}
}
//----------------------------------------------------------------------------
int32_t main(){
int t; cin >> t;
while(t--){
solve();
}
return 0;
}
36 changes: 36 additions & 0 deletions B_GCD_Length.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include<bits/stdc++.h>
// #define int long long
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define ff first
#define ss second
#define PI 3.141592653589793238462
#define set_bits __builtin_popcountll
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define loop for(int i = 0; i < n; i++)
using namespace std;

int countDigit(long long n){
return floor(log10(n) + 1);
}
//-----------------------------------------------------------------------------
void solve(){
int a, b, c;
cin >> a >> b >> c;
int s = 7, t = 3, ans = 1;
ans *= pow(10, c-1);
int ans2 = ans;
while(ceil(log10(ans + 1)) < a) ans *= s;
while(ceil(log10(ans2 + 1)) < b) ans2 *= t;
cout << ans << " " << ans2 << endl;
}
//----------------------------------------------------------------------------
int32_t main(){
int t; cin >> t;
while(t--){
solve();
}
return 0;
}
71 changes: 71 additions & 0 deletions B_Minor_Reduction.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define ff first
#define ss second
#define PI 3.141592653589793238462
#define set_bits __builtin_popcountll
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define loop for(int i = 0; i < n; i++)
using namespace std;

int countDigit(long long n){
return floor(log10(n) + 1);
}
//-----------------------------------------------------------------------------
void solve(){
string s; cin >> s;
int n = s.length();
// cout << n << endl;
int flag = 0;
int num[n];
loop{num[i] = s[i]-'0';}
for(int i = n-1; i > 0; i--){
if(num[i]+num[i-1] >= 10){
int temp = num[i]+num[i-1];
num[i] = temp%10;
num[i-1] = temp / 10;
flag = 1;
break;
}
}
// cout << flag << endl;
if(!flag){
// cout << "chala" << endl;
int index = -1;
int temp;
for(int i = 0; i < n-1; i++){
temp = num[i]+num[i+1];
// cout << temp << endl;
if(temp >= num[i]){
index = i;
flag = 1;
break;
}
}
if(flag){
for(int i = 0; i < n; i++){
if(i == index){
cout << temp ;
i++;
}
else cout << num[i];
}
cout << endl;
return;
}
}
loop{cout << num[i];}
cout << endl;
}
//----------------------------------------------------------------------------
int32_t main(){
int t; cin >> t;
while(t--){
solve();
}
return 0;
}
51 changes: 51 additions & 0 deletions B_Mirror_in_the_String.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define ff first
#define ss second
#define PI 3.141592653589793238462
#define set_bits __builtin_popcountll
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define loop for(int i = 0; i < n; i++)
using namespace std;

int countDigit(long long n){
return floor(log10(n) + 1);
}
//-----------------------------------------------------------------------------
void solve(){
int n; cin >> n;
char a[n];
for(int i = 0; i < n; i++){
cin >> a[i];
}
if(n==1){
cout << a[0] << a[0] << endl;
return;
}
if(a[0] == a[1]){
cout << a[0] << a[0] << endl;
return;
}
vector<char> b;
b.push_back(a[0]);
for(int i = 1; i < n; i++){
if(a[i] <= a[i-1]) b.pb(a[i]);
else break;
}
for(auto &it: b) cout << it;
reverse(b.begin(), b.end());
for(auto &it: b) cout << it;
cout << endl;
}
//----------------------------------------------------------------------------
int32_t main(){
int t; cin >> t;
while(t--){
solve();
}
return 0;
}
27 changes: 27 additions & 0 deletions B_Ordinary_Numbers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <bits/stdc++.h>
using namespace std;

using ll = long long;

void solve() {
int n;
cin >> n;
int res = 0;
for (ll pw = 1; pw <= n; pw = pw * 10 + 1) {
for (int d = 1; d <= 9; d++) {
if (pw * d <= n) {
res++;
}
}
}
cout << res << endl;
}

int main() {
int tests;
cin >> tests;
while (tests-- > 0) {
solve();
}
return 0;
}
Loading