Sunday, June 19, 2016

TLAC Kattis Rock-Paper-Scissors Tournament Patience Test

Always read the problem fully , specially if there are something in bold / mathematical form never miss those .
This problem created enough confusion when taking input for k.

Had some confusing thoughts .
For some player , win_average = his_win/(his_win+his_lose)
When , N = 2
We can write ,
win_average = his_win/(his_win+his_lose)
                     = his_wins/total_match

But, this doesn't apply when N>2 .



 
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/****************************************

@_@
Cat Got Bored *_*
#_#
*****************************************/

#include <bits/stdc++.h>


#define loop(i,s,e) for(int i = s;i<=e;i++) //including end point

#define pb(a) push_back(a)

#define sqr(x) ((x)*(x))

#define CIN ios_base::sync_with_stdio(0); cin.tie(0);

#define ll long long

#define ull unsigned long long

#define SZ(a) int(a.size())

#define read() freopen("input.txt", "r", stdin)

#define write() freopen("output.txt", "w", stdout)


#define ms(a,b) memset(a, b, sizeof(a))

#define all(v) v.begin(), v.end()

#define PI acos(-1.0)

#define pf printf

#define sfi(a) scanf("%d",&a);

#define sfii(a,b) scanf("%d %d",&a,&b);

#define sfl(a) scanf("%lld",&a);

#define sfll(a,b) scanf("%lld %lld",&a,&b);

#define sful(a) scanf("%llu",&a);

#define sfulul(a,b) scanf("%llu %llu",&a,&b);

#define sful2(a,b) scanf("%llu %llu",&a,&b); // A little different

#define sfc(a) scanf("%c",&a);

#define sfs(a) scanf("%s",a);


#define mp make_pair

#define paii pair<int, int>

#define padd pair<dd, dd>

#define pall pair<ll, ll>

#define fs first

#define sc second

#define CASE(t) printf("Case #%d: ",++t) // t initialized 0

#define cCASE(t) cout<<"Case "<<++t<<": ";

#define INF 1000000000   //10e9

#define EPS 1e-9

#define flc fflush(stdout); //For interactive programs , flush while using pf (that's why __c )

using namespace std;
//Remove this before submission
int num_wins[10009];
int num_loss[1009];
inline int win_pl(int mv1,int mv2)
{
    if(mv1==mv2)
        return 0;
    if(mv1==1 && mv2==2)
        return 2;
    if(mv1==2 && mv2==3)
        return 2;
    if(mv1==1 && mv2==3)
        return 1;
    if(mv1==2 && mv2==1)
        return 1;
    if(mv1==3 && mv2==2)
        return 1;
    if(mv1==3 && mv2==1)
        return 2;

    return 0;
}
inline int prser(char ss[])
{
    if(strcmp(ss,"rock")==0)
        return 1;
    if(strcmp(ss,"paper")==0)
        return 2;
    if(strcmp(ss,"scissors")==0)
        return 3;
}
int main()
{
    //write();

    int n,k;
    int cas = 0;
    while(true)
    {
        

        

        sfi(n);

        if(n==0)
            break;
            
        if(cas!=0)
            pf("\n");
            
        cas++;

        sfi(k);
        ms(num_wins,0);
        ms(num_loss,0);
        int num_mtc = 0;

        loop(i,1,(k*n*(n-1))/2)
        {
            int plyer1;
            char tke1[20];

            int plyer2;
            char tke2[20];
            //cin>>plyer1>>tke1;
            sfi(plyer1);
            sfs(tke1);
            sfi(plyer2);
            sfs(tke2);
            //cin>>plyer2>>tke2;
            //cout<<plyer1<<" "<<plyer2<<endl;
            //cout<<tke1<<" "<<tke2<<endl;
            //cout<<prser[tke1]<<" "<<prser[tke2]<<endl;

            if(win_pl(prser(tke1),prser(tke2))==1)
            {
                num_wins[plyer1]++;
                num_loss[plyer2]++;
                num_mtc++;
            }
            else if(win_pl(prser(tke1),prser(tke2))==2)
            {
                num_wins[plyer2]++;
                num_loss[plyer1]++;
                num_mtc++;
            }
        }
        //cout<<"La"<<endl;


        loop(i,1,n)
        {

            if(num_wins[i]+num_loss[i]==0)
            {
                pf("-\n");
            }
            else
            {

                double wn = (double)(num_wins[i]*1.000)/( (num_wins[i]+ num_loss[i])*1.00);
                pf("%.3lf\n",wn);
            }
        }

    }

    return 0;
}

TLAC Kattis T9Spelling

Having a bad day , struggling with even simplest problems.

If can't think of anything smarter , code the dumb one if IT WORKS.


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/****************************************

@_@
Cat Got Bored *_*
#_#
*****************************************/

#include <bits/stdc++.h>


#define loop(i,s,e) for(int i = s;i<=e;i++) //including end point

#define pb(a) push_back(a)

#define sqr(x) ((x)*(x))

#define CIN ios_base::sync_with_stdio(0); cin.tie(0);

#define ll long long

#define ull unsigned long long

#define SZ(a) int(a.size())

#define read() freopen("input.txt", "r", stdin)

#define write() freopen("output.txt", "w", stdout)


#define ms(a,b) memset(a, b, sizeof(a))

#define all(v) v.begin(), v.end()

#define PI acos(-1.0)

#define pf printf

#define sfi(a) scanf("%d",&a);

#define sfii(a,b) scanf("%d %d",&a,&b);

#define sfl(a) scanf("%lld",&a);

#define sfll(a,b) scanf("%lld %lld",&a,&b);

#define sful(a) scanf("%llu",&a);

#define sfulul(a,b) scanf("%llu %llu",&a,&b);

#define sful2(a,b) scanf("%llu %llu",&a,&b); // A little different

#define sfc(a) scanf("%c",&a);

#define sfs(a) scanf("%s",a);


#define mp make_pair

#define paii pair<int, int>

#define padd pair<dd, dd>

#define pall pair<ll, ll>

#define fs first

#define sc second

#define CASE(t) printf("Case #%d: ",++t) // t initialized 0

#define cCASE(t) cout<<"Case "<<++t<<": ";

#define INF 1000000000   //10e9

#define EPS 1e-9

#define flc fflush(stdout); //For interactive programs , flush while using pf (that's why __c )

using namespace std;
//Remove this before submission

const int parser[] = {0,2,22,222,3,33,333,4,44,444,5,55
                      ,555,6,66,666,7,77
                      ,777,7777,8,88,888,9,99,999,9999
                     };

int prse(char c)
{
    if(c==' ')
        return 0;
    int cnv = c - 'a' +1;
    return parser[cnv];
}
bool sme_btn(char ch1,char ch2)
{
    //cout<<c1<<"  "<<c2<<endl;
    if(ch1==ch2)
        return true;
    int c1 = ch1 - 'a' +1;
    int c2 = ch2 - 'a' +1;

    //cout<<cn1<<" "<<cn2<<endl;
    if(c1==1 || c1==2 ||c1==3)
        if(c2==1 || c2==2 ||c2==3)
            return true;
    if(c1==4 || c1==5 ||c1==6)
        if(c2==4 || c2==5 ||c2==6)
            return true;

    if(c1==7 || c1==8 ||c1==9){
        if(c2==7 || c2==8 ||c2==9)
            return true;

    }

    if(c1==10 || c1==11 ||c1==12)
        if(c2==10 || c2==11 ||c2==12)
            return true;

    if(c1==13 || c1==14 ||c1==15)
        if(c2==13 || c2==14 ||c2==15)
            return true;

    if(c1==16 || c1==17 ||c1==18 ||c1==19)
        if(c2==16 || c2==17 ||c2==18 ||c2==19)
            return true;

    if(c1==22 || c1==20 ||c1==21)
        if(c2==22 || c2==20 ||c2==21)
            return true;
    if(c1==25 || c1==23 ||c1==24 ||c1==26)
        if(c2==25 || c2==23 ||c2==24 ||c2==26)
            return true;

    return false;


}
char ss[10000];
int main()
{
    int N;
    int cas = 0;
    sfi(N);
    gets(ss);
    while(N--)
    {

        gets(ss);
        int len = strlen(ss);

        CASE(cas);
        loop(i,0,len-1)
        {
            if(i!=0 && sme_btn(ss[i-1],ss[i]))
                cout<<" "<<prse(ss[i]);
            else
                cout<<prse(ss[i]);
        }
        cout<<endl;
    }

    return 0;
}

TLAC Kattis Toilet Seat

Lesson :
Always sketch the solution in paper , Never ever just start coding NO MATTER how easy the problem is .
Think 2 minutes more if you have easier / shorter solution .
It's not about getting AC , it's about writing such a program that you can proof it's correctness .


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/****************************************

@_@
Cat Got Bored *_*
#_#
*****************************************/

#include <bits/stdc++.h>


#define loop(i,s,e) for(int i = s;i<=e;i++) //including end point

#define pb(a) push_back(a)

#define sqr(x) ((x)*(x))

#define CIN ios_base::sync_with_stdio(0); cin.tie(0);

#define ll long long

#define ull unsigned long long

#define SZ(a) int(a.size())

#define read() freopen("input.txt", "r", stdin)

#define write() freopen("output.txt", "w", stdout)


#define ms(a,b) memset(a, b, sizeof(a))

#define all(v) v.begin(), v.end()

#define PI acos(-1.0)

#define pf printf

#define sfi(a) scanf("%d",&a);

#define sfii(a,b) scanf("%d %d",&a,&b);

#define sfl(a) scanf("%lld",&a);

#define sfll(a,b) scanf("%lld %lld",&a,&b);

#define sful(a) scanf("%llu",&a);

#define sfulul(a,b) scanf("%llu %llu",&a,&b);

#define sful2(a,b) scanf("%llu %llu",&a,&b); // A little different

#define sfc(a) scanf("%c",&a);

#define sfs(a) scanf("%s",a);


#define mp make_pair

#define paii pair<int, int>

#define padd pair<dd, dd>

#define pall pair<ll, ll>

#define fs first

#define sc second

#define CASE(t) printf("Case %d: ",++t) // t initialized 0

#define cCASE(t) cout<<"Case "<<++t<<": ";

#define INF 1000000000   //10e9

#define EPS 1e-9

#define flc fflush(stdout); //For interactive programs , flush while using pf (that's why __c )

using namespace std;
//Remove this before submission
char toil[1009];
char up[1009];
char down[1009];
char prf[1009];
int main()
{
    sfs(toil);

    int len = strlen(toil);
    loop(i,0,len-1)
    {
        up[i] = toil[i];
        down[i] = toil[i];
        prf[i] = toil[i];
    }

    int cnt = 0;
    int u = 0;
    int d = 0;
    int p = 0;
    loop(i,1,len-1)
    {
        char ch1 = up[i];
        char ch2 = up[i-1];
        if(ch1!=ch2)
        {
            u++;
        }
        if(ch1=='D')
        {
            u++;
            up[i] = 'U';
        }
    }

    loop(i,1,len-1)
    {
        char ch1 = down[i];
        char ch2 = down[i-1];
        if(ch1!=ch2)
        {
            d++;
        }
        if(ch1=='U')
        {
            d++;
            down[i] = 'D';
        }
    }

    loop(i,1,len-1)
    {
        char ch1 = prf[i];
        char ch2 = prf[i-1];
        if(ch1!=ch2)
        {
            p++;
        }

    }

    cout<<u<<endl;
    cout<<d<<endl;
    cout<<p<<endl;

    return 0;
}

Wednesday, May 18, 2016

HackerRank Training DS 5

Parentheses Matching Check using stack .

Be careful ans = true only when no mismatch occured and stack was "EMPTY" . else ans = No.


Try to avoid STL stack .It has many limitations .Try to use vector to imitate stack or use plain array (Best to avoid some SegFault error).



  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/****************************************

@_@
Cat Got Bored *_*
#_#
*****************************************/

#include <bits/stdc++.h>


#define loop(i,s,e) for(int i = s;i<=e;i++) //including end point

#define pb(a) push_back(a)

#define sqr(x) ((x)*(x))

#define CIN ios_base::sync_with_stdio(0); cin.tie(0);

#define ll long long

#define ull unsigned long long

#define SZ(a) int(a.size())

#define read() freopen("input.txt", "r", stdin)

#define write() freopen("output.txt", "w", stdout)


#define ms(a,b) memset(a, b, sizeof(a))

#define all(v) v.begin(), v.end()

#define PI acos(-1.0)

#define pf printf

#define sfi(a) scanf("%d",&a);

#define sfii(a,b) scanf("%d %d",&a,&b);

#define sfl(a) scanf("%lld",&a);

#define sfll(a,b) scanf("%lld %lld",&a,&b);

#define mp make_pair

#define paii pair<int, int>

#define padd pair<dd, dd>

#define pall pair<ll, ll>

#define fs first

#define sc second

#define CASE(t) printf("Case %d: ",++t) // t initialized 0

#define INF 1000000000   //10e9

#define EPS 1e-9


using namespace std;
char paren[1009];
char validator[1009];
int main()
{
    //write();

    int T;
    cin>>T;
    loop(t,1,T)
    {
        int idx = 0;


        scanf("%s",paren);

        int ans = 1;
        loop(x,0,strlen(paren)-1)
        {


            char curBrace = paren[x];
            if(curBrace=='(' || curBrace=='{' || curBrace=='[')
                validator[idx++]=(curBrace);

            else if(curBrace==')')
            {
                char cB = validator[idx-1];
                if(cB!='(')
                {
                    ans = 0;
                    break;
                }
                idx--;
            }
            else if(curBrace=='}')
            {
                char cB = validator[idx-1];
                if(cB!='{')
                {
                    ans = 0;
                    break;
                }
                idx--;
            }

            else if(curBrace==']')
            {
                char cB = validator[idx-1];
                if(cB!='[')
                {
                    ans = 0;
                    break;
                }
                idx--;
            }


        }

        if(ans && idx==0) //idx==0 is very important 
        //what if the test input is {((([ After checking there will be no mismatch but still ans = No as "STACK NOT EMPTY"
            pf("YES\n");
        else
            pf("NO\n");
        //paren.clear();
        //validator.clear();



    }



    return 0;
}

HackerRank Training DS 4

Dynamic maximum element query in O(N) using two stacks.
Let's say you are given N integers initially then you are given Q queries.
In each query , you are either told to add X to the list or remove the last added element and in the other type of query you are told to print the maximum element in the list.
You can solve it using following algorithm -  >


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
occurances[] < - 0

 stack elementSimulation , maxElem ;

curMax = -INF

 

in < - new input 

if ( add query )

{

    elementSimulation.push(in);

    occurances[in]++;

    if( in > curMax)  // Not >= as I will only keep distinct element in maxElement stack

    {

        curMax = in;

        maxElem.push(in);

    }

 

 

} 


if( remove query )

{

elementSimulation.pop();

occurances[in]--;

if( in == curMax && occurances[in]==0) // I dont have any more copy of this max element in stack

{

    maxElem.pop();

    curMax = maxElem.top(); //The next top element will be now new max

}

if( print query)

{

   print curMax;

}


}





The idea behind adding elements in the second stack (maxElem) is that , lets say you have curMax = A1. Now you are told to add , A2 . and if (A2>A1) you add that to maxElem as it is the new max as it is > curMax . But if it is lower than A1 we dont add it to the list .
Lets say our maxElem : A4 A3 A1    and A2<=A1 but A2 > A3 .
Then , I remove an element .Look as I have added A2 later than A1,A3,A4.So,If I must remove A2 before any of these A2 can never in future be maximum.So,it is okay that we didn't add A2 to the maxElem meaning if some Ai has not the potential to be the curMax now it will never be the maximum in future as I will remove (If I have the remove query) Ai before the curMax , second curMax and so on as Ai was added last and in stack who is added last gets removed first.

 ID


Code : -




  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/****************************************

@_@
Cat Got Bored *_*
#_#
*****************************************/

#include <bits/stdc++.h>


#define loop(i,s,e) for(int i = s;i<=e;i++) //including end point

#define pb(a) push_back(a)

#define sqr(x) ((x)*(x))

#define CIN ios_base::sync_with_stdio(0); cin.tie(0);

#define ll long long

#define ull unsigned long long

#define SZ(a) int(a.size())

#define read() freopen("input.txt", "r", stdin)

#define write() freopen("output.txt", "w", stdout)


#define ms(a,b) memset(a, b, sizeof(a))

#define all(v) v.begin(), v.end()

#define PI acos(-1.0)

#define pf printf

#define sfi(a) scanf("%d",&a);

#define sfii(a,b) scanf("%d %d",&a,&b);

#define sfl(a) scanf("%lld",&a);

#define sfll(a,b) scanf("%lld %lld",&a,&b);

#define mp make_pair

#define paii pair<int, int>

#define padd pair<dd, dd>

#define pall pair<ll, ll>

#define fs first

#define sc second

#define CASE(t) printf("Case %d: ",++t) // t initialized 0

#define INF 1000000000   //10e9

#define EPS 1e-9


using namespace std;
stack<ll>element_simulation;
stack<ll>max_elem;
map<ll,int>occurances;
int main()
{


    ll cur_max = -INF;
    int N;
    sfi(N);
    while(N--)
    {
        int qtype;
        sfi(qtype);
        if(qtype==1)
        {
            ll add;
            sfl(add);
            element_simulation.push(add);
            occurances[add]++;
            if(add>cur_max)
            {
                cur_max = add;
                max_elem.push(add);

            }

        }
        else if(qtype==2)
        {
            ll top_elem = element_simulation.top();
            if(!element_simulation.empty())
            element_simulation.pop();
            occurances[top_elem]--;
            if(top_elem==cur_max && occurances[top_elem]==0)
            {
                ll new_max;
                if(!max_elem.empty())
                max_elem.pop();
                if(!max_elem.empty())
                 new_max = max_elem.top();
                else
                 new_max = -INF;
                cur_max = new_max;
            }

        }
        else
        {
            pf("%lld\n",cur_max);
        }

    }

    return 0;
}


Another simplification could be avoiding the use of occurances if we changed the line 86 to
if(add>=cur_max) allowing duplicate elements in maxElem then that would be 
handled internally . We wouldn't need to worry about that.
 
 
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/****************************************

@_@
Cat Got Bored *_*
#_#
*****************************************/

#include <bits/stdc++.h>


#define loop(i,s,e) for(int i = s;i<=e;i++) //including end point

#define pb(a) push_back(a)

#define sqr(x) ((x)*(x))

#define CIN ios_base::sync_with_stdio(0); cin.tie(0);

#define ll long long

#define ull unsigned long long

#define SZ(a) int(a.size())

#define read() freopen("input.txt", "r", stdin)

#define write() freopen("output.txt", "w", stdout)


#define ms(a,b) memset(a, b, sizeof(a))

#define all(v) v.begin(), v.end()

#define PI acos(-1.0)

#define pf printf

#define sfi(a) scanf("%d",&a);

#define sfii(a,b) scanf("%d %d",&a,&b);

#define sfl(a) scanf("%lld",&a);

#define sfll(a,b) scanf("%lld %lld",&a,&b);

#define mp make_pair

#define paii pair<int, int>

#define padd pair<dd, dd>

#define pall pair<ll, ll>

#define fs first

#define sc second

#define CASE(t) printf("Case %d: ",++t) // t initialized 0

#define INF 1000000000   //10e9

#define EPS 1e-9


using namespace std;
stack<ll>element_simulation;
stack<ll>max_elem;

int main()
{


    ll cur_max = -INF;
    int N;
    sfi(N);
    while(N--)
    {
        int qtype;
        sfi(qtype);
        if(qtype==1)
        {
            ll add;
            sfl(add);
            element_simulation.push(add);
            
            if(add>=cur_max)
            {
                cur_max = add;
                max_elem.push(add);

            }

        }
        else if(qtype==2)
        {
            ll top_elem = element_simulation.top();
            if(!element_simulation.empty())
            element_simulation.pop();
            
            if(top_elem==cur_max)
            {
                ll new_max;
                if(!max_elem.empty())
                max_elem.pop();
                if(!max_elem.empty())
                 new_max = max_elem.top();
                else
                 new_max = -INF;
                cur_max = new_max;
            }

        }
        else
        {
            pf("%lld\n",cur_max);
        }

    }

    return 0;
}

Tuesday, May 17, 2016

HackerRank Training DS 3

ID



Code : -




 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/****************************************

@_@
Cat Got Bored *_*
#_#
*****************************************/

#include <bits/stdc++.h>


#define loop(i,s,e) for(int i = s;i<=e;i++) //including end point

#define pb(a) push_back(a)

#define sqr(x) ((x)*(x))

#define CIN ios_base::sync_with_stdio(0); cin.tie(0);

#define ll long long

#define ull unsigned long long

#define SZ(a) int(a.size())

#define read() freopen("input.txt", "r", stdin)

#define write() freopen("output.txt", "w", stdout)


#define ms(a,b) memset(a, b, sizeof(a))

#define all(v) v.begin(), v.end()

#define PI acos(-1.0)

#define pf printf

#define sfi(a) scanf("%d",&a);

#define sfii(a,b) scanf("%d %d",&a,&b);

#define sfl(a) scanf("%lld",&a);

#define sfll(a,b) scanf("%lld %lld",&a,&b);

#define mp make_pair

#define paii pair<int, int>

#define padd pair<dd, dd>

#define pall pair<ll, ll>

#define fs first

#define sc second

#define CASE(t) printf("Case %d: ",++t) // t initialized 0

#define INF 1000000000   //10e9

#define EPS 1e-9


using namespace std;
map<string,int>Occurance;
int main()
{

    int N;
    sfi(N);
    while(N--)
    {
        string strtemp;
        cin>>strtemp;
        Occurance[strtemp]++;
    }
    int Q;
    sfi(Q);
    while(Q--)
    {
        string stemp;
        cin>>stemp;
        cout<<Occurance[stemp]<<endl;
    }


    return 0;
}

HackerRank Training DS 2

ID



Code :-



  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
/****************************************

@_@
Cat Got Bored *_*
#_#
*****************************************/

#include <bits/stdc++.h>


#define loop(i,s,e) for(int i = s;i<=e;i++) //including end point

#define pb(a) push_back(a)

#define sqr(x) ((x)*(x))

#define CIN ios_base::sync_with_stdio(0); cin.tie(0);

#define ll long long

#define ull unsigned long long

#define SZ(a) int(a.size())

#define read() freopen("input.txt", "r", stdin)

#define write() freopen("output.txt", "w", stdout)


#define ms(a,b) memset(a, b, sizeof(a))

#define all(v) v.begin(), v.end()

#define PI acos(-1.0)

#define pf printf

#define sfi(a) scanf("%d",&a);

#define sfii(a,b) scanf("%d %d",&a,&b);

#define sfl(a) scanf("%lld",&a);

#define sfll(a,b) scanf("%lld %lld",&a,&b);

#define mp make_pair

#define paii pair<int, int>

#define padd pair<dd, dd>

#define pall pair<ll, ll>

#define fs first

#define sc second

#define CASE(t) printf("Case %d: ",++t) // t initialized 0

#define INF 1000000000   //10e9

#define EPS 1e-9


using namespace std;
vector<ll>Seq[100005];
int main()
{
    int N;
    int Q;
    sfii(N,Q);
    int lastAns = 0;
    while(Q--)
    {
      int qtype;
      sfi(qtype);
      if(qtype==1)
      {
          int x,y;
          sfii(x,y);
         int idx = (x^lastAns)%N;
         //cout<<"~"<<idx<<endl;
         Seq[idx].pb(y);
      }
      else
      {
           int x,y;
          sfii(x,y);
         int idx = (x^lastAns)%N;
         // cout<<"~"<<idx<<endl;

         int sz = Seq[idx].size();
         int new_val = Seq[idx][y%sz];
         //cout<<"//"<<y<<" "<<sz<<" "<<new_val<<endl;

         lastAns = new_val;
         pf("%d\n",lastAns);
      }



    }

    return 0;
}