Monday, March 31, 2014

Codeforces Round #239 (Div. 2), problem: (D) Long Path solution

Codeforces Round #239 (Div. 2), problem: (D) Long Path: http://codeforces.com/contest/408/problem/D

Codeforces Round #239 (Div. 2), problem: (D) Long Path editorial in russian: http://codeforces.com/blog/entry/11333?locale=ru


В задаче требовалось промоделировать путь персонажа по графу. Заметим, что если мы пришли в вершину i, то ребра во всех вершинах с номерами меньшими, чем i, повернуты в pi. Это дает нам возможность заметить рекуррентную формулу: пусть dpi— число шагов, необходимое, чтобы добраться из вершины 1 в вершину i, если все ребра изначально повернуты назад, в pi. тогда dpi + 1 = 2dpi + 2 - dppi. Ответом будет dpn + 1.
Сложность решения — O(n).
BONUS: Умеете ли вы решать задачу, если нет ограничения, что pi ≤ i? В такой формулировке задача становится более интересной, но я пока не знаю решения.

Codeforces Round #239 (Div. 2), problem: (D) Long Path solution in english(translated via google): 


In the task required to simulate the way a character count. Note that if we came to the top of i, then the edges of all vertices with numbers less than i, turned in pi. This gives us the opportunity to observe the recurrence formula : let dpi - the number of steps required to get from one vertex to vertex i, if all the edges initially turned back in pi. then dpi + 1 = 2dpi + 2 - dppi. The answer is dpn + 1.

Complexity of the solution - O (n).

BONUS: Can you solve the problem if there is no restriction that pi ≤ i? In this formulation, the problem becomes more interesting , but I do not know the solution.

Codeforces Round #239 (Div. 2), problem: (D) Long Path solution: http://ideone.com/UO6jUA

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

int main() {
    int n, p[1005], a[1005], r;
    const int md=1e9+7;
    r=0;
    scanf("%d", &n);
    for(int i=1; i<=n; i++) scanf("%d", &p[i]);
    for(int i=1; i<=n; i++) {
        a[i]=2;
        for(int j=p[i]; j<i; j++) a[i]+=a[j], a[i]%=md;
        r+=a[i], r%=md;
    }
    printf("%d", r);
    return 0;   
}

NOTE: if you try sample inputs then you will realise that it needs some dynamic programming as indicated in problem tags as "dp". try yourself some inputs, for example:
3
1 1 1
output should be 14. the dynamic programming array (ceilings of rooms) looks like below case:
8 4 2
and after that Vasya can enter to room n+1.
and other numbers of crosses at the ceilings of rooms are displayed below:
sample input 1:
2
1 2
output 1:
4
numbers of crosses at the ceilings of rooms:
2 2

sample input 2:
4
1 1 2 3
output 2:
20
numbers of crosses at the ceilings of rooms:
8 6 4 2

sample input 3:
5
1 1 1 1 1
output 3:
62
numbers of crosses at the ceilings of rooms:
32 16 8 4 2



Codeforces Round #239 (Div. 2), problem: (C) Triangle solution

Codeforces Round #239 (Div. 2), problem: (C) Triangle: http://codeforces.com/contest/408/problem/C

Codeforces Round #239 (Div. 2), problem: (C) Triangle editorial: http://codeforces.com/blog/entry/11333

Codeforces Round #239 (Div. 2), problem: (C) Triangle solution: http://ideone.com/KVM6Cb


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

int gcd(int u, int v) {
    if(u==v) return u;
    if(u==0) return v;
    if(v==0) return u;
    if(~u & 1) {
        if(v & 1) return gcd(u>>1, v);
        else 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 a, b, x, y, g, Bx, By, B;
    scanf("%d%d", &a, &b);
    for(int i=0; i<a; i++) {
        for(int j=0; j<a; j++) {
            if(pow(i, 2) + pow(j, 2)==pow(a, 2)) {
                x=i, y=j;
                g=gcd(x, y);
                Bx=-y/g;
                By=x/g;
                B=sqrt(pow(Bx, 2) + pow(By, 2));
                if(b%B==0 && sqrt(pow(By*b/B-y, 2))!=0 && sqrt(pow(Bx*b/B-x, 2))!=0) {
                    printf("YES\n");
                    printf("0 0\n");
                    printf("%d %d\n", x, y);
                    printf("%d %d\n", Bx*b/B, By*b/B);
                    return 0;
                }
            }
        }
    }
    printf("NO");
    return 0;
}


note: the above code is my solution based on the editorial. you can have a look for another sol's also. i like the below solutions also. check other's codes if you dont get editorial well.

By BigChampion, contest: Codeforces Round #239 (Div. 1), problem: (A) Triangle, Accepted#


#include <cstdio>
using namespace std;
int gcd(int x,int y)
{
 if (!y) return x; return gcd(y,x%y);
}
int main()
{
 int a,b,x=0,y,i,j;
 scanf("%d%d",&a,&b);
 int g=gcd(a,b);
 for (i=1;i<g;i++)
     for (j=1;j<g;j++) if ((i*a!=j*b)&&(i*i+j*j==g*g)) x=i,y=j;
 if (!x) {printf("NO\n"); return 0;}
 printf("YES\n");
 printf("1 1\n%d %d\n%d %d\n",1+a/g*x,1+a/g*y,1+b/g*y,1-b/g*x);
 return 0;
}

By ehsan.m.a.e, contest: Codeforces Round #239 (Div. 2), problem: (C) Triangle, Accepted#


#include<iostream>
#include <cmath>
using namespace std;
int main()
{
int a,b,i,a2;
float t,ta,tb;
cin>>a>>b;
a2=a*a;
for(i=1;i<a;i++)
{
    t=sqrt(a2-i*i);
    if(t-(int)t==0)
    {
        ta=-1*(b*t)/a;
        tb=(b*i)/a;
        if(ta-(int)ta==0 && tb-(int)tb==0 && tb!=t)
        {
            cout<<"YES\n0 0\n"<<i<<" "<<t<<"\n"<<ta<<" "<<tb;
            return 0;
        }
    }
}
cout<<"NO";
return 0;
}

Codeforces Round #239 (Div. 2), problem: (B) Garland solution

Codeforces Round #239 (Div. 2), problem: (B) Garland: http://codeforces.com/contest/408/problem/B

Codeforces Round #239 (Div. 2), problem: (B) Garland editorial: http://codeforces.com/blog/entry/11333

Codeforces Round #239 (Div. 2), problem: (B) Garland solution: http://ideone.com/2Ls7nn


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

int main() {
    int a[50], b[50], cnt, flag;
    char s1[1005], s2[1005];
    memset(a, 0, sizeof(a));
    memset(b, 0, sizeof(b));
    cnt=flag=0;
    scanf("%s%s", s1, s2);
    for(int i=0; i<strlen(s1); i++) a[s1[i]-97]++;
    for(int i=0; i<strlen(s2); i++) b[s2[i]-97]++;
    for(int i=0; i<50; i++) {
        cnt+=min(a[i], b[i]);
        if(a[i]==0 && b[i]>0) {
            flag=1;
            break;
        }
    }
    if(flag) printf("-1");
    else printf("%d", cnt);
    return 0;
}

Codeforces Round #239 (Div. 2), problem: (A) Line to Cashier solution

Codeforces Round #239 (Div. 2), problem: (A) Line to Cashier: http://codeforces.com/contest/408/problem/A

Codeforces Round #239 (Div. 2), problem: (A) Line to Cashier editorial: http://codeforces.com/blog/entry/11333

Codeforces Round #239 (Div. 2), problem: (A) Line to Cashier solution: 

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

int main() {
    int n, k[105], m, cnt, min;
    min=INT_MAX;
    scanf("%d", &n);
    for(int i=0; i<n; i++) scanf("%d", &k[i]);
    for(int i=0; i<n; i++) {
        cnt=0;
        for(int j=0; j<k[i]; j++) {
            scanf("%d", &m);
            cnt+=m*5;
            cnt+=15;
        }
        if(cnt<min) min=cnt;
    }
    printf("%d", min);
    return 0;
}

Saturday, March 29, 2014

codechef March Challenge 2014 Walk solution

codechef March Challenge 2014 Walk: http://www.codechef.com/MARCH14/problems/WALK , http://www.codechef.com/problems/WALK

codechef March Challenge 2014 Walk editorial: http://discuss.codechef.com/questions/39939/walk-editorial

codechef March Challenge 2014 Walk solution: http://ideone.com/HuRk2m


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

int main() {
 int t, n, w, max, in, step;
 scanf("%d", &t);
 while(t--) {
  step=max=in=0;
  scanf("%d", &n);
  for(int i=0; i<n; i++) {
   scanf("%d", &w);
   if(w>step) step=max=w, in=i;
   step--;
  }
  printf("%d\n", max+in);
 }
 return 0;
}


codechef March Cook-Off 2014 Dividing Stamps solution

codechef March Cook-Off 2014 Dividing Stamps: http://www.codechef.com/COOK44/problems/DIVIDING , http://www.codechef.com/problems/DIVIDING

codechef March Cook-Off 2014 Dividing Stamps editorial: http://discuss.codechef.com/questions/40353/dividing-editorial

codechef March Cook-Off 2014 Dividing Stamps solution: http://ideone.com/HuRk2m


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

int main() {
 long long int n, a, cnt;
 cnt=0;
 scanf("%lld", &n);
 for(int i=0; i<n; i++) scanf("%lld", &a), cnt+=a;
 if(n*(n+1)/2==cnt) printf("YES\n");
 else printf("NO\n");
 return 0;
}

Codeforces Round #237 (Div. 2), problem: (A) Valera and X solution

Codeforces Round #237 (Div. 2), problem: (A) Valera and X: http://codeforces.com/contest/404/problem/A

Codeforces Round #237 (Div. 2), problem: (A) Valera and X editorial: http://codeforces.com/blog/entry/11095

Codeforces Round #237 (Div. 2), problem: (A) Valera and X solution: http://ideone.com/fq6RN9


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

int main() {
    int n, flag;
    char s[305][305], diag, other;
    flag=0;
    scanf("%d", &n);
    for(int i=0; i<n; i++) scanf("%s\n", s[i]);
    diag=s[0][0];
    other=s[0][1];
    if(diag==other) flag=1;
    for(int i=0; i<n; i++) {
        for(int j=0; j<n; j++) {
            if(i==j || i+j==n-1) {
                if(s[i][j]!=diag) {
                    flag=1;
                    break;
                }
            } 
            else {
                if(s[i][j]!=other) {
                    flag=1;
                    break;
                }
            }
        }
        if(flag) break;
    }
    if(flag) printf("NO");
    else printf("YES");
    return 0;
}

Saturday, March 22, 2014

Codeforces Round #238 (Div. 2), problem: (B) Domino Effect solution

Codeforces Round #238 (Div. 2), problem: (B) Domino Effect: http://codeforces.com/contest/405/problem/B

Codeforces Round #238 (Div. 2), problem: (B) Domino Effect solution: 

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

int main() {
    int n, r, l, ans, cnt;
    ans=cnt=r=l=0;
    char s[3005];
    scanf("%d", &n);
    scanf("%s", s);
    for(int i=0; i<n; i++) {
        if(s[i]=='.' && r==0) cnt++;
        if(s[i]=='L') {
            cnt=0;
            r=0;
        }
        if(s[i]=='R') {
            ans+=cnt;
            cnt=0;
            r=1;
        }
    }
    if(cnt>0) ans+=cnt, cnt=0;
    r=l=0;
    for(int i=0; i<n; i++) {
        if(s[i]=='R') r=1;
        if(s[i]=='.' && r==1) cnt++;
        if(s[i]=='L') {
            if(cnt%2==1) ans++;
            cnt=0;
            r=0;
        }
    }
    printf("%d", ans);
    return 0;
}

Codeforces Round #238 (Div. 2), problem: (A) Gravity Flip solution

Codeforces Round #238 (Div. 2), problem: (A) Gravity Flip: http://codeforces.com/contest/405/problem/A

Codeforces Round #238 (Div. 2), problem: (A) Gravity Flip solution: 

#include <iostream>
#include <stdio.h>      /* printf */
#include <stdlib.h>     /* qsort */

int n, a[105];

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

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

Thursday, March 20, 2014

Codeforces Round #105 (Div. 2), problem: (A) Insomnia cure solution

Codeforces Round #105 (Div. 2), problem: (A) Insomnia cure: http://codeforces.com/problemset/problem/148/A

Codeforces Round #105 (Div. 2), problem: (A) Insomnia cure solution: http://ideone.com/8QDphs

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

int main() {
    int k, l, m, n, d, a[100005], cnt;
    memset(a, 0, sizeof(a));
    cnt=0;
    scanf("%d%d%d%d%d", &k, &l, &m, &n, &d);
    for(int i=k-1; i<d; i+=k) a[i]++;
    for(int i=l-1; i<d; i+=l) a[i]++;
    for(int i=m-1; i<d; i+=m) a[i]++;
    for(int i=n-1; i<d; i+=n) a[i]++;
    for(int i=0; i<d; i++) if(a[i]) cnt++;
    printf("%d", cnt);
    return 0;
}