Friday, September 23, 2016

Published September 23, 2016 by with 0 comment

UVA 10101 - Bangla Numbers Solution

#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
    long long int tc=0,n;
    while (scanf("%lld",&n)!=EOF)
    {
        printf("%4lld.",++tc);
        if (n==0){ printf(" 0\n"); continue;}
        int a[20]={0},i=14;
        while (n) { a[i--]=n%10; n/=10; }
        //printf("%d\n",i);
        int b=a[0];
        if (b) printf(" %d kuti",b);
        b=a[1]*10+a[2];
        if (b) printf(" %d lakh",b);
        b=a[3]*10+a[4];
        if (b) printf(" %d hajar",b);
        b=a[5];
        if (b) printf(" %d shata",b);
        b=a[6]*10+a[7];
        if (b) printf(" %d",b);
        if (i<7) printf(" kuti");
        b=a[8]*10+a[9];
        if (b) printf(" %d lakh",b);
        b=a[10]*10+a[11];
        if (b) printf(" %d hajar",b);
        b=a[12];
        if (b) printf(" %d shata",b);
        b=a[13]*10+a[14];
        if (b) printf(" %d",b);
        printf("\n");

    }
     return 0;
}
Read More
      edit
Published September 23, 2016 by with 7 comments

UVA 11503 - Virtual Friends Solution

#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;

class DS{
private:
    vector<int>parent,rank,setSize;
    int numSets;
public:
    DS(int n)
    {
        numSets=n;
        parent.assign(n,0);
        rank.assign(n,0);
        setSize.assign(n,1);
        for (int i=0;i<n;i++) parent[i]=i;
    }
    int findParent(int i)
    {
        return parent[i]==i?i:parent[i]=findParent(parent[i]);
    }
    bool isSame(int i,int j) { return findParent(i)==findParent(j);}
    void unionSet(int x,int y)
    {
        int xRoot=findParent(x);
        int yRoot=findParent(y);
        if (xRoot==yRoot) return;
        if (rank[xRoot]>rank[yRoot])
        {
            setSize[xRoot]+=setSize[yRoot];
            parent[yRoot]=xRoot;
        }
        else
        {
            setSize[yRoot]+=setSize[xRoot];
            parent[xRoot]=yRoot;
            if (rank[xRoot]==rank[yRoot]) rank[yRoot]++;
        }
        numSets--;
    }
    int numDisjointSet(){ return numSets; }
    int SizeOfSet(int i) { return setSize[findParent(i)]; }
};
map<string,int>m;

int main()
{
    int testcases;
    scanf("%d",&testcases);
    while (testcases--)
    {
        int friendships;
        scanf("%d",&friendships);
        DS u(friendships*2);
        m.clear();
        string s1,s2;
        int indexing=0;
        while (friendships--)
        {
            cin>>s1>>s2;
            if (!m.count(s1)) m[s1]=indexing++;
            if (!m.count(s2)) m[s2]=indexing++;
            u.unionSet(m[s1],m[s2]);
            printf("%d\n",u.SizeOfSet(m[s1]));
        }
    }
    return 0;
}
Read More
      edit

Tuesday, September 20, 2016

Published September 20, 2016 by with 0 comment

Codeforces 716A Solution

#include<bits/stdc++.h>
using namespace std;

int main()
{
    int n,c;
    scanf("%d%d",&n,&c);
    int ans=0;
    int d=0;
    while (n--)
    {
        int t;
        scanf("%d",&t);
        if (t-d>c) ans=1;
        else ans++;
        d=t;
    }
    printf("%d\n",ans);
    return 0;
}
Read More
      edit