Wednesday, April 23, 2014

Codeforces Coder-Strike 2014 - Finals (online edition, Div. 2), problem: (B) Start Up solution

Codeforces Coder-Strike 2014 - Finals (online edition, Div. 2), problem: (B) Start Up: http://codeforces.com/contest/421/problem/B

Codeforces Coder-Strike 2014 - Finals (online edition, Div. 2), problem: (B) Start Up solution: 

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

using namespace std;

int main()
{
   int flag=0;
   char s[100005];
   scanf("%s", s);
   for(int i=0; i<strlen(s); i++) {
       if(s[i]!='A' && s[i]!='H' && s[i]!='I' && s[i]!='M' && s[i]!='O' && s[i]!='T' && s[i]!='U' && s[i]!='V' && s[i]!='W' && s[i]!='X' && s[i]!='Y') {
           flag=1;
           break;
       }
   }
   if(flag==0) {
       for(int i=0; i<strlen(s)/2+1; i++) {
           if(s[i]!=s[strlen(s)-i-1]) {
               flag=1;
               break;
           }
       }
   }
   if(flag==1) printf("NO");
   else printf("YES");
   return 0;
}

note: look up the ascii table and see which letters mirror itself.

Codeforces Coder-Strike 2014 - Finals (online edition, Div. 2), problem: (A) Pasha and Hamsters solution

Codeforces Coder-Strike 2014 - Finals (online edition, Div. 2), problem: (A) Pasha and Hamsters: http://codeforces.com/contest/421/problem/A

Codeforces Coder-Strike 2014 - Finals (online edition, Div. 2), problem: (A) Pasha and Hamsters solution: http://ideone.com/g8KLnU

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

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

Monday, April 21, 2014

Codeforces Coder-Strike 2014 - Round 2, problem: (C) Jeopardy! solution

Codeforces Coder-Strike 2014 - Round 2, problem: (C) Jeopardy! : http://codeforces.com/contest/413/problem/C

Codeforces Coder-Strike 2014 - Round 2, problem: (C) Jeopardy! solution: http://ideone.com/BPHLAz

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

int main() {
    int n, m, a[105], b, c[105]={0}, it;
    long long int s=0, d[105]={0};
    scanf("%d%d", &n, &m);
    for(int i=0; i<n; i++) scanf("%d", &a[i]), d[i]=a[i];
    for(int i=0; i<m; i++) scanf("%d", &b), c[b-1]++;
    for(int i=0; i<n; i++) if(c[i]==0) s+=a[i];
    sort(d, d+n);
    it=n-1;
    for(int i=0; i<n; i++) if(c[i]) s+=max(s, d[it]), it--;
    printf("%I64d\n", s);
    return 0;
}

note: R2 needs the highest point possible at the end of the game. so to do that, in hope of getting higher sum start with non-auction questions. and summing them all up, move onto auction questions. the advantage of auction questions is that they may get increased to R2's collected total points. and for every auction question start with highest point possible, in order to achieve that we sorted array d[]. looping through every question check whether that is auction question or not, if yes, then add the highest available auction point to collection (s+=max(s, d[it])). to get that highest point available we already sorted d[] array and we iterate from last index to the first (it=n-1, d[it], it--).




Codeforces Coder-Strike 2014 - Round 2, problem: (B) Spyke Chatting solution

Codeforces Coder-Strike 2014 - Round 2, problem: (B) Spyke Chatting: http://codeforces.com/contest/413/problem/B

Codeforces Coder-Strike 2014 - Round 2, problem: (B) Spyke Chatting solution: http://ideone.com/sd0vzI


Here is AC version: 
#include <iostream>
#include <cstdio>
using namespace std;

const int ln=2*1e4;
int a[ln+1][12]={0}, b[ln+1]={0}, c[12]={0};

int main() {
    int n, m, k, x, y, r;
    scanf("%d%d%d", &n, &m, &k);
    for(int i=0; i<n; i++) for(int j=0; j<m; j++) scanf("%d", &a[i][j]);
    for(int i=0; i<k; i++) scanf("%d%d", &x, &y), b[x-1]++, c[y-1]++;
    for(int i=0; i<n; i++) {
        r=0;
        for(int j=0; j<m; j++) if(a[i][j]) r+=c[j];
        r-=b[i];
        printf("%d ", r);
    }
    return 0;
}

note: you get every x and y to its array. because that sent message y from x affects every worker on that chat column, meaning a[i][j]=1 would get its share from y. that is why, we iterate over inner loop to notice the worker for every sent message if worker in that chatroom (for(int j=0; j<m; j++) if(a[i][j]) r+=c[j]; ) and then we subtract if that worker himself sent message (r-=b[i];  ).

Here is TLE version 1:
#include <iostream>
#include <cstdio>
using namespace std;

const int ln=2*1e4;
int a[ln+1][12]={0}, b[ln+1]={0};

int main() {
    int n, m, k, x, y;
    scanf("%d%d%d", &n, &m, &k);
    for(int i=0; i<n; i++) for(int j=0; j<m; j++) scanf("%d", &a[i][j]);
    while(k--) {
        scanf("%d%d", &x, &y);
        for(int i=0; i<n; i++) {
            if(i==x-1) continue;
            if(a[i][y-1]) b[i]++;
        }
    }
    for(int i=0; i<n; i++) printf("%d ", b[i]);
    return 0;
}


And here is TLE version 2:
#include <iostream>
#include <cstdio>
using namespace std;

const int ln=2*1e4;
int a[ln+1][12]={0}, b[ln+1]={0}, c[ln+1][12]={0};

int main() {
    int n, m, k, x, y;
    scanf("%d%d%d", &n, &m, &k);
    for(int i=0; i<n; i++) for(int j=0; j<m; j++) scanf("%d", &a[i][j]);
    while(k--) scanf("%d%d", &x, &y), c[x-1][y-1]++;
    for(int i=0; i<n; i++) for(int j=0; j<m; j++) if(c[i][j]) {
        for(int p=0; p<n; p++) {
            if(p==i) continue;
            if(a[p][j]) b[p]+=c[i][j];
        }
    }
    for(int i=0; i<n; i++) printf("%d ", b[i]);
    return 0;
}

note on TLE: i think idea was correct, but brute force dont work as fast as its AC version above.

Sunday, April 20, 2014

Codechef April Cook-Off 2014 Snape and Ladder solution

Codechef April Cook-Off 2014 Snape and Ladder: http://www.codechef.com/COOK45/problems/SNAPE/

Codechef April Cook-Off 2014 Snape and Ladder editorial: http://discuss.codechef.com/questions/41500/snape-editorial

Codechef April Cook-Off 2014 Snape and Ladder solution: 

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

int main() {
    int t;
    float b, ls, rs;
    scanf("%d", &t);
    while(t--) {
        scanf("%f%f", &b, &ls);
        rs=sqrt(pow(ls, 2) - pow(b, 2));
        printf("%.6f ", rs);
        rs=sqrt(pow(ls, 2) + pow(b, 2));
        printf("%.6f\n", rs);
    }
    return 0;
}

Codeforces Round #241 (Div. 2), problem: (B) Art Union solution

Codeforces Round #241 (Div. 2), problem: (B) Art Union: http://codeforces.com/contest/416/problem/B

Codeforces Round #241 (Div. 2), problem: (B) Art Union solution: http://ideone.com/DSaHao


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

int main() {
    int n, m, a[6]={0}, t;
    scanf("%d%d", &m, &n);
    while(m--) {
        for(int i=0; i<n; i++) {
            scanf("%d", &t);
            if(i==0) a[i]+=t;
            else {
                if(a[i-1]>a[i]) a[i]=a[i-1]+t;
                else a[i]+=t;
            }
            if(i==n-1) printf("%d ", a[i]);
        }
    }
    return 0;
}

Saturday, April 19, 2014

Codeforces Round #241 (Div. 2), problem: (A) Guess a number! solution

Codeforces Round #241 (Div. 2), problem: (A) Guess a number! : http://codeforces.com/contest/416/problem/A

Codeforces Round #241 (Div. 2), problem: (A) Guess a number! solution: http://ideone.com/S4pcpK



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

int main() {
    int n, x, l=-2*1e9, r=2*1e9;
    char s1[5], s2[5];
    scanf("%d", &n);
    while(n--) {
        scanf("%s%d%s", s1, &x, s2);
        if(s1[0]=='>' && s1[1]=='=' && s2[0]=='Y' && x>l) l=x;
        else if(s1[0]=='>' && s1[1]!='=' && s2[0]=='Y' && x+1>l) l=x+1;
        else if(s1[0]=='>' && s1[1]=='=' && s2[0]=='N' && x-1<r) r=x-1;
        else if(s1[0]=='>' && s1[1]!='=' && s2[0]=='N' && x<r) r=x;
        else if(s1[0]=='<' && s1[1]=='=' && s2[0]=='Y' && x<r) r=x;
        else if(s1[0]=='<' && s1[1]!='=' && s2[0]=='Y' && x-1<r) r=x-1;
        else if(s1[0]=='<' && s1[1]=='=' && s2[0]=='N' && x+1>l) l=x+1;
        else if(s1[0]=='<' && s1[1]!='=' && s2[0]=='N' && x>l) l=x;
    }
    if(l<=r) printf("%d", l);
    else printf("Impossible");
    return 0;
}


note: first of all,  think that whole numbers between -2000000000 and 2000000000 are acceptable, because there is no restraint. assign l=-2*1e9 (the most left hand side) and r=2*1e9 (the rightmost hand side). and then focus on constraints, based on them try to minimize your left or right hand side. to get the idea look pictures below:


that is before any contsraints. and now we apply constraints:



and after constraints the left handside and right handside set to new points. after applying all the constraints we check whether if left handside is still less than or equal to right hand side, if(l<=r), if yes we print whatever number between that range. if left hand side already passed right handside, if(l>r), then print Impossible.

got the idea? and now looking for more challenging one? then peek at it: Codechef Guessing Game - http://www.codechef.com/problems/A3

Codeforces Coder-Strike 2014 - Qualification Round problem A - Password Check solution

Codeforces Coder-Strike 2014 - Qualification Round problem A - Password Check : http://codeforces.com/contest/411/problem/A

Codeforces Coder-Strike 2014 - Qualification Round problem A - Password Check solution : 


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

int main() {
    int num, big, small;
    char s[105];
    num=big=small=0;
    scanf("%s", s);
    for(int i=0; i<strlen(s); i++) {
        if(s[i]>47 && s[i]<58) num++;
        else if(s[i]>64 && s[i]<91) big++;
        else if(s[i]>96 && s[i]<123) small++;
    }
    if(num>0 && big>0 && small>0 && strlen(s)>=5) printf("Correct");
    else printf("Too weak");
    return 0;
}