Monday, February 17, 2014

Rockethon 2014 problem A - Genetic Engineering solution

Rockethon 2014 problem A - Genetic Engineering :  http://codeforces.com/contest/391/problem/A

Rockethon 2014 problem A - Genetic Engineering solution :

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

int main() {
    int cnt, r;
    char s[105], c;
    scanf("%s\n", s);
    c=s[0];
    cnt=1;
    r=0;
    for(int i=1; i<strlen(s); i++) {
        if(s[i]!=c) {
             if(cnt%2==0) r++;
             cnt=1;
             c=s[i];
        }
        else cnt++;
    }
    if(cnt%2==0) r++;
    printf("%d\n", r);
    return 0;
}

Sunday, February 16, 2014

codechef E1 - "the white knight" solution

codechef E1 - "the white knight" : http://www.codechef.com/problems/E1
codechef E1 - "the white knight" tutorial 1 : http://www.codechef.com/wiki/tutorial-white-knight
codechef E1 - "the white knight" tutorial 2 may be : http://discuss.codechef.com/questions/1363/code-e1-the-white-knight

codechef E1 - "the white knight" solution :  http://ideone.com/tw6NWO


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

int main() {
int t, n, a[1010][1010], x, y;
char s[1010];
scanf("%d", &t);
while(t--) {
memset(a, 0, sizeof(a));
scanf("%d", &n);
for(int i=0; i<n; i++) {
scanf("%s\n", s);
for(int j=0; j<n; j++) {
if(s[j]=='P') a[i+5][j+5]++;
else if(s[j]=='K') x=i+5, y=j+5;
}
}
for(int j=n+4; j>4; j--) {
for(int i=n+4; i>4; i--) {
a[i][j]+=max(max(a[i+1][j+2], a[i+2][j+1]), max(a[i-1][j+2], a[i-2][j+1]));
}
}
printf("%d\n", a[x][y]);
}
return 0;
}

note: i tried many times and got TLE or WA. eventually got AC. i just used extra space in order not to check edge cases. i also increased char and int array size, and also dont forget to read newline at the end of string(scanf("%s\n", string)).

Codeforces Round #229 (Div. 2) problem B - Inna, Dima and Song solution

Codeforces Round #229 (Div. 2) problem B - Inna, Dima and Song :  http://codeforces.com/contest/390/problem/B

Codeforces Round #229 (Div. 2) problem B - Inna, Dima and Song solution : http://ideone.com/RQi4RR

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

int main() {
int n, a[100005], b;
long long cnt;
scanf("%d", &n);
cnt=0;
for(int i=0; i<n; i++) scanf("%d", &a[i]);
for(int i=0; i<n; i++) {
scanf("%d", &b);
if(b>a[i]*2 || b<2) cnt--;
else cnt+=pow(b, 2)/4;
}
printf("%lld\n", cnt);
return 0;
}

note: put "%I64d" instead of "%lld" when submitting to codeforces

codeforces round #229 (Div.2) problem A - Inna and Alarm Clock solution

codeforces round #229 (Div.2) problem A - Inna and Alarm Clock :  http://codeforces.com/contest/390/problem/A

codeforces round #229 (Div.2) problem A - Inna and Alarm Clock solution : http://ideone.com/vGitjD

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

int main() {
int n, a[100005], b[100005], x, y, cntx, cnty;
scanf("%d", &n);
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
cntx=cnty=0;
for(int i=0; i<n; i++) {
scanf("%d%d", &x, &y);
if(a[x]==0) a[x]++, cntx++;
if(b[y]==0) b[y]++, cnty++;
}
if(cntx<cnty) printf("%d\n", cntx);
else printf("%d\n", cnty);
return 0;
}

Sunday, February 9, 2014

codechef RRCODE - "code" solution

codechef RRCODE - "code" : http://www.codechef.com/problems/RRCODE
codechef RRCODE - "code" editorial : http://discuss.codechef.com/questions/24632/rrcode-editorial

codechef RRCODE - "code" solution : http://ideone.com/akIeg9

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

int main() {
int t, n, k, ans, a[1005];
char s[5];
scanf("%d", &t);
while(t--) {
scanf("%d%d%d", &n, &k, &ans);
for(int i=0; i<n; i++) scanf("%d", &a[i]);
scanf("%s", s);
if(s[0]=='X') {
if(k%2!=0) for(int i=0; i<n; i++) ans^=a[i];
}
else if(s[0]=='A') {
if(k!=0) for(int i=0; i<n; i++) ans&=a[i];
}
else {
if(k!=0) for(int i=0; i<n; i++) ans|=a[i];
}
printf("%d\n", ans);
}
return 0;
}

note: pay attn for "k", it doesnt matter how big k is, just whether k is even or odd matters for XOR; also, dont forget the k=0 case, in which you dont need to calculate anything at all.

Friday, February 7, 2014

codechef MIME2 - "internet media types" solution

codechef MIME2 - "internet media types" : http://www.codechef.com/problems/MIME2
codechef MIME2 - "internet media types" editorial : http://discuss.codechef.com/questions/4156/mime2-editorial

codechef MIME2 - "internet media types" solution :  http://ideone.com/BpkSuR

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

int main() {
int n, q, dot, index, flag;
char s[105][55], e[105][55], a[55], b[55], tmp[55];
scanf("%d%d", &n, &q);
for(int i=0; i<n; i++) scanf("%s%s", s[i], e[i]);
while(q--) {
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
memset(tmp, 0, sizeof(tmp));
dot=0;
flag=0;
index=0;
scanf("%s", a);
for(int i=0; i<strlen(a); i++) {
if(a[strlen(a)-1-i]=='.') {
dot=1;
break;
}
tmp[i]=a[strlen(a)-1-i];
}
if(dot) {
for(int i=0; i<strlen(tmp); i++) {
b[i]=tmp[strlen(tmp)-1-i];
}
for(int i=0; i<n; i++) {
if(strcmp(b, s[i])==0) {
flag=1;
index=i;
break;
}
}
if(flag) printf("%s\n", e[index]);
else printf("unknown\n");
}
else printf("unknown\n");
}
return 0;
}

note: n lines entered two strings, 1st is extension, 2nd is file type. and then q lines user inputs media names. coders task is to check if there exist any extension at any given file name, if no coder outputs "unknown", if yes, coder now seeks for same extension's file type through saved file extensions and types char array. if coder finds any equivalent file type for existing extension, then he outputs corresponding file type, if vice versa, he then outputs "unknown"

Thursday, February 6, 2014

codechef TACHSTCK - "chopsticks" solution

codechef TACHSTCK - "chopsticks" : http://www.codechef.com/problems/TACHSTCK
codechef TACHSTCK - "chopsticks" editorial : http://discuss.codechef.com/questions/18267/tachstck-editorial

codechef TACHSTCK - "chopsticks" solution :  http://ideone.com/6RPG0U

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

int cmp(const void *a, const void *b) {
return (*(int*)a - *(int*)b);}

int main() {
int n, d, l[100005], cnt;
scanf("%d%d", &n, &d);
cnt=0;
l[n]=INT_MAX;
for(int i=0; i<n; i++) scanf("%d", &l[i]);
qsort(l, n, sizeof(int), cmp);
for(int i=0; i<n-1;) {
if(l[i+1]-l[i]<=d) cnt++, i+=2;
else i++;
}
printf("%d\n", cnt);
return 0;
}