Posts

Showing posts from 2019

Github for stl

1.  https://github.com/gibsjose/cpp-cheat-sheet/blob/master/Data%20Structures%20and%20Algorithms.md 2.  https://github.com/dimkatsi91/Cpp-STL-Examples 3.  https://github.com/yiranyyu/STL-note/tree/master/STL_note 4.  https://github.com/Bhupesh-V/30-seconds-of-cpp 5.  https://gist.github.com/alyssaq/6743129

File use in competitive programming

https://www.geeksforgeeks.org/inputoutput-external-file-cc-java-python-competitive-programming/

Fast i/o for programming

https://www.geeksforgeeks.org/fast-io-for-competitive-programming/

Count the number of possible triangles

1. geeksforgeeks.org/find-number-of-triangles-possible/

UVA 353

#include<bits/stdc++.h> using namespace std; int main() {     //freopen("input.txt","r",stdin);     //freopen("output.txt","w",stdout);     string a,b;     int flag;     while(getline(cin,a))     {         set<string>s1;         for(int i=0; i<a.size(); i++)         {             for(int j=i; j<a.size(); j++)             {                 vector<char>v;                 char c[1000];                 for(int k=i; k<=j; k++)                 {                     v.push_back(a[k]);                 }       ...

UVA 401

#include<bits/stdc++.h> using namespace std; int main() {     //freopen("input.txt","r",stdin);     //freopen("output.txt","w",stdout);     string a;     int flag,flag1;     while(getline(cin,a))     {         vector<char>b;         for(int i=0; i<a.size()/2; i++)         {             if(a[i]==a[a.size()-i-1])             {                 flag=0;             }             else             {                 flag=1;                 break;             }         }         int flag3=0;    ...

UVA 11586

#include<bits/stdc++.h> using namespace std; int main() {     //freopen("input.txt","r",stdin);     //freopen("output.txt","w",stdout);     int a,flag,sum=0,sum1=0,sum2=0;     string b;     cin>>a;     getchar();     for(int i=0; i<a; i++)     {         getline(cin,b);         //cout<<b<<endl;         for(int i=0; i<b.size()-1; i++)         {             //cout<<b[i]<<" "<<b[i+1]<<endl;             if(b[i]=='F' && b[i+1]=='M')             {                 sum++;             }             else if(b[i]=='M' && b[i+1]=='F')         ...

UVA 11683

#include<bits/stdc++.h> using namespace std; int main() {     int a,b,c;     while(cin>>a)     {         if(a==0)         {             break;         }         cin>>b;         vector<int>v;         for(int i=0; i<b; i++)         {             cin>>c;             v.push_back(abs(a-c));         }         int m=v[0],sum=0;         sum=sum+v[0];         for(int i=1; i<b; i++)         {             if(v[i]>m && v[i]>v[i-1])             {                 sum=sum+(v[i...

UVA 11661

#include<bits/stdc++.h> using namespace std; int main() {     int a,b,sum,sum1;     string c;     while(cin>>a)     {         if(a==0)         {             break;         }         getchar();         getline(cin,c);         vector<int>v;         sum=10e9;         sum1=10e9;         for(int i=0; i<a; i++)         {             if(c[i]=='Z')             {                 v.push_back(0);                 break;             }             else if(c[i]=='R')        ...

UVA 10919

#include<bits/stdc++.h> using namespace std; int main() {     //freopen("input.txt","r",stdin);     //freopen("output.txt","w",stdout);     int a,b,c,d,e,e1,sum=0,flag=0;     while(cin>>a)     {         if(a==0)         {             break;         }         cin>>b;         vector<int>v;         for(int i=0; i<a; i++)         {             cin>>c;             v.push_back(c);         }         sort(v.begin(),v.end());         for(int i=0; i<b; i++)         {             cin>>c>>d;             for(int j=...

UVA 10424

#include<bits/stdc++.h> using namespace std; int main() {     //freopen("input.txt","r",stdin);     //freopen("output.txt","w",stdout);     string a,b;     char c='a';     int sum=0,sum1=0,sum12=0,sum2=0,sum22=0;     vector<int>v;     vector<char>v1;     for(int i=0; i<26; i++)     {         sum=sum+1;         v.push_back(sum);     }     for(int i=0; i<26; i++)     {         v1.push_back(c);         c++;     }     while(getline(cin,a))     {         getline(cin,b);         for(int i=0; i<26; i++)         {             for(int j=0; j<a.size(); j++)             {       ...

UVA 10324

#include<bits/stdc++.h> using namespace std; int main() {     //freopen("input.txt","r",stdin);     //freopen("output.txt","w",stdout);     long long int a,b,c,flag,sum=0,e,sum1=0,e1,e2,e3;     string d;     while(cin>>d)     {         sum1++;         vector<long long int>v;         v.push_back(0);         for(int i=0; i<d.size(); i++)         {             sum=sum+(d[i]-'0');             v.push_back(sum);         }         cin>>a;         flag=1;         for(int i=0; i<a; i++)         {             cin>>b>>c;             if(flag==1)     ...

UVA 10141

This easy problem blow out my mind because of getchar() and not understanding conditions of strings. #include<bits/stdc++.h> using namespace std; int main() { //freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); int a,b,h,g,sum1=1,best; while(cin>>a>>b) { string c,d,f,name; double e,price; //sum1++; best=0; price=1e9; // string name; if(a+b==0) { break; } getchar(); for(int i=0; i<a; i++) { getline(cin,c); } for(int i=0; i<b; i++) { //getchar(); getline(cin,d); cin>>e>>g; getchar(); if(best<g) { best=g; name=d; price=e; } else if(best==g && price>e) { ...

FILE input output

https://www.geeksforgeeks.org/inputoutput-external-file-cc-java-python-competitive-programming/

UVA 661

*I thought it will be ampere rather than amperes for 1.It costs many WA. #include<bits/stdc++.h> using namespace std; int main() {     long long int a,b,c,d,e,f,sum=0,g,h=0,x=0,flag;     while(cin>>a>>b>>c)     {         x++;         if(a+b+c==0)         {             break;         }         vector<long long int>v1,v2,v3;         for(int i=0; i<a; i++)         {             cin>>d;             v1.push_back(d);         }         //sort(v1.begin(),v1.end());         for(int i=0; i<b; i++)         {             cin>>d;           ...

UVA 573

#include<bits/stdc++.h> using namespace std; int main() {     double a,b,c,d,e=0,f=0,sum=0;     while(cin>>a>>b>>c>>d)     {         if(a+b+c+d==0)         {             break;         }         e=d/100.0;         e=e*b;         while(f>0 || f<=a)         {             if(sum>=1)             {                 b=b-e;             }             if(b<0)             {                 b=0;             }             f=f+b;       ...

UVA 119

#include<bits/stdc++.h> using namespace std; int main() {     int a,c[100],f,e,x1=0;     char b[100][100],d[100],g[100][100];     while(cin>>a)     {         x1++;         if(x1>=2)         {             cout<<endl;         }         for(int i=0; i<a; i++)         {             cin>>b[i];             c[i]=0;         }         for(int i=0; i<a; i++)         {             cin>>d>>e>>f;             if(f!=0)             {                 for(int j=0; j<f; j++)      ...

UVA 12554

/* ID: Nipun Paul LANG: C++ PROB: God knows */ #include<bits/stdc++.h> using namespace std; typedef long long ll; int main() {     /*ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);*/     int a,sum=0,flag=0,l=0;     string b[105],c[16];     cin>>a;     c[0]="Happy";     c[1]="birthday";     c[2]="to";     c[3]="you";     c[4]="Rujia";     for(int i=0; i<a; i++)     {         cin>>b[i];     }     while(true)     {         for(int i=0; i<a; i++)         {             cout<<b[i]<<": ";             sum++;             //cout<<sum<<endl;             if(sum==12 || (sum...

UVA 12503

/* ID: Nipun Paul LANG: C++ PROB: God knows */ #include<bits/stdc++.h> using namespace std; typedef long long ll; int main() {     /*ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);*/     int a,c,d[101],e,f,g=0,l=0,sum=0,h;     string b;     cin>>a;     for(int i=0; i<a; i++)     {         cin>>h;         getchar();         for(int k=0; k<h; k++)         {             getline(cin,b);             //cout<<b<<endl;             e=b.size();             if(e==4 || e==5)             {                 if(b=="LEFT")                 { ...

UVA 12015

/* ID: Nipun Paul LANG: C++ PROB: God knows */ #include<bits/stdc++.h> using namespace std; typedef long long ll; int main() {     /*ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);*/     int a,b,c=0,d[105];     cin>>a;     pair<int,string>p[105];     for(int i=0; i<a; i++)     {         for(int j=0; j<10; j++)         {             cin>>p[j].second>>p[j].first;             d[j]=p[j].first;         }         sort(d,d+10);         b=d[9];         cout<<"Case #"<<i+1<<":"<<endl;         for(int j=0; j<10; j++)         {             if(b==p[j].first)    ...

UVA 11679

/* ID: Nipun Paul LANG: C++ PROB: God knows */ #include<bits/stdc++.h> using namespace std; typedef long long ll; int main() {     /*ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);*/     ll a,b,c,d,e;     while(cin>>a>>b)     {         if(a+b==0)         {             break;         }         //vector<ll>v;         ll v[a];         memset(v,0,sizeof(v));         for(int i=0; i<a; i++)         {             cin>>v[i];         }         for(int i=0; i<b; i++)         {             cin>>c>>d>>e;           ...

UVA 10963

/* ID: Nipun Paul LANG: C++ PROB: God knows */ #include<bits/stdc++.h> using namespace std; typedef long long ll; int main() {     /*ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);*/     ll a,b,c,d;     cin>>a;     for(int i=0; i<a; i++)     {         cin>>b;         vector<ll>v;         for(int j=0; j<b; j++)         {             cin>>c>>d;             v.push_back(abs(c-d));         }         sort(v.begin(),v.end());         if(v[0]==v[v.size()-1])         {             cout<<"yes"<<endl;         }         else       ...

UVA 10114

#include<bits/stdc++.h> using namespace std; int main() {     double a,b,c,d,e,f,g[101],pay,carown;     int sum=0;     while(cin>>a>>b>>c>>d)     {         if(a<0)         {             break;         }         while(d--)         {             cin>>e>>f;             for(int i=e; i<101; i++)             {                 g[i]=f;             }         }         pay=c/a;         carown=(b+c)*(1-g[0]);         while(carown<c)         {             sum++;   ...

UVA 621

/* ID: Nipun Paul LANG: C++ PROB: God knows */ #include<bits/stdc++.h> using namespace std; typedef long long ll; int main() {     /*ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);*/     ll a;     cin>>a;     for(int i=0; i<a; i++)     {         string b;         cin>>b;         if(b=="1" || b=="4" || b=="78")         {             cout<<"+"<<endl;         }         else if(b[b.size()-1]=='5' && b[b.size()-2]=='3')         {             cout<<"-"<<endl;         }         else if(b[0]=='9' && b[b.size()-1]=='4')         {            ...

UVA 11498

/* ID: Nipun Paul LANG: C++ PROB: God knows */ #include<bits/stdc++.h> using namespace std; typedef long long ll; int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ll a,b,c,d,e;     while(cin>>a)     {         if(a==0)         {             break;         }         cin>>b>>c;         for(int i=0; i<a; i++)         {             cin>>d>>e;             if(d==b || e==c)             {                 cout<<"divisa"<<endl;             }             else if(d>b && e>c)   ...

UVA 10550

/* ID: Nipun Paul LANG: C++ PROB: God knows */ #include<bits/stdc++.h> using namespace std; typedef long long ll; int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ll a,b,c,d;     while(cin>>a>>b>>c>>d)     {         if(a+b+c+d==0)         {             break;         }         ll sum=0,b1=0,c1=0,d1=0;         if(a>=b)         {             a=40+(b+1)-(a+1);             b1=0;         }         else         {             b1=b;         }         if(b<=c)         {     ...

UVA 272

/* ID: Nipun Paul LANG: C++ PROB: God knows */ #include<bits/stdc++.h> using namespace std; typedef long long ll; int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int flag=0;     string a;     while(getline(cin,a))     {         for(int i=0; i<a.size(); i++)         {             if(a[i]=='"' && flag==0)             {                 cout<<"``";                 flag=1;             }             else if(a[i]=='"' && flag==1)             {                 cout<<"''";               ...

Algorithm on how to find the day of a week

https://www.hackerearth.com/blog/developers/how-to-find-the-day-of-a-week/

Prefix sum or for fixed range summation

https://codeforces.com/problemset/problem/433/B https://codeforces.com/problemset/problem/313/B DP catagory solution where accumulate sum not possible in this kind of problem because of test cases and time limit.So if we do sum and insert it in an array previously,just use a vector and subtract from upper trange to lower range we'll get the answer.

Codeforces 295(Div 2) B

https://codeforces.com/problemset/problem/520/B Editorial from blog- Its quite simple . First understand if m<n then ans is n-m  else  make m less than or equal to n by dividing it by 2 , increase the count by one and if at any moment it becomes odd increase the count by 1 and increase m by 1 . e.g  4 17  1. 17->18  2. 18->9  3. 9->10  4. 10->5  5. 5->6  6. 6->3  then 3 is smaller than 4 so ans is 6+(4-3) = 7  * Fully reverse of what the question said to do and have a alternative main solution using  breadth-first search.

Big sum(nsu question)

Image
#include<bits/stdc++.h> using namespace std; typedef long long ll; int main() {     int c,d,e,l,x,y;     string a,b;     char f[1000],g[1000];     cin>>a>>b;     c=a.size();     d=b.size();     if(c>d)     {         e=c-d;         for(int i=0; i<e; i++)         {             f[i]='0';         }         l=0;         for(int i=e; i<e+d; i++)         {             f[i]=b[l];             l++;         }         l=0;         int temp=0;         int temp1=0;         for(int i=c-1; i>=0; i--)         {   ...

UVA 10432 & About Polygon and sin() function

Polygon-  https://www.mathopenref.com/polygonregulararea.html Sin()- It work's with radian rather than degree,so we first have to convert the given degree to radian by multiplying (PI/180). Then calling the function. /* ID: Nipun Paul LANG: C++ PROB: God knows */ #include<bits/stdc++.h> using namespace std; int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     double a,b,c,d;     while(cin>>a>>b)     {         d=(360/b*1.0)*(M_PI/180);         c=((a*a*b)*(sin(d)))/2;         printf("%.3f\n",c);     } }

Spoj-PRIME GENERATOR (Using segmented sieve print billion range primes)

https://www.spoj.com/problems/PRIME1/ https://www.geeksforgeeks.org/segmented-sieve-print-primes-in-a-range/ #include <bits/stdc++.h> using namespace std; // This functions finds all primes smaller than limit // using simple sieve of eratosthenes.  It stores found // primes in vector prime[] void simpleSieve(int limit, vector<int>& prime) {     bool mark[limit + 1];     memset(mark, false, sizeof(mark));     for (int i = 2; i <= limit; ++i) {         if (mark[i] == false) {             // If not marked yet, then its a prime             prime.push_back(i);             for (int j = i; j <= limit; j += i)                 mark[j] = true;         }     } } // Finds all prime numbers in given range using // segmented sieve void primes...

C++ output manipulators like endl,setw

https://www.includehelp.com/cpp-tutorial/cpp-manipulators-endl-setw-setprecision-setf-cpp-programming-tutorial.aspx

UVA 382

/* ID: Nipun Paul LANG: C++ PROB: God knows */ #include<bits/stdc++.h> using namespace std; typedef long long ll; int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int a,b,c,sum=0;     int v[100000];     int flag=1;     while(cin>>a)     {         if(flag==1)         {             cout<<"PERFECTION OUTPUT"<<endl;             flag=0;         }         if(a==0)         {             break;         }         int l=0;         v[l]=a;         l++;         for(int i=0; i<l; i++)         {         ...

UVA 10469

/* ID: Nipun Paul LANG: C++ PROB: God knows */ #include<bits/stdc++.h> using namespace std; typedef long long ll; int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ll a,b,c,d,l=0,m=0,sum=0;     while(cin>>a>>b)     {         vector<int>v,v1,v3;         c=a;         d=b;         while(c!=0)         {             v.push_back(c%2);             c=c/2;             l++;         }         while(d!=0)         {             v1.push_back(d%2);             d=d/2;             m++;         }   ...

UVA 494

/* ID: Nipun Paul LANG: C++ PROB: God knows */ #include<bits/stdc++.h> using namespace std; typedef long long ll; int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int sum=0,flag;     string a;     while(getline(cin,a)){     //cout<<a<<endl;     for(int i=0; i<a.size(); i++)     {         if((a[i]>='a' && a[i]<='z') || (a[i]>='A' && a[i]<='Z'))         {             flag=0;         }         else         {             if(flag==0)             {                 sum++;             }             flag=1; ...

UVA 10929 (multiple of 11)

/* ID: Nipun Paul LANG: C++ PROB: God knows */ #include<bits/stdc++.h> using namespace std; typedef long long ll; vector<int> v; int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int sum=0,b;     char a[1005];     while(cin>>a)     {         if(strlen(a)==1 && a[0]=='0')         {             break;         }         for(int i=0; i<strlen(a); i++)         {             b=a[i]-'0';             if((i+1)%2==0)             {                 b=(-1)*b;             }             sum=sum+b;         } ...

Fixed range sum without loop :D

1.  https://www.geeksforgeeks.org/numeric-header-in-c-stl-set-1-accumulate-and-partial_sum/ 2.  https://codeforces.com/contest/1197/problem/C Time complexity-O(n*k)

Binary Search

Using Upper_bound 1.  https://codeforces.com/problemset/problem/474/B 2.  https://codeforces.com/problemset/problem/706/B Contest Link 1.  https://vjudge.net/contest/311179#status/Nipun_Paul/-/1/

isalpha() whether a character is an alphabet (a to z and A-Z) or not

https://www.programiz.com/c-programming/library-function/ctype.h/isalpha Solution example -  https://vjudge.net/contest/307320#status/Nipun_Paul/I/0/

Pattern ;)

/* ─────────▄──────────────▄─────────wow───────────────────────────────────── ────────▌▒█───────────▄▀▒▌──────────────────────────────────────────────── ────────▌▒▒█────────▄▀▒▒▒▐──────────────────────────────────────────────── ───────▐▄▀▒▒▀▀▀▀▄▄▄▀▒▒▒▒▒▐────────────▄▄▄▄─▄▄▄▄────▄▄▄▄─▄▄▄▄─▄▄▄▄─▄─────── ─────▄▄▀▒░▒▒▒▒▒▒▒▒▒█▒▒▄█▒▐────────────█▄▄▄─█──█────█────█──█─█──█─█─────── ───▄▀▒▒▒░░░▒▒▒░░░▒▒▒▀██▀▒▌────────────▄▄▄█─█▄▄█────█▄▄▄─█▄▄█─█▄▄█─█▄▄▄──── ──▐▒▒▒▄▄▒▒▒▒░░░▒▒▒▒▒▒▒▀▄▒▒▌─────────────────────────────────────────────── ──▌░░▌█▀▒▒▒▒▒▄▀█▄▒▒▒▒▒▒▒█▒▐─────────────────────────────────────────────── ─▐░░░▒▒▒▒▒▒▒▒▌██▀▒▒░░░▒▒▒▀▄▌─────────────────so─ascii───────────────────── ─▌░▒▄██▄▒▒▒▒▒▒▒▒▒░░░░░░▒▒▒▒▌────────────────────────────────────────────── ▀▒▀▐▄█▄█▌▄░▀▒▒░░░░░░░░░░▒▒▒▐────much─codeforces─────────────────────────── ▐▒▒▐▀▐▀▒░▄▄▒▄▒▒▒▒▒▒░▒░▒░▒▒▒▒▌──────────────────────────────────────omg──── ▐▒▒▒▀▀▄▄▒▒▒▄▒▒▒▒▒▒▒▒░▒░▒░▒▒▐───────────▄─────▄─▄▄▄▄─▄─────▄─────────────── ─▌▒▒▒▒▒...

UVA 673(using stack)

/* ID: Nipun Paul LANG: C++ PROB: God knows */ #include<bits/stdc++.h> using namespace std; int main() {     int a,flag;     cin>>a;     getchar();     for(int i=0; i<a; i++)     {     string b;     getline(cin,b);     stack<char> st;     for(int i=0; i<b.size(); i++)     {         if(b[i]=='(' || b[i]=='[' || b[i]==' ')         {             st.push(b[i]);         }         else if(b[i]==')')         {             if(!st.empty() && st.top()=='(')             {                 st.pop();             }             else       ...