Monday, November 14, 2016

1241 - Pinocchio

Problem Link : http://lightoj.com/volume_showproblem.php?problem=1241

Solution :

#include<bits/stdc++.h>
using namespace std;


int main()
{

    int t;
    scanf("%d",&t);

    int n,a,cnt,b,c;

    for(int ca=1; ca<=t; ca++)
    {
        cnt=0;
        a=-1;
        b=2;
        scanf("%d",&n);
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&a);
            c=a-b;

            cnt=cnt+(c/5);
            if((c%5)!=0) cnt++;

            b=a;

        }
        printf("Case %d: %d\n",ca,cnt);
    }


}

1225 - Palindromic Numbers (II)

Problem Link : http://lightoj.com/volume_showproblem.php?problem=1225

Solution :

#include<bits/stdc++.h>

using namespace std;

int main()
{
    int t,l,m,cnt;
    char s[20];

    scanf("%d",&t);

    for(int ca=1;ca<=t;ca++)
    {
       scanf("%s",s);
       l=strlen(s);

       if(l%2==0) m=l/2;
       else m=(l/2)+1;
       cnt =1;

       for(int i=0;i<m;i++)
       {
          if(s[i]==s[l-i-1]);
          else
          {
              cnt=0;
              break;
          }
       }

       if(cnt==0) printf("Case %d: No\n",ca);
       else printf("Case %d: Yes\n",ca);

    }
}

1166 - Old Sorting

Problem Link : http://lightoj.com/volume_showproblem.php?problem=1166

Solution :

#include<bits/stdc++.h>

using namespace std;

int main()
{

    int t;
    int n,a,b,c;
    int cnt,mnt,ans,m;
    int ara[102];

    scanf("%d",&t);

    for(int ca=1; ca<=t; ca++)
    {
        scanf("%d",&n);

        m=n,cnt=0,mnt=0,ans=0;

        for(int i=1; i<=n; i++)
        {
            scanf("%d",&a);
            ara[i]=a;
        }


        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
            {
                if(ara[j]==i and j!=i)
                {

                    ara[j]=ara[i];
                    ara[i]=i;
                    cnt++;
                }
            }

        }

        printf("Case %d: %d\n",ca,cnt);
    }
}

Monday, November 7, 2016

1141 - Number Transformation

Problem Linkhttp://lightoj.com/volume_showproblem.php?problem=1141

Solution :

#include<bits/stdc++.h>

using namespace std;

bool status[1021];

vector<int>factor;
vector< int>prime;

void sieve()
{
    int n=1020;
    int sq=sqrt(n);

    for( int i=4; i<=n; i=i+2) status[i]=true;

    prime.push_back(2);

    for( int i=3; i<=sq; i=i+2)
    {
        if(status[i]==false)
        {
            for(int j=i*i; j<=n; j=j+i) status[j]=true;
        }
    }
    status[1]=1;
    status[0]=1;

    for( int i=3; i<=n; i=i+2)
    {
        if(status[i]==0) prime.push_back(i);
    }
}

int visited[1020];

int bfs(int s,int d)
{
    int u,p;
    memset(visited,-1,sizeof(visited));

    queue<int>q;

    q.push(s);
    visited[s]=0;

    while(!q.empty())
    {
        u=q.front();
        q.pop();

        for(int i=0; prime[i]<u; i++)
        {
            if(u%prime[i]==0)
            {
                factor.push_back(prime[i]);
            }
        }


        for(int i=0; i<factor.size(); i++)
        {
            p=u+factor[i];

            if(visited[p]==-1 and p<=d)
            {
                visited[p]=visited[u]+1;
                q.push(p);

            }
        }
        factor.clear();
    }
    return visited[d];


}

int main()
{
    int t,a,b,ans;
    sieve();
    scanf("%d",&t);

    for(int ca=1; ca<=t; ca++)
    {
        scanf("%d%d",&a,&b);
        ans=bfs(a,b);
        printf("Case %d: %d\n",ca,ans);
        factor.clear();
    }
}

1433 - Minimum Arc Distance

Problem Linkhttp://lightoj.com/volume_showproblem.php?problem=1433

Solution :

#include<bits/stdc++.h>

#define PI acos(-1.0)

using namespace std;

double cosinv(double angle)
{
    return acos(angle)*(180/PI);
}

int main()
{
    int t;
    scanf("%d",&t);

    double ox,oy,ax,ay,bx,by,r,s,angle,a;

    for(int ca=1; ca<=t; ca++)
    {
        scanf("%lf%lf%lf%lf%lf%lf",&ox,&oy,&ax,&ay,&bx,&by);

        r=sqrt(((ox-ax)*(ox-ax))+((oy-ay)*(oy-ay)));
        a=sqrt(((ax-bx)*(ax-bx))+((ay-by)*(ay-by)));

        angle=(2*(r*r)-(a*a))/(2*r*r);
        angle=cosinv(angle);
        angle=(angle*PI)/180;

        s=r*angle;

        printf("Case %d: %f\n",ca,s);
    }
}

1331 - Agent J

Problem Link : http://lightoj.com/volume_showproblem.php?problem=1331

Solution

#include<bits/stdc++.h>

#define PI acos(-1.0)

using namespace std;

double cosinv(double angle)
{
    return acos(angle)*(180/PI);
}

int main()
{
    int t;
    scanf("%d",&t);

    double r1,r2,r3,area,a1,a2,a3,t1,t2,t3;

    for(int ca=1; ca<=t; ca++)
    {
        scanf("%lf%lf%lf",&r1,&r2,&r3);

        t1=((r1+r2)*(r1+r2))+((r1+r3)*(r1+r3))-((r2+r3)*(r2+r3));
        t1=t1/(2*(r1+r2)*(r1+r3));
        t1=cosinv(t1);

        t2=((r1+r2)*(r1+r2))+((r2+r3)*(r2+r3))-((r1+r3)*(r1+r3));
        t2=t2/(2*((r1+r2))*(r2+r3));
        t2=cosinv(t2);

        t3=((r1+r3)*(r1+r3))+((r2+r3)*(r2+r3))-((r1+r2)*(r1+r2));
        t3=t3/(2*((r1+r3)*(r2+r3)));
        t3=cosinv(t3);

        a1=PI*(r1*r1)*(t1/360);
        a2=PI*(r2*r2)*(t2/360);
        a3=PI*(r3*r3)*(t3/360);

        area=.5*(r1+r2)*(r2+r3)*sin(t2*PI/180);
        area=area-a1-a2-a3;

        printf("Case %d: %f\n",ca,area);
    }

}

Saturday, November 5, 2016

1311 - Unlucky Bird

Problem Linkhttp://lightoj.com/volume_showproblem.php?problem=1311

Solution

#include<bits/stdc++.h>

using namespace std;

int main()
{
    int t;
    scanf("%d",&t);
    double v1,v2,v3,a1,a2,t1,t2,d,ans,s1,s2;

    for(int ca=1;ca<=t;ca++)
    {
        scanf("%lf%lf%lf%lf%lf",&v1,&v2,&v3,&a1,&a2);

        s1=(v1*v1)/(2.0*a1);
        s2=(v2*v2)/(2.0*a2);

        d=s1+s2;

        t1=v1/a1;
        t2=v2/a2;

        t1=max(t1,t2);

        ans=v3*t1;

        printf("Case %d: %f %f\n",ca,d,ans);

    }

}

Friday, November 4, 2016

1337 - The Crystal Maze

Problem Linkhttp://lightoj.com/volume_showproblem.php?problem=1337

Solution :

#include<bits/stdc++.h>
#define pii pair<int,int>

using namespace std;

int cell[502][502];
int dell[502][502];
int visited[502][502];


int fx[]= {1,-1,0,0};
int fy[]= {0,0,1,-1};

int r,c,ok=1;

map<int,int>mp;

int bfs(pii s)
{
    visited[s.first][s.second]=1;
    queue<pii>q;
    pii u,a;
    int x,y,cnt=0;
    if(cell[s.first][s.second]==3) cnt++;
    dell[s.first][s.second]=ok;
    q.push(s);

    while(!q.empty())
    {
        u=q.front();
        q.pop();

        for(int i=0; i<4; i++)
        {
            x=fx[i]+u.first;
            y=fy[i]+u.second;

            if(x>=1 and x<=r and y>=1 and y<=c and cell[x][y]!=2 and visited[x][y]==0)
            {
                visited[x][y]=1;
                dell[x][y]=ok;
                if(cell[x][y]==3) cnt++;
                a.first=x;
                a.second=y;
                q.push(a);
            }
        }

    }
    mp[ok]=cnt;
    return cnt;

}

int main()
{
    int t,ans,q,a,b;
    char ch;
    pii s,s1,s2,d;
    scanf("%d",&t);

    for(int ca=1; ca<=t; ca++)
    {
        scanf("%d%d%d",&r,&c,&q);
        getchar();
        memset(dell,-1,sizeof(dell));
        memset(visited,0,sizeof(visited));
        ok=1;
        for(int i=1; i<=r; i++)
        {
            for(int j=1; j<=c; j++)
            {
                scanf("%c",&ch);
                if(ch=='.') cell[i][j]=1;
                else if(ch=='#') cell[i][j]=2;
                else if(ch=='C')
                {
                    cell[i][j]=3;

                }
            }
            getchar();
        }
        printf("Case %d:\n",ca);
        for(int i=0; i<q; i++)
        {
            scanf("%d%d",&a,&b);

            s.first=a;
            s.second=b;
            if(visited[a][b]!=0) printf("%d\n",mp[dell[a][b]]);
            else
            {
                ans= bfs(s);
                printf("%d\n",ans);
            }
            ok++;
        }

        mp.clear();

    }

}

Wednesday, November 2, 2016

1020 - A Childhood Game

Problem Linkhttp://lightoj.com/volume_showproblem.php?problem=1020

Solution :

#include<bits/stdc++.h>
#define ll long long
using namespace std;

int main()
{
    string a;
    ll int n,ok;

    int t;

    scanf("%d",&t);

    for(int ca=1;ca<=t;ca++)
    {
        cin>>n>>a;

        ok=0;

        if(a=="Alice")
        {
            if((n-1)%3==0)
            {
                ok=1;
            }
            else ok=0;
        }
        else
        {
        if(n%3==0) ok=0;
        else ok=1;
        }

        if(ok==1) printf("Case %d: Bob\n",ca);
        else printf("Case %d: Alice\n",ca);
    }


}