Monday, June 9, 2014

Topcoder High School SRM 8 250 pt - Level one - Fussy Sleeper solution

Topcoder High School SRM 250 pt - Level one - Fussy Sleeper statement: http://community.topcoder.com/stat?c=problem_statement&pm=6259&rd=10060

Topcoder High School SRM 250 pt - Level one - Fussy Sleeper editorial: http://community.topcoder.com/tc?module=Static&d1=hs&d2=match_editorials&d3=hs_srm8

Topcoder High School SRM 250 pt - Level one - Fussy Sleeper solution: 

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

class FussySleeper {
    public: vector <int> nextTime(vector <int> currentTime, int elephants) {
        int a, b, c;
        a = currentTime[0] * 100 + currentTime[1];
        b = currentTime[0], c = currentTime[1];
        do {
            currentTime[1]++;
            if(currentTime[1] == 60) currentTime[0]++, currentTime[1] = 0;
            if(currentTime[0] == 24) currentTime[0] = 0;
            a = currentTime[0] * 100 + currentTime[1];
            if(b == currentTime[0] && c == currentTime[1]) {
                if(a % elephants == 0) break;
                else {
                    currentTime[0] = 0;
                    currentTime[1] = 0;
                    break;
                } 
            }
        } while(a % elephants != 0); 
        return currentTime;
    }
};

Sunday, June 8, 2014

Codeforces Round #250 (Div. 2), problem: (C) The Child and Toy solution

Codeforces Round #250 (Div. 2), problem: (C) The Child and Toy: http://codeforces.com/contest/437/problem/C

Codeforces Round #250 (Div. 2), problem: (C) The Child and Toy editorial: http://codeforces.com/blog/entry/12513

Codeforces Round #250 (Div. 2), problem: (C) The Child and Toy solution: http://ideone.com/WAR619

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

int main() {
    int n, m, v[1005], x, y;
    long long int sum=0;
    scanf("%d%d", &n, &m);
    for(int i=0; i<n; i++) scanf("%d", &v[i]);
    for(int i=0; i<m; i++) {
        scanf("%d%d", &x, &y);
        sum+=min(v[x-1], v[y-1]);
    }
    printf("%I64d", sum);
    return 0;
}

Codeforces Round #250 (Div. 2), problem: (B) The Child and Set solution & lowest bit of binary numbers

Codeforces Round #250 (Div. 2), problem: (B) The Child and Set: http://codeforces.com/contest/437/problem/B

Codeforces Round #250 (Div. 2), problem: (B) The Child and Set editorial: http://codeforces.com/blog/entry/12513

Codeforces Round #250 (Div. 2), problem: (B) The Child and Set solution: http://ideone.com/J22uP6

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

int main() {
    int sum, lim, low_bit, n=0, a[100005];
    scanf("%d%d", &sum, &lim);
    for(int i=lim; i>=1; i--) {
        low_bit = i & -i;
        if(sum>=low_bit) a[n]=i, n++, sum-=low_bit;
        if(sum==0) break;
    }
    if(sum>0) printf("-1");
    else {
        printf("%d\n", n);
        while(n--) printf("%d ", a[n]);
    }
    return 0;
}

note: to get lowest bit of binary representations of numbers, please look: http://www.allaboutcircuits.com/vol_4/chpt_2/3.html

Saturday, June 7, 2014

Topcoder High School SRM 7 Level one - 250 pt - Text Processor solution

Topcoder High School SRM 7 Level one - 250 pt - Text Processor statement: http://community.topcoder.com/stat?c=problem_statement&pm=6002&rd=10059

Topcoder High School SRM 7 Level one - 250 pt - Text Processor editorial: http://community.topcoder.com/tc?module=Static&d1=hs&d2=match_editorials&d3=hs_srm7

Topcoder High School SRM 7 Level one - 250 pt - Text Processor solution: 

#include <iostream>
#include <cctype>
#include <algorithm>

using namespace std;

class TextProcessor {
    public: string collectLetters(string text) {
        string s="";
        for(int i=0; i<text.length(); i++) if(isalpha(text[i])) s+=tolower(text[i]);
        sort(s.begin(), s.end());
        return s;
    }
};

Topcoder High School SRM 6 Level one - 250 pt - Digital Display solution

Topcoder High School SRM 6 Level one - 250 pt - Digital Display statement: http://community.topcoder.com/stat?c=problem_statement&pm=6492&rd=10058

Topcoder High School SRM 6 Level one - 250 pt - Digital Display editorial: http://community.topcoder.com/tc?module=Static&d1=hs&d2=match_editorials&d3=hs_srm6

Topcoder High School SRM 6 Level one - 250 pt - Digital Display solution:

#include <iostream>
#include <cstdio>

using namespace std;

class DigitalDisplay {
    public: int waysToInterpret(string display) {
        int a[4], cnt=0;
        sscanf(display.c_str(), "%d:%d:%d", &a[0], &a[1], &a[2]);
        for(int i=0; i<3; i++) if(a[i%3]>=01 && a[i%3]<=12 && a[(i+1)%3]>=00 && a[(i+1)%3]<=59 && a[(i+2)%3]>=00 && a[(i+2)%3]<=59) cnt++;
        return cnt*2;
    }
};


codeforces Testing Round #10, problem: (A) Forgotten Episode solution

codeforces Testing Round #10, problem: (A) Forgotten Episode: http://codeforces.com/problemset/problem/440/A

codeforces Testing Round #10, problem: (A) Forgotten Episode solution: http://ideone.com/R5D3Tw

#include <iostream>
#include <cstdio>

using namespace std;

int a[100003]={0};

int main() {
    int n, b;
    a[0]=1;
    scanf("%d", &n);
    for(int i=1; i<n; i++) scanf("%d", &b), a[b]++;
    for(int i=0; i<=n; i++) if(a[i]==0) printf("%d", i);
    return 0;
}


version 2:

it gave me WA:
#include <iostream>
#include <cstdio>
using namespace std;

int main() {
    int n, a;
    long long int sum=0, total;
    scanf("%d", &n);
    for(int i=1; i<n; i++) scanf("%d", &a), sum+=a;
    total=(n*(n+1))/2;
    total-=sum;
    printf("%I64d", total);
    return 0;
}


while change from int to long long int gave AC: http://ideone.com/9l0AVw
#include <iostream>
#include <cstdio>
using namespace std;

int main() {
    long long int sum=0, total, n, a;
    scanf("%I64d", &n);
    for(int i=1; i<n; i++) scanf("%I64d", &a), sum+=a;
    total=(n*(n+1))/2;
    total-=sum;
    printf("%I64d", total);
    return 0;
}

note on version 2: just use total of all numbers from 1 to n: http://en.wikipedia.org/wiki/1_%2B_2_%2B_3_%2B_4_%2B_%E2%8B%AF

version 3: http://ideone.com/6Moq07
#include <iostream>
using namespace std;

int main() {
    int a, n, bin_sum=0;
    cin>>n;
    for(int i=1; i<n; i++) cin>>a, bin_sum^=a, bin_sum^=i;
    bin_sum^=n;
    cout<<bin_sum;
    return 0;
}


note on version 3: i used xor. http://codeforces.com/blog/entry/12534. but it gave WA when i used scanf() and it gave AC when i used cin>>.

Monday, June 2, 2014

Codeforces Round #250 (Div. 2), problem: (A) The Child and Homework solution

Codeforces Round #250 (Div. 2), problem: (A) The Child and Homework: http://codeforces.com/contest/437/problem/A

Codeforces Round #250 (Div. 2), problem: (A) The Child and Homework editorial: http://codeforces.com/blog/entry/12513

Codeforces Round #250 (Div. 2), problem: (A) The Child and Homework solution: http://ideone.com/G7cPqT

#include <iostream>
#include <cstdio>
#include <cstring>
#include <climits>

using namespace std;

int main() {
    int mi=INT_MAX, ma=0, ri, ra, cnti=0, cnta=0;
    char s[5][105];
    for(int i=0; i<4; i++) scanf("%s", s[i]);
    for(int i=0; i<4; i++) {
        if(strlen(s[i])-2<mi) mi=strlen(s[i])-2, ri=i;
    }
    for(int i=0; i<4; i++) if(mi<=(strlen(s[i])-2)/2) cnti++;
    for(int i=0; i<4; i++) {
        if(strlen(s[i])-2>ma) ma=strlen(s[i])-2, ra=i;
    }
    for(int i=0; i<4; i++) if(ma>=(strlen(s[i])-2)*2) cnta++;
    if(cnti>=3 && cnta>=3) printf("%c", s[2][0]); 
    else if(cnti>=3) printf("%c", s[ri][0]);
    else if(cnta>=3) printf("%c", s[ra][0]);
    else printf("%c", s[2][0]);
    return 0;
}

note: i dont know why, INT_MIN in <climits> does not work properly. i assigned ma=INT_MIN, that did not work so i changed it to 0. btw, be careful with input:
Input
A._
B.__
C.____
D.________

strlen() of A is 1, B is 2, C is 4, D is 8. that is tricky ;)