Skip to main content

Posts

codeforces div3 round 547-Superhero Battle

#include < bits / stdc ++. h > using namespace std ; int main () { long long H ; int n , i ; cin >> H >> n ; long long d [ n ], sum = 0 , h = H , gap = 0 ; for ( i = 0 ; i < n ; i ++) { cin >> d [ i ]; sum -= d [ i ]; h = h + d [ i ]; if ( h <= 0 ) { cout << i + 1 << endl ; return 0 ; } gap = max ( gap , sum ); } if ( sum <= 0 ) { cout << "-1" << endl ; return 0 ; } long long whole =( H - gap )/ sum ; H = H - whole * sum ; long long result = whole * n ; for ( i = 0 ;; i ++) { H = H + d [ i % n ]; result ++; if ( H <= 0 ) { cout << result << endl ; break ; } } return 0 ; }

codeforces-beautiful year

#include < bits / stdc ++. h > using namespace std ; bool check ( int n ) { int count [ 10 ]={ 0 }; while ( n != 0 ) { int t = n % 10 ; count [ t ]++; n = n / 10 ; } int flag = 1 ; for ( int i = 0 ; i < 10 ; i ++) { if ( count [ i ]> 1 ) { return false ; flag = 0 ; break ; } } if ( flag == 1 ) return true ; } int main () { int y ; cin >> y ; int i ; if ( check ( y )) y ++; for ( i = y ;; i ++) { if ( check ( i )) { cout << i ; break ; } } return 0 ; }

codeforces-expression

#include < bits / stdc ++. h > using namespace std ; void check ( int a , int b , int c ) { int p , q , r , s , t , u ; p = a * b + c ; q = a + b * c ; r =( a + b )* c ; s = a * b * c ; t = a *( b + c ); u = a + b + c ; cout << max ( p , max ( q , max ( r , max ( s , max ( t , u ))))); } int main () { int a , b , c ; cin >> a >> b >> c ; check ( a , b , c ); return 0 ; }

codeforces-presents

#include < bits / stdc ++. h > using namespace std ; int main () { int n , i ; cin >> n ; vector < pair < int , int >> vect ; for ( i = 0 ; i < n ; i ++) { int x ; cin >> x ; vect . push_back ( make_pair ( x , i + 1 )); } sort ( vect . begin (), vect . end ()); for ( i = 0 ; i < n ; i ++) { cout << vect [ i ]. second << " " ; } return 0 ; }

codeforces-puzzles solution

#include <iostream> #include <vector> #include <algorithm> #include <map> using namespace std ; int main () { int n , m ; cin >> n >> m ; vector <int> vect ; map < int , int > mp ; for ( int i = 0 ; i < m ; i ++) { int x ; cin >> x ; mp [ x ]++; vect . push_back ( x ); } sort ( vect . begin (), vect . end ()); int small = vect [ n - 1 ]- vect [ 0 ]; for ( int i = 0 ; i < vect . size ()- n + 1 ; i ++) { int d = vect [ i + n - 1 ]- vect [ i ]; if ( small > d ) small = d ; } int flag = 1 ; for ( auto k : mp ) { if ( k . second >= n ) { cout << "0" ; flag = 0 ; break ; } } if ( flag == 1 ) { cout << small ; } return 0 ; }

codeforces-Word

#include <iostream> using namespace std ; void process1 ( char str []) { int i = 0 ; while ( str [ i ]!= '\0' ) { if ( str [ i ]>= 65 and str [ i ]<= 90 ) { str [ i ]= str [ i ]+ 32 ; } i ++; } } void process2 ( char str []) { int i = 0 ; while ( str [ i ]!= '\0' ) { if ( str [ i ]>= 97 and str [ i ]<= 122 ) { str [ i ]= str [ i ]- 32 ; } i ++; } } void check ( char str []) { int i = 0 , count = 0 , count1 = 0 ; while ( str [ i ]!= '\0' ) { if ( str [ i ]>= 65 and str [ i ]<= 90 ) { count ++; } else if ( str [ i ]>= 97 and str [ i ]<= 122 ) { count1 ++; } i ++; } if ( count1 == count ) process1 ( str ); else if ( count1 < count ) process2 ( str ); else if ( count1 > count ) process1 ( str ); } int main () { char str [ 101 ]; cin >> str ; check ( str ); cout << str ; ...

codeforces-infinite sequence

#include <iostream> using namespace std ; int main () { long long a , b , c ; cin >> a >> b >> c ; if ( c == 0 ) { if ( a == b ) cout << "YES" ; else cout << "NO" ; } else { if (( b - a )% c == 0 and ( b - a )/ c >= 0 ) { cout << "YES" ; } else { cout << "NO" ; } } return 0 ; }

codeforces-calculating function

#include <iostream> #include <cmath> using namespace std ; long long fun ( long long n ) { if ( n % 2 == 0 ) return n / 2 ; else return - n + fun ( n - 1 ); } int main () { long long n , sum = 0 ; cin >> n ; cout << fun ( n ); return 0 ; }

codeforces-insomnia cure

#include <iostream> using namespace std ; int main () { int k , l , m , n ; long d , count = 0 ; cin >> k >> l >> m >> n >> d ; for ( long i = 1 ; i <= d ; i ++) { if ( i % k != 0 and i % l != 0 and i % m != 0 and i % n != 0 ) count ++; } cout << d - count ; return 0 ; }

Codeforces-Dubstep solution

#include < bits / stdc ++. h > using namespace std ; int main () { string str ; getline ( cin , str ); int index = str . find ( "WUB" ); int count = 0 ; while ( index !=- 1 ) { str . replace ( index , 3 , " " ); //str.erase(str.begin()+index,str.begin()+index+3); index = str . find ( "WUB" ); } cout << str ; return 0 ; }

codeforces-Anton and Danik

#include < bits / stdc ++. h > using namespace std ; int main () { long n ; string str ; cin >> n ; cin >> str ; long count = 0 , index ; index = str . find ( "A" ); while ( index !=- 1 ) { count ++; str . erase ( str . begin ()+ index , str . begin ()+ index + 1 ); index = str . find ( "A" ); } if ( count > n - count ) cout << "Anton" ; else if ( count < n - count ) cout << "Danik" ; else cout << "Friendship" ; return 0 ; }

codeforces-divide it solution

#include < bits / stdc ++. h > using namespace std ; void fun ( long long n , int count ) { if ( n == 1 ) cout << count << endl ; else if ( n % 2 != 0 and n % 3 != 0 and n % 5 != 0 ) cout << "-1" << endl ; if ( n % 5 == 0 ) { n =( 4 * n )/ 5 ; count ++; fun ( n , count ); } else if ( n % 3 == 0 ) { n =( 2 * n )/ 3 ; count ++; fun ( n , count ); } else if ( n % 2 == 0 ) { n = n / 2 ; count ++; fun ( n , count ); } } int main () { int q ; cin >> q ; for ( int i = 1 ; i <= q ; i ++) { long long x ; cin >> x ; fun ( x , 0 ); } return 0 ; }

codeforces-merge it solution

#include < bits / stdc ++. h > using namespace std ; int main () { int t ; cin >> t ; for ( int i = 1 ; i <= t ; i ++) { int count [ 3 ]={ 0 }; int n ; cin >> n ; for ( int j = 0 ; j < n ; j ++) { long x ; cin >> x ; count [ x % 3 ]++; } int result = count [ 0 ]; int t = min ( count [ 1 ], count [ 2 ]); result += t ; count [ 1 ]-= t ; count [ 2 ]-= t ; result +=( count [ 1 ]+ count [ 2 ])/ 3 ; cout << result << endl ; } return 0 ; }

Codeforces round 565 div 3-lose it

#include<iostream> #include<map> using namespace std; int main() {     int arr[6]={0};     map<int,int> m;     m[4]=0;     m[8]=1;     m[15]=2;     m[16]=3;     m[23]=4;     m[42]=5;     int n;     cin>>n;     int a[n];     for(int i=0;i<n;i++)     {         cin>>a[i];         if(a[i]==4)             arr[0]++;         else if(arr[m[a[i]]-1]>0)         {             arr[m[a[i]]-1]--;             arr[m[a[i]]]++;         }     }     cout<<n-6*arr[...

Counting the number of strongly connected sub graph

#include<bits/stdc++.h> using namespace std; class graph {     int v;     list<int> *adj; public:     graph(int v)     {         this->v=v;         adj=new list<int>[v];     }     void add_edge(int v,int w)     {         adj[v].push_back(w);         adj[w].push_back(v);     }     void dfs(int start,bool visited[]);     void counted(int start); }; void graph:: dfs(int start,bool visited[]) {     visited[start]=true;     for(auto it=adj[start].begin();it!=adj[start].end();it++)     {         if(!visited[*it])         {             dfs(*it,visited);  ...

DFS using a stack

#include<bits/stdc++.h> using namespace std; class graph {     int v;     list<int> *adj; public:     graph(int v)     {         this->v=v;         adj=new list<int>[v];     }     void add_edge(int v,int w)     {         adj[v].push_back(w);     }     void dfs(int start); }; void graph:: dfs(int start) {     bool *visited=new bool[v];     for(int i=0;i<v;i++)         visited[i]=false;     visited[start]=true;     stack<int> s;     s.push(start);     while(!s.empty())     {         int k=s.top();         s.pop();       ...

DFS of a graph

#include<bits/stdc++.h> using namespace std; class graph {     int v;     list<int> *adj; public:     graph(int v)     {         this->v=v;         adj=new list<int>[v];     }     void add_edge(int v,int w)     {         adj[v].push_back(w);     }     void dfs(int v);     void dfsSearch(int v,bool visited[]); }; void graph:: dfsSearch(int start,bool visited[]) {     visited[start]=true;     cout<<start<<" ";     for(auto itr=adj[start].begin();itr!=adj[start].end();itr++)     {         if (!visited[*itr])         {             dfsSearch(*itr,visited); ...

BFS of a graph

#include<bits/stdc++.h> using namespace std; class graph {     int v;     list<int> *adj; public:     graph(int v)     {         this->v=v;         adj=new list<int>[v];     }     void add_edge(int v,int w)     {         adj[v].push_back(w);     }     void bfs(int start); }; void graph:: bfs(int start) {     bool *visited=new bool[v];     for(int i=0;i<v;i++)         visited[i]=false;     visited[start]=true;     list<int> queue;     queue.push_back(start);     while(!queue.empty())     {         int s=queue.front();         cout<<s<...

Weighted graph using stl

#include<bits/stdc++.h> using namespace std; void add_edge(vector<pair<int,int>> adj[],int u,int v,int wt) {     adj[u].push_back(make_pair(v,wt));     adj[v].push_back(make_pair(u,wt)); } void print_graph(vector<pair<int,int>> adj[],int V) {     int v,w;     for(int i=0;i<V;i++)     {         cout<<"Node "<<i<<" makes an edge with"<<endl;         for(auto it=adj[i].begin();it!=adj[i].end();it++)         {             v=it->first;             w=it->second;             cout<<"node "<<v<<"with weight "<<w<<endl;         }         cout<<e...