USACO: The Castle

0

Problem Link : http://train.usaco.org/usacoprob2?a=qBACVBxoOsW&S=castle


/*
PROG: castle
LANG: C++
*/

#include <bits/stdc++.h>

#define pii             pair <int,int>
#define sc              scanf
#define pf              printf
#define Pi              2*acos(0.0)
#define ms(a,b)         memset(a, b, sizeof(a))
#define pb(a)           push_back(a)
#define MP              make_pair
#define db              double
#define ll              long long
#define EPS             10E-10
#define ff              first
#define ss              second
#define sqr(x)          (x)*(x)
#define D(x)            cout<<#x " = "<<(x)<<endl
#define VI              vector <int>
#define DBG             pf("Hi\n")
#define MOD             100007
#define MAX             10000
#define CIN             ios_base::sync_with_stdio(0); cin.tie(0)
#define SZ(a)           (int)a.size()
#define sf(a)           scanf("%d",&a)
#define sfl(a)          scanf("%lld",&a)
#define sff(a,b)        scanf("%d %d",&a,&b)
#define sffl(a,b)       scanf("%lld %lld",&a,&b)
#define sfff(a,b,c)     scanf("%d %d %d",&a,&b,&c)
#define sfffl(a,b,c)    scanf("%lld %lld %lld",&a,&b,&c)
#define loop(i,n)       for(int i=0;i<n;i++)
#define REP(i,a,b)      for(int i=a;i<=b;i++)
#define TEST_CASE(t)    for(int z=1;z<=t;z++)
#define PRINT_CASE      printf("Case %d: ",z)
#define all(a)          a.begin(),a.end()
#define intlim          2147483648
#define inf             1000000
#define ull             unsigned long long

using namespace std;


/*----------------------Graph Moves----------------*/
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
/*------------------------------------------------*/

/*-----------------------Bitmask------------------*/
//int Set(int N,int pos){return N=N | (1<<pos);}
//int reset(int N,int pos){return N= N & ~(1<<pos);}
bool check(int N,int pos)
{
    return (bool)(N & (1<<pos));
}
/*------------------------------------------------*/

int graph[52][52];
int dp[2505];
int visited[52][52];

int r,c,s,extend=0;

int dfs(int i, int j, int x)
{
//    if(visited[i][j]) return 0;
    visited[i][j]=x;
    int d=0;
    if(!check(graph[i][j],0) && !visited[i][j-1]) d+=1+dfs(i,j-1,x);
    if(!check(graph[i][j],2) && !visited[i][j+1]) d+=1+dfs(i,j+1,x);
    if(!check(graph[i][j],1) && !visited[i-1][j]) d+=1+dfs(i-1,j,x);
    if(!check(graph[i][j],3) && !visited[i+1][j]) d+=1+dfs(i+1,j,x);
    return d;
}

int n,m;

void func(int i, int j,int x)
{
    int t=visited[i][j];
    int d=0;
    if(check(graph[i][j],0) && t!=visited[i][j-1] && j-1>=1)
    {
        d=dp[t]+dp[visited[i][j-1]];
        if(d>extend)
        {
            extend=d;
            r=i,c=j;
            s=0;
        }
        else if(d==extend)
        {
            if(j<c)
            {
                r=i,c=j;
                s=0;
            }
            else if(j==c)
            {
                if(i>r)
                {
                    r=i,j=c;
                    s=0;
                }
            }
        }
    }

    if(check(graph[i][j],2) && t!=visited[i][j+1] && j+1<=m)
    {
        d=dp[t]+dp[visited[i][j+1]];
        if(d>extend)
        {
            extend=d;
            r=i,c=j;
            s=2;
        }
        else if(d==extend)
        {
            if(j<c)
            {
                r=i,c=j;
                s=2;
            }
            else if(j==c)
            {
                if(i>r)
                {
                    r=i,j=c;
                    s=2;
                }
                else if(i==r)
                {
                    if(s!=1)
                    {
                        r=i,j=c;
                        s=2;
                    }
                }
            }
        }
    }

    // if(check(graph[i][j],2) && !visited[i][j+1]) d+=1+dfs(i,j+1,x);
    //if(check(graph[i][j],1) && !visited[i-1][j]) d+=1+dfs(i-1,j,x);
    if(check(graph[i][j],1) && t!=visited[i-1][j] && i-1>=1)
    {
        d=dp[t]+dp[visited[i-1][j]];
        if(d>extend)
        {
            extend=d;
            r=i,c=j;
            s=1;
        }
        else if(d==extend)
        {
            if(j<c)
            {
                r=i,c=j;
                s=1;
            }
            else if(j==c)
            {
                if(i>r)
                {
                    r=i,j=c;
                    s=1;
                }
                else if(i==r)
                {
                    r=i,j=c;
                    s=1;
                }
            }
        }
    }
    //if(check(graph[i][j],3) && !visited[i+1][j]) d+=1+dfs(i+1,j,x);

    if(check(graph[i][j],3) && t!=visited[i+1][j] && i+1<=n)
    {
        d=dp[t]+dp[visited[i+1][j]];
        if(d>extend)
        {
            extend=d;
            r=i,c=j;
            s=3;
        }
        else if(d==extend)
        {
            if(j<c)
            {
                r=i,c=j;
                s=3;
            }
            else if(j==c)
            {
                if(i>r)
                {
                    r=i,j=c;
                    s=3;
                }
            }
        }
    }
}

int main()
{
    freopen("castle.in","r",stdin);
    freopen("castle.out","w",stdout);
    cin>>n>>m;
    swap(n,m);
    REP(i,1,n) REP(j,1,m) cin>>graph[i][j];

    int k=0;
    int ans=-1;
    REP(i,1,n) REP(j,1,m)
    {
        if(!visited[i][j])
        {
            k++;
            dp[k]=dfs(i,j,k)+1;
            ans=max(ans,dp[k]);
        }
    }

    cout<<k<<endl<<ans<<endl;
    REP(i,1,n) REP(j,1,m)
    {
        func(i,j,visited[i][j]);
//        cout<<i<<" "<<j<<" "<<extend<<" "<<s<<endl;
    }
    cout<<extend<<endl;
    cout<<r<<" "<<c;
    if(s==2)pf(" E\n");
    else
        pf(" N\n");
    return 0;
}

Advertisements

USACO : Prime Palindromes

0

Problem Link : http://train.usaco.org/usacoprob2?a=tffkkLavmsp&S=pprime


/*
PROG: pprime
LANG: C++
*/

#include <bits/stdc++.h>

#define pii             pair <int,int>
#define sc              scanf
#define pf              printf
#define Pi              2*acos(0.0)
#define ms(a,b)         memset(a, b, sizeof(a))
#define pb(a)           push_back(a)
#define MP              make_pair
#define db              double
#define ll              long long
#define EPS             10E-10
#define ff              first
#define ss              second
#define sqr(x)          (x)*(x)
#define D(x)            cout<<#x " = "<<(x)<<endl
#define VI              vector <int>
#define DBG             pf("Hi\n")
#define MOD             100007
#define MAX             10000
#define CIN             ios_base::sync_with_stdio(0); cin.tie(0)
#define SZ(a)           (int)a.size()
#define sf(a)           scanf("%d",&a)
#define sfl(a)          scanf("%lld",&a)
#define sff(a,b)        scanf("%d %d",&a,&b)
#define sffl(a,b)       scanf("%lld %lld",&a,&b)
#define sfff(a,b,c)     scanf("%d %d %d",&a,&b,&c)
#define sfffl(a,b,c)    scanf("%lld %lld %lld",&a,&b,&c)
#define loop(i,n)       for(int i=0;i<n;i++)
#define REP(i,a,b)      for(int i=a;i<=b;i++)
#define TEST_CASE(t)    for(int z=1;z<=t;z++)
#define PRINT_CASE      printf("Case %d: ",z)
#define all(a)          a.begin(),a.end()
#define intlim          2147483648
#define inf             1000000
#define ull             unsigned long long

using namespace std;


/*----------------------Graph Moves----------------*/
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
/*------------------------------------------------*/

/*-----------------------Bitmask------------------*/
//int Set(int N,int pos){return N=N | (1<<pos);}
//int reset(int N,int pos){return N= N & ~(1<<pos);}
//bool check(int N,int pos){return (bool)(N & (1<<pos));}
/*------------------------------------------------*/


bool isPrime(ll n)
{
    if(!(n & 1))
        return false;
    else
    {
        int root= sqrt(n);
        for(int i=3; i<=root+1; i+=2)
            if(n%i==0) return false;
    }
    return true;
}


vector<ll>v;

int main()
{
    freopen("pprime.in","r",stdin);
    freopen("pprime.out","w",stdout);

    ll a,b;
    cin>>a>>b;

    bool test=1;

    for(int i=0; i<10 && test; i++)
        for(int j=0; j<10 && test; j++)
            for(int k=0; k<10 && test; k++)
                for(int l=0; l<10 && test; l++)
                {
                    ll palin=10*l+l;
                    if(k)
                    {
                        palin=1000*k+k+palin*10;
                    }
                    if(j)
                    {
                        if(k==0) palin=100000*j+j+palin*100;
                        else
                            palin=100000*j+j+palin*10;
                    }
                    if(i)
                    {
                        if(k==0 && j==0) palin=10000000*i+i+palin*1000;
                        else if(k!=0 && j==0) palin=10000000*i+i+palin*100;
                        else
                            palin=10000000*i+i+palin*10;
                    }
                    if(palin>b)
                    {
                        test=0;
                        break;
                    }
                    if(palin>=a && isPrime(palin))
                        v.pb(palin);
                }

    test=1;

    for(int i=0; i<10 && test; i++)
        for(int j=0; j<10 && test; j++)
            for(int k=0; k<10 && test; k++)
                for(int l=0; l<10 && test; l++)
                {
                    ll palin=l;
                    if(k)
                    {
                        palin=100*k+k+palin*10;
                    }
                    if(j)
                    {
                        if(k==0) palin=10000*j+j+palin*100;
                        else
                            palin=10000*j+j+palin*10;
                    }
                    if(i)
                    {
                        if(k==0 && j==0) palin=1000000*i+i+palin*1000;
                        else if(k!=0 && j==0) palin=1000000*i+i+palin*100;
                        else
                            palin=1000000*i+i+palin*10;
                    }
                    if(palin>b)
                    {
                        test=0;
                        break;
                    }
                    if(palin>=a && isPrime(palin))
                        v.pb(palin);
                }
    sort(all(v));
    loop(i,SZ(v)) cout<<v[i]<<endl;
    return 0;
}


Codeforces : 495C Treasure

0

This one is one of my favorite. 🙂


/*
User ID: Tanmoy_Datta
Link : http://codeforces.com/contest/495/problem/C
*/

#include <bits/stdc++.h>

#define pii pair <int,int>
#define sc scanf
#define pf printf
#define Pi 2*acos(0.0)
#define ms(a,b) memset(a, b, sizeof(a))
#define pb(a) push_back(a)
#define MP make_pair
#define oo 1<<29
#define dd double
#define ll long long
#define EPS 10E-10
#define ff first
#define ss second
#define MAX 10000
#define SZ(a) (int)a.size()
#define getint(a) scanf("%d",&a)
#define loop(i,n) for(int i=0;i<n;i++)
#define all(a) a.begin(),a.end()
#define intlim 2147483648
#define inf 1000000
#define rtintlim 46340
#define llim 9223372036854775808
#define rtllim 3037000499
#define ull unsigned long long
#define I int

using namespace std;

int main()
{
    ///freopen("in.txt","r",stdin);
    ///freopen("out.txt","w",stdout);
    int c,d,a,b;
    c=d=a=b=0;
    string str;
    cin>>str;
    int len=SZ(str);
    for(int i=0; i<len; i++)
    {
        if(str[i]=='(')
        {
            a++;
            c++;
        }
        if(str[i]==')')
        {
            b++;
            if(c!=0)
                c--;
        }
        if(str[i]=='#')
        {
            d++;
            c=0;
        }
        if(b+d>a)
        {
            cout<<-1<<endl;
            return 0;
        }
    }

    if(c>0)
    {
        cout<<-1<<endl;
        return 0;
    }

    for(int i=1; i<d; i++)
    {
        cout<<1<<endl;
        b++;
    }
    cout<<a-b<<endl;
    return 0;
}

(UVa) 12462 – Rectangle

0

I learned a new and nice technique from this one and it is a high thinking and time killing problem.


/*
user: turing_13
link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&amp;Itemid=8&amp;page=show_problem&amp;category=24&amp;problem=3905&amp;mosmsg=Submission+received+with+ID+14810702
*/

#include <bits/stdc++.h>

#define pii pair <int,int>
#define sc scanf
#define pf printf
#define Pi 2*acos(0.0)
#define ms(a,b) memset(a, b, sizeof(a))
#define pb(a) push_back(a)
#define MP make_pair
#define oo 1<<29
#define dd double
#define ll long long
#define EPS 10E-10
#define ff first
#define ss second
#define MAX 100010
#define SZ(a) (int)a.size()
#define getint(a) scanf("%d",&a)
#define loop(i,n) for(int i=0;i<n;i++)
#define all(a) a.begin(),a.end()
#define intlim 2147483648
#define inf 1000000
#define rtintlim 46340
#define llim 9223372036854775808
#define rtllim 3037000499
#define ull unsigned long long
#define I int

using namespace std;

/* Bits operation */

int Set(int n,int pos)  { return n = n | 1<<pos;}
bool check(int n,int pos) { return n & 1<<pos;}
int Reset(int n, int pos) { return n=n & ~(1<<pos);}

/* ---------End of Header---------0*/

ll hight[MAX],color[MAX],ans[MAX];

int main()
{
    ///freopen("in.txt","r",stdin);
    ///freopen("out.txt","w",stdout);

    int n,c;

    while(cin>>n>>c && n)
    {
        for(int i=1;i<=n;i++)
            cin>>hight[i];
        for(int i=1;i<=n;i++)
            {
                int j;
                cin>>j;
                color[i]=1<<j;
            }

        hight[0]=hight[n+1]=0;
        color[0]=color[n+1]=0;

        /* Left to Right Check */
        {
            stack<int>stk;
            stk.push(0);
            for(int i=1;i<=n;i++)
            {
                while(hight[stk.top()]>=hight[i])
                {
                    color[i]|=color[stk.top()];
                    stk.pop();
                }
                ans[i]=(i-stk.top())*hight[i];
                stk.push(i);
            }
        }

        /* Right to Left Check */
        {
            stack<int>stk;
            stk.push(n+1);
            for(int i=n;i>0;i--)
            {
                while(hight[stk.top()]>=hight[i])
                {
                    color[i]|=color[stk.top()];
                    stk.pop();
                }
                ans[i]+=(stk.top()-i)*hight[i];
                stk.push(i);
            }
        }

        for(int i=1;i<=n;i++)
            ans[i]-=hight[i];

        int ans_color=(1<<c)-1;
        ll max_ans=0;

        for(int i=1;i<=n;i++)
            if(color[i]==ans_color)
                max_ans=max(max_ans,ans[i]);

        cout<<max_ans<<endl;
    }
    return 0;
}