Wednesday, July 30, 2014

TCHS SRM 48 Level one - 250 pt - Sending Cards solution

TCHS SRM 48 Level one - 250 pt - Sending Cards: http://community.topcoder.com/stat?c=problem_statement&pm=8456&rd=10807

TCHS SRM 48 Level one - 250 pt - Sending Cards editorial: http://community.topcoder.com/tc?module=Static&d1=hs&d2=match_editorials&d3=hs_srm48

TCHS SRM 48 Level one - 250 pt - Sending Cards solution: 


#include <iostream>
#include <vector>

class SendingCards{
    public: int howMany(std::vector <std::string> f) {
        int cnt=0;
        for(int i=0; i<f.size(); i++) for(int j=0; j<f[i].length(); j++) if(f[i][j]=='Y' && f[j][i]=='N') cnt++;
        return cnt;
    }
};

TCHS SRM 47 Level one - 250 pt - Cards shuffle solution

TCHS SRM 47 Level one - 250 pt - Cards shuffle statement: http://community.topcoder.com/stat?c=problem_statement&pm=8295&rd=10803

TCHS SRM 47 Level one - 250 pt - Cards shuffle editorial: http://community.topcoder.com/tc?module=Static&d1=hs&d2=match_editorials&d3=hs_srm47

TCHS SRM 47 Level one - 250 pt - Cards shuffle solution: 


#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

class CardsShuffle {
    public: string shuffle(string c, int f, int l, int t) {
        std::rotate(c.begin(), c.begin() + ((f-1)*t % l), c.begin() + l);
        return c;       
    }
};

my solution:
#include <iostream>
#include <cstring>

class CardsShuffle{
    public: std::string shuffle(std::string c, int f, int l, int t) {
        std::string s;
        while(t--) s=c.substr(f-1, l-(f-1)), s+=c.substr(0, f-1), s+=c.substr(l), c=s;
        return s;
    }
};

Tuesday, July 29, 2014

TCHS SRM 46 Level one - 250 pt - Pawns solution

TCHS SRM 46 Level one - 250 pt - Pawns statement: http://community.topcoder.com/stat?c=problem_statement&pm=8108&rd=10801

TCHS SRM 46 Level one - 250 pt - Pawns editorial: http://community.topcoder.com/tc?module=Static&d1=hs&d2=match_editorials&d3=hs_srm46

TCHS SRM 46 Level one - 250 pt - Pawns solution: 


#include <iostream>
#include <vector>

class Pawns{
    public: int pawnsAttack(std::vector <std::string> p) {
        int a[8][8]={0}, b[8][8]={0}, cnt=0;
        for(int i=0; i<p.size(); i++) a[8-(p[i][1]-'0')][p[i][0]-'a']++;
        for(int i=1; i<8; i++) for(int j=0; j<8; j++) if(a[i][j]) {
            if(j==0) {
                if(!a[i-1][j+1] && !b[i-1][j+1]) cnt++, b[i-1][j+1]++;
            }
            else if(j==7) {
                if(!a[i-1][j-1] && !b[i-1][j-1]) cnt++, b[i-1][j-1]++;
            }
            else {
                if(!a[i-1][j-1] && !b[i-1][j-1]) cnt++, b[i-1][j-1]++;
                if(!a[i-1][j+1] && !b[i-1][j+1]) cnt++, b[i-1][j+1]++;
            }
        }
        return cnt;
    }
};

TCHS SRM 45 Level one - 250 pt - Solving Equation solution

TCHS SRM 45 Level one - 250 pt - Solving Equation statement: http://community.topcoder.com/stat?c=problem_statement&pm=8141&rd=10799

TCHS SRM 45 Level one - 250 pt - Solving Equation editorial: http://community.topcoder.com/tc?module=Static&d1=hs&d2=match_editorials&d3=hs_srm45

TCHS SRM 45 Level one - 250 pt - Solving Equation solution: 


#include <iostream>
#include <climits>

class SolvingEquation{
    public: int solve(int a, int b, int c, int w) {
        int mi=INT_MAX, flag=0;
        for(int i=0; i<=100; ++i) for(int j=0; j<=100; ++j) for(int k=0; k<=100; ++k) {
            if(a*i + b*j + c*k == w && i+j+k<mi) mi=i+j+k, flag=1; 
        }
        if(flag==1) return mi;
        else return -1;
    }
};

Monday, July 28, 2014

TCHS SRM 44 Level one - 250 pt - Young Brother solution

TCHS SRM 44 Level one - 250 pt - Young Brother statement: http://community.topcoder.com/stat?c=problem_statement&pm=8252&rd=10795

TCHS SRM 44 Level one - 250 pt - Young Brother editorial: http://community.topcoder.com/tc?module=Static&d1=hs&d2=match_editorials&d3=hs_srm44

TCHS SRM 44 Level one - 250 pt - Young Brother solution: 

#include <iostream>
#include <vector>

class YoungBrother{
 public: std::vector <std::string> restoreWords(std::vector <std::string> l, int n, int k) {
  std::string s="";
  std::vector <std::string> ans;
  for(int i=0; i<l.size(); i++) s+=l[i];
  for(int i=0, j=0; i<n; i++, j+=k) ans.push_back(s.substr(j, k));
  return ans;
 }
};

Codeforces 2014 MemSQL Start[c]UP 2.0 - Round 1, problem: (A) Eevee solution

Codeforces 2014 MemSQL Start[c]UP 2.0 - Round 1, problem: (A) Eevee: http://codeforces.com/contest/452/problem/A

Codeforces 2014 MemSQL Start[c]UP 2.0 - Round 1, problem: (A) Eevee solution: http://ideone.com/XyPs1J


#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

int main() {
    int n, cnt=0, idx, b=0;
    char s[15], a[15][15]={"vaporeon","jolteon","flareon","espeon","umbreon","leafeon","glaceon","sylveon"};
    scanf("%d", &n);
    scanf("%s", s);
    for(int i=0; i<n; i++) if(s[i]!='.') b++;
    for(int i=0; i<8; i++) if(strlen(a[i])==n) {
        for(int j=0; j<n; j++) if(a[i][j]==s[j] && s[j]!='.') cnt++;
        if(cnt==b) {idx=i; break;}
        cnt=0;
    }
    printf("%s", a[idx]);
    return 0;
}

Sunday, July 27, 2014

timus 1080 - Map coloring solution

timus 1080 - Map coloring statement: http://acm.timus.ru/problem.aspx?space=1&num=1080

timus 1080 - Map coloring solution: http://ideone.com/vNEv5A

//http://ideone.com/vNEv5A
//169445
#include <iostream>
#include <cstdio>
using namespace std;

int n, adj[105][105]={0}, c[105], a;

bool dfs(int v, int col) {
    if(c[v]!=-1 && c[v]==col) return true;
    else if(c[v]!=-1 && c[v]!=col) return false;
    c[v]=col;
    for(int i=0; i<n; i++) if(adj[v][i] && !dfs(i, 1-col)) return false;
    return true;
}

int main() {
    scanf("%d", &n);
    for(int i=0; i<n; i++) while(true) {
        scanf("%d", &a);
        if(a==0) break;
        adj[i][a-1]=adj[a-1][i]=1;
    }
    for(int i=0; i<105; i++) c[i]=-1;
    if(dfs(0, 0)==true) for(int i=0; i<n; i++) printf("%d", c[i]);
    else printf("-1");
    return 0;
}

note: the code is almost the same of irancoldfusion's: https://bitbucket.org/pykello/timus/src/a3037a3bd7a7/1080.cpp

Friday, July 25, 2014

timus 1106 - Two Teams solution

timus 1106 - Two Teams statement: http://acm.timus.ru/problem.aspx?space=1&num=1106

timus 1106 - Two Teams solution: http://ideone.com/r8JprM


#include <iostream>
#include <cstdio>
using namespace std;

int main() {
    int adj[105][105]={0}, b[105]={0}, g[105]={0}, f, cnt, flag=0, n;
    scanf("%d", &n);
    for(int i=0; i<n; i++) {
        cnt=0;
        while(true) {
            scanf("%d", &f);
            if(f==0 && cnt==0) {flag=1; break;}
            else if(f==0) break;
            adj[i][f-1]=1, cnt++;
        }
    }
    cnt=0;
    for(int i=0; i<n; i++) {
        for(int j=0; j<n; j++) {
            if(adj[i][j]==1 && b[j]==0) {
                b[i]=b[j]=1;
                if(g[i]==1) g[j]=2;
                else g[j]=1, cnt++;
            }
        }
    }
    if(flag==1) printf("0");
    else {
        printf("%d\n", cnt);
        for(int i=0; i<n; i++) if(g[i]==1) printf("%d ", i+1);
    }
    return 0;
}

note: dfs or bfs, i dont know which applies to solve this question. but in my case, it was like this: assign this guy to one of either groups, and assign its friends(if not assigned yet) to opposing group. adj[] array is adjacency list(you would have seen that while you were studying bfs). b[] is for bool, whether that vertex has been visited or not. g[] for group.

for other versions of solutions:
1 - https://github.com/marioyc/Online-Judge-Solutions/blob/master/Timus%20Online%20Judge/1106%20-%20Two%20Teams.cpp

2 - https://github.com/galymzhan/plzgosolve/blob/master/timus/1106.cpp

3 - http://blog.csdn.net/pegasuswang_/article/details/12394355

4 - https://code.google.com/p/morbidel-timus/source/browse/trunk/done/1106.cpp?r=15

Thursday, July 24, 2014

Hackerrank Weekly Challenges - Week 7 - Die Hard 3 solution

Hackerrank Weekly Challenges - Week 7 - Die Hard 3: https://www.hackerrank.com/contests/w7/challenges/die-hard-3

Hackerrank Weekly Challenges - Week 7 - Die Hard 3 editorial: http://chasethered.com/2014/07/hackerrank-weekly-challenge-7-problem-1-die-hard-3/ &
https://www.hackerrank.com/contests/w7/challenges/die-hard-3/editorial

Hackerrank Weekly Challenges - Week 7 - Die Hard 3 solution: http://ideone.com/FET4JY

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

int gcd(int u, int v) {
    if(u==v) return u;
    if(v==0) return u;
    if(u==0) return v;
    if(~u&1) {
        if(v&1) return gcd(u>>1, v);
        return gcd(u>>1, v>>1)<<1;
    }
    if(~v&1) return gcd(u, v>>1);
    if(u>v) return gcd((u-v)>>1, v);
    return gcd((v-u)>>1, u);
}

int main() {
    int t, a, b, c;
    scanf("%d", &t);
    while(t--) {
        scanf("%d%d%d", &a, &b, &c);
        if(c%gcd(a, b)==0 && c<=max(a, b)) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

Monday, July 21, 2014

Codeforces Round #153 (Div. 1), problem: (A) Points on Line solution

Codeforces Round #153 (Div. 1), problem: (A) Points on Line: http://codeforces.com/problemset/problem/251/A

Codeforces Round #153 (Div. 1), problem: (A) Points on Line editorial: http://codeforces.com/blog/entry/6054

Codeforces Round #153 (Div. 1), problem: (A) Points on Line solution: http://ideone.com/N2cHDh


#include <iostream>
#include <cstdio>
using namespace std;

int main() {
    int n, d, x[100005];
    long long int ans=0;
    scanf("%d%d", &n, &d);
    for(int i=0; i<n; i++) scanf("%d", x+i);
    for(int i=0, j=0; i<n; i++) {
        while(x[i]-x[j]>d) j++;
        ans+=(long long)(i-j)*(i-j-1)/2;
    }
    printf("%I64d", ans);
}

i tried the code below but it gave WA: http://ideone.com/e8VzQB

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

int main() {
    int n, d, x[100005], it;
    long long int cnt1, cnt2, tmp=-1, ans=0, ma=0, mi=0, dif=0;
    const int con=1e9+1;
    map <int, int> m;
    scanf("%d%d", &n, &d);
    for(int i=0; i<n; i++) scanf("%d", &x[i]);
    for(int i=1; i<n; i++) for(int j=x[i-1]; j<x[i]; j++) m[j]=i;
    m[x[n-1]]=n;
    for(int i=0; i<n; i++) {
        if(x[i]+d>=x[n-1]) it=x[n-1];
        else it=x[i]+d;
        dif=m[it]-m[x[i]]+1;
        if(dif>=3) {
            if(m[it]==tmp) continue;
            cnt1=1, cnt2=1;
            for(int j=1; j<=dif; j++) cnt1*=j;
            for(int j=1; j<=dif-3; j++) cnt2*=j;
            ans+=cnt1/(cnt2*6);
            tmp=m[it];
        }
    }
    printf("%I64d", ans);
    return 0;
}
note: if you give right AC approach similar to my WA approach, that would be appreciated