Tuesday, 20 March 2018

Codeforces Problem 950D - A Leapfrog in the Array (C++)

Welcome:
The question to the problem can be found here:

Explanation:

1. It is a tricky one but easy to code if you understand it.
2. using the index of the array we can solve it.
3. First lets test your attention skill. Look at the IV Figure and then the I figure.
4. If the array index number is a odd number(see input) if you add one and divide by 2, you get the required answer(the value at that index)
5. On the other hand as long as the index number is even, do x+=n- x/2.
6. Do them in Bitwise so it will reduce a lot of time to run.
7. Happy Coding.


Code:

#include<bits/stdc++.h>
using namespace std;
int main(){
long long int n,q,x;
cin>>n>>q;
while(q--){
    cin>>x;
    while(!(x&1)){
        x+=n-x/2;
    }
    cout<<(x+1>>1)<<endl;

}



return 0;}

Wednesday, 14 March 2018

UVA Problem 10685 - Nature (C++)


The question to the problem can be found here:

Explanation:
1. You need to declare two Array, one for Parent and other for number of Children in this case as chains of animals.
2. You need to use MAP data structure to store names as integers.
3. First of all Parents of Node are itself.
4. Take the Names(C) as input Node and Names.
5. Take Relationships between two elements as edges. Now the logical part.
6. If parents of these edges don't match set the 1st name(corresponding int) parent of the 2nd name(corresponding int).
7. Add the array of 2nd to the array of 1st to keep track of size of chain
8. Find the maximum element in the array
9.Happy Coding :)

Code:
#include<bits/stdc++.h>
using namespace std;
int par[5002],arr[5002];
map<string,int>mp;
int find(int n){
if(par[n] == n)
    return n;
return find(par[n]);
}
void check(int u,int v){
int U,V;
U=find(u);
V=find(v);
//cout<<U<<" "<<V<<endl;
if(U!=V){
    par[V]=U;
    arr[U]+=arr[V];
}
}
void makeset(int n){
for(int i=0;i<=n;i++)
par[i]=i;
}

int main(){

string s1,s2,s3;
int node,edge;
while(scanf("%d%d",&node,&edge)==2){
    if(node == 0 && edge ==0){
        break;
    }

    makeset(node);
    for(int i=0;i<node;i++){
        arr[i] = 1;
    }
    for(int i=0;i<node;i++){

        cin>>s1;
        mp[s1]=i;
    }

    for(int i=0;i<edge;i++){
        cin>>s2>>s3;
        int x= mp[s2];
        //cout<<x<<endl;
        int y= mp[s3];
       // cout<<y<<endl;
        check(x,y);
    }
int maximum =0;
for(int i=0;i<node;i++){
    maximum = max(maximum,arr[i]);
}
cout<<maximum<<endl;
}

return 0;}

Spoj Problem ACMCEG2C - Pick the candies (C++)

  The problem link may be found here.       Explanation: Use Deque to keep track of elements of the variety of candies. If i is gre...