Skip to content
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
45 changes: 45 additions & 0 deletions Codeforces/1345B.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include<bits/stdc++.h>
#include<math.h>
using namespace std;
const long long MAX = 1e9;
vector<long long> pyr; //cards in pyramid

int binarySearch(int n)
{
int l = 0,r = pyr.size() - 1;
int ind = 0;
while(l <= r){
int mid = l + (r - l)/2;
if(pyr[mid] == n) return mid;
if(pyr[mid] < n){
l = mid + 1;
ind = mid;
}
else{
r = mid - 1;
}
}
return ind;
}

int main(){
long long sum =0;
for(int i =1;sum<=MAX;i++){
pyr.push_back(sum);
sum += 3*i-1;
}
int t;
cin>>t;
while(t--){
int n;
cin>>n;
int ans =0;
int i = binarySearch(n);
while(i>0&&n>0){
n -= pyr[i];
ans ++;
i = binarySearch(n);
}
cout<<ans<<endl;
}
}
27 changes: 27 additions & 0 deletions Codeforces/1346E.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include<bits/stdc++.h>
#include<math.h>
using namespace std;

int main(){
int n,m,k;
cin>>n>>m>>k;
int ans[n];
for (int i = 0; i < n; ++i)
{
ans[i] = 2*10e6;
}
ans[k-1]=0;
while(m--){
int x;
int y;
cin>>x>>y;
int ix = ans[x];
int iy = ans[y];
ans[x] = min(ix+1, iy);
ans[y] = min(iy+1, ix);
}
for(auto x:ans){
cout<<((x==2*10e6)?-1:x)<<" ";
}
cout<<endl;
}
29 changes: 29 additions & 0 deletions Codeforces/1372B.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include<bits/stdc++.h>
using namespace std;

int main(){
int t;
cin>>t;
while(t--){
int n;
cin>>n;
if(n%2==0){
cout<<n/2<<" "<<n/2<<endl;
}
else{
int a=0;
for(int i=2;i<=sqrt(n);i++){
if(n%i==0){
a = n/i;
break;
}
}
if(a==0){
cout<<1<<" "<<n-1<<endl;
}
else{
cout<<a<<" "<<n-a<<endl;
}
}
}
}
33 changes: 33 additions & 0 deletions Codeforces/1380C.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include<bits/stdc++.h>
#include<math.h>
using namespace std;

int main(){
int t;
cin>>t;
while(t--){
int n,x;
cin>>n>>x;
std::vector<int> a(n);
for (int i = 0; i < n; ++i)
{
cin>>a[i];
}
sort(a.begin(),a.end(),greater<int>());
int c =1;
int ans =0;
for (int i = 0; i < n; ++i)
{
if(a[i]*c>=x){
ans += 1;
c =0;
}
c++;

}
cout<<ans<<endl;


}
return 0;
}
26 changes: 26 additions & 0 deletions Codeforces/1399A.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include<bits/stdc++.h>
using namespace std;

int main(){
int t;
cin>>t;
while(t--){
int n;
cin>>n;
int a[n];
for(int i =0;i<n;i++){
cin>>a[i];
}
sort(a,a+n);
bool ans = true;
for(int i=1;i<n-1;i++){
if(abs(a[i] - a[i+1])>1){
ans = false;
break;
}
}
if(ans) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}
37 changes: 37 additions & 0 deletions Codeforces/1418B.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include<bits/stdc++.h>
using namespace std;

int main(){
int t;
cin>>t;
while(t--){
int n;
cin>>n;
int a[n], l[n];
vector<int> d;
for(auto &x:a){
cin>>x;
}
for(int i=0;i<n;i++){
cin>>l[i];
if(l[i]==0){
d.push_back(a[i]);
}
}
vector<int>::iterator j;
sort(d.begin(),d.end());
reverse(d.begin(), d.end());
j = d.begin();
for(int i =0;i<n;i++){
if(l[i]==0){
a[i]= *j;
j++;
}
}
for(int &x:a){
cout<<x<<" ";
}
cout<<endl;
}
return 0;
}
19 changes: 19 additions & 0 deletions Codeforces/1418C_DP.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include<bits/stdc++.h>
using namespace std;

int solve(int n, int a[])

int main(){
int t;
cin>>t;
while(t--){
int n;
cin<<n;
int a[n];
for(auto &x:a){
cin>>x;
}
c = solve(n,a);
cout<<c<<endl;
}
}
36 changes: 36 additions & 0 deletions Codeforces/1419D1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include<bits/stdc++.h>
using namespace std;

int main(){
int n;
cin>>n;
int a[n];
for(auto &x:a){
cin>>x;
}
sort(a, a+n);
int ans =0;
if(n>2){
if(n%2==0){
for(int i =0;i+2<n;i+=2){
int temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
ans++;
}
}
else{
for(int i =0;i+1<n;i+=2){
int temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
ans++;
}
}
}
cout<<ans<<endl;
for(auto x: a){
cout<<x<<" ";
}
return 0;
}
48 changes: 48 additions & 0 deletions Codeforces/1419D2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include<bits/stdc++.h>
using namespace std;

int main(){
int n;
cin>>n;
int a[n];
for(auto &x:a){
cin>>x;
}
sort(a, a+n);
int ans =0;
if(n>2){
if(n%2==0){
for(int i =0;i+2<n;i+=2){
int j;
for(j =i+1;j<n-1;j++){
if(a[j]!=a[i]){
break;
}
}
int temp = a[i];
a[i] = a[j];
a[j] = temp;
ans++;
}
}
else{
for(int i =0;i+1<n;i+=2){
int j;
for(j =i+1;j<n-1;j++){
if(a[j]!=a[i]){
break;
}
}
int temp = a[i];
a[i] = a[j];
a[j] = temp;
ans++;
}
}
}
cout<<ans<<endl;
for(auto x: a){
cout<<x<<" ";
}
return 0;
}
21 changes: 21 additions & 0 deletions Codeforces/189A(dp).cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include<bits/stdc++.h>
using namespace std;
int dp[4001] ={INT_MIN};
int a,b,c;

int main(){
int n, a[3];
cin>>n>>a[0]>>a[1]>>a[2];
sort(a,a+3);
dp[0] =0;
for(int i=0;i<n+1;i++){
for(int j=0;j<3;j++){
if(i - a[j]>=0){
dp[i] = max(dp[i],dp[i-a[j]]);
}
}
dp[i] += 1;
}
cout<<dp[n]<<endl;
return 0;
}
Empty file added Codeforces/1932C(IMP).cpp
Empty file.
19 changes: 19 additions & 0 deletions Codeforces/455A(dp).cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include<bits/stdc++.h>
using namespace std;
#define MAXN 100005
long long occurance[MAXN] ={0};
long long ans[MAXN] ={0};
int main(){
int n,x;
cin>>n;
while(n--){
cin>>x;
occurance[x]++;
}
ans[1]=occurance[1];
for(int i=2;i<MAXN;i++){
ans[i] = max(ans[i-1],i*occurance[i]+ans[i-2]);
}
cout<<ans[MAXN-1]<<endl;
return 0;
}
30 changes: 30 additions & 0 deletions Codeforces/489B.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,m;
cin>>n;
int b[n];
for(auto &x : b){
cin>>x;
}
cin>>m;
int g[m];
for(auto &x : g){
cin>>x;
}
int i=0,j=0,p=0;
sort(b, b+n);
sort(g,g+m);
while(j<m){
for(i=0;i<n;i++){
if(abs(b[i]-g[j])<=1){
b[i] =102;
p++;
break;
}
}
j++;
}
cout<<p<<endl;
return 0;
}
10 changes: 10 additions & 0 deletions Codeforces/489C.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include<bits/stdc++.h>
using namespace std;

int main(){
int m,s;
cin>>m>>s;


return 0;
}
Loading