Light OJ: 1127 – Funny Knapsack

Problem Link : http://lightoj.com:81/volume/problem/1127



#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 CASE_PRINT      cout<<"Case "<<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));}
/*------------------------------------------------*/
 
ll ara[50];
ll l[65536],r[65536];
 
int main()
{
    ///freopen("in.txt","r",stdin);
    ///freopen("out.txt","w",stdout);
    int t;
    sf(t);
    TEST_CASE(t)
    {
        ms(l,0);
        ms(r,0);
        ll n,w;
        sffl(n,w);
        loop(i,n)
        {
            sfl(ara[i]);
        }
 
        int m1=n/2;
//        if(m1==0) m1++;
        int m2=(n-m1);
 
        int mask= 1<<(m1);
        int aa=mask;
        loop(i,mask)
        {
            loop(j,m1)
            if(check(i,j))
            {
                l[i]+=ara[j];
            }
        }
 
        mask=1<<(m2);
        int bb=mask;
        loop(i,mask)
        {
            loop(j,m2)
            {
                if(check(i,j))
                    r[i]+=ara[m1+j];
            }
        }
 
 
        sort(r,r+bb);
 
        ll ans=0;
 
        loop(i,aa)
        {
            if(w-l[i]>=0)
            ans+= upper_bound(r,r+bb,w-l[i])-r;
        }
 
        PRINT_CASE;
        pf("%lld\n",ans);
    }
    return 0;
}

Advertisements

UVa 11795 – Mega Man’s Mission

Problem Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2895


/*
     If opportunity doesn't knock, build a door.

            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
            |S|.|S|.|R|.|A|.|S|.|A|.|M|.|K|
            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

    Success is how high you bounce when you hit bottom.
*/


#include <bits/stdc++.h>

#define pii              pair <int,int>
#define pll              pair <long long,long long>
#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              1000000007
#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 stlloop(v)       for(__typeof(v.begin()) it=v.begin();it!=v.end();it++)
#define loop(i,n)        for(int i=0;i<n;i++)
#define REP(i,a,b)       for(int i=a;i<b;i++)
#define RREP(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 CASE_PRINT       cout<<"Case "<<z<<": "
#define all(a)           a.begin(),a.end()
#define intlim           2147483648
#define infinity         (1<<28)
#define ull              unsigned long long
#define gcd(a, b)        __gcd(a, b)
#define lcm(a, b)        ((a)*((b)/gcd(a,b)))

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 n;
ll dp[18][(1<<17)+100];
int ara[18][18];
int temp[18];

ll func(int idx, int mask)
{
    if(__builtin_popcount(mask)==n) return 1;

    ll &ret=dp[idx][mask];

    if(ret!=-1) return ret;

    ll p=0;

    for(int i=0; i<n; i++)
    {
        if(!check(mask,i))
        {
            if(ara[idx][i] || ara[0][i] || temp[i])
            {
                vector<int>v;
                loop(j,n)
                {
                    if(temp[j]==0 && ara[idx][j])
                    {
                        temp[j]=1;
                        v.pb(j);
                    }
                }
                p+=func(i+1,Set(mask,i));
                loop(j,SZ(v)) temp[v[j]]=0;
            }
        }
    }

    return ret=p;

}



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

    int t;
    cin>>t;
    TEST_CASE(t)
    {
        cin>>n;
        loop(i,n+1)
        {
            string str;
            cin>>str;
            loop(j,n) ara[i][j]=(str[j]=='1');
        }

        ms(dp,-1);
        CASE_PRINT;
        cout<<func(0,0)<<endl;

    }

    return 0;
}

UVa 10911 – Forming Quiz Teams


/*
User ID: turing_13
Link: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1852
*/

/*
         +-+ +-+ +-+
         |R| |.| |S|
         +-+ +-+ +-+
 */

#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 CASE_PRINT      cout<<"Case "<<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));
}
/*------------------------------------------------*/

vector<pii>v;

double dp[1<<17][16][2];
int vis[1<<17][16][2];
int n,z;

double dist(pii x, pii y)
{
    return sqrt((1.0*(x.ff-y.ff)*(x.ff-y.ff))+(1.0*(x.ss-y.ss)*(x.ss-y.ss)));
}

double call(int mask,int last, int pp)
{
    if(__builtin_popcount(mask)==2*n) return 0.0;

    double &ret=dp[mask][last][pp];
    int &state=vis[mask][last][pp];
    if(state==z) return ret;
    state=z;

    double temp=100000000.0;
//    ret=100000000.0;
    if(pp==0)
    {
        loop(i,2*n)
        {
            if(!check(mask,i))
            {
                temp=min(temp,call(Set(mask,i),i,1));
                break;
            }
        }
    }
    else
    {
        loop(i,2*n)
        {
            if(!check(mask,i))
                temp=min(temp,dist(v[last],v[i])+call(Set(mask,i),i,0));
        }
    }

    return ret=temp;
}

int main()
{
//    freopen("in.txt","r",stdin);
    ///freopen("out.txt","w",stdout);
    CIN;
    while(cin>>n && n)
    {
        z++;
        loop(i,2*n)
        {
            string str;
            cin>>str;
            int a,b;
            cin>>a>>b;
            v.pb(MP(a,b));
        }
        CASE_PRINT;
        cout<<setprecision(2)<<fixed<<call(0,0,0)<<endl;
        v.clear();
    }


    return 0;
}


Light OJ 1119 – Pimp My Ride

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


/*
         +-+ +-+ +-+
         |R| |.| |S|
         +-+ +-+ +-+
 */

#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 sff(a,b)        scanf("%d%d",&a,&b)
#define sfff(a,b,c)     scanf("%d%d%d",&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             100000000
#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 n,ara[16][16],dp[1<<16];

int func(int mask)
{
    if(mask==(1<<n)-1) return 0;
    int &ret=dp[mask];
    if(ret!=-1) return ret;
    ret=inf;
    int temp;
    loop(i,n)
    {
        if(check(mask,i)==0)
        {
            int prev=0;
            loop(j,n)
                if(check(mask,j))
                    prev+=ara[i][j];
            temp=func(Set(mask,i))+ara[i][i]+prev;
            ret=min(ret,temp);
        }
    }
    return ret;
}

int main()
{
    ///freopen("in.txt","r",stdin);
    ///freopen("out.txt","w",stdout);
    int t;
    sf(t);
    TEST_CASE(t)
    {
        sf(n);
        ms(dp,-1);
        loop(i,n) loop(j,n)
        sf(ara[i][j]);
        PRINT_CASE;
        pf("%d\n",func(0));
    }
    return 0;
}

Light OJ : 1406 – Assassin`s Creed

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


/*
         +-+ +-+ +-+
         |R| |.| |S|
         +-+ +-+ +-+
 */

#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 sff(a,b)        scanf("%d%d",&a,&b)
#define sfff(a,b,c)     scanf("%d%d%d",&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;

vector<int>graph[16];
bool visited[16][1<<16];
bool check_vis[1<<16];
int dp[1<<16];
int n,m;

void dfs(int u, int mask)
{
    visited[u][mask]=1;
    check_vis[mask]=1;
    loop(i,SZ(graph[u]))
    {
        int v=graph[u][i];
        if(!visited[v][mask|(1<<v)])
            dfs(v,mask|(1<<v));
    }
}

void allclear()
{
    loop(i,n+1)
    {
        graph[i].clear();
    }
    ms(visited,0);
    ms(check_vis,0);
    ms(dp,-1);
}

int dp_func(int mask)
{
    int &ret = dp[mask];
    if(mask==0) return 0;
    if(ret!=-1) return ret;
    ret=100;
    for(int i=mask; i>0; i=mask & (i-1))
    {
        if(check_vis[i])
        {
            ret=min(ret,1+dp_func(mask^i)); // XOR for remaining unvisited node
        }
    }
    return ret;
}

int main()
{
    ///freopen("in.txt","r",stdin);
   ///freopen("out.txt","w",stdout);
    int t,a,b;
    sf(t);
    TEST_CASE(t)
    {
        sff(n,m);
        allclear();
        loop(i,m)
        {
            sff(a,b);
            a--,b--;
            graph[a].pb(b);
        }

        loop(i,n)
        {
            dfs(i,1<<i);
        }
        PRINT_CASE;
        cout<<dp_func((1<<n)-1)<<endl;
    }
    return 0;
}

Light OJ 1042 – Secret Origins


/*

Link : http://www.lightoj.com/volume_showproblem.php?problem=1042
*/

#include <stdio.h>

int cnt(long long n)
{
    int temp=0;
    for(long long i=1; i<=n; i*=2)
    {
        if(n & i)
            temp++;
    }
    return temp;
}

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

    while(t--)
    {
        long long n,ans;
        scanf("%lld",&n);

        for(long long i=1; i<=n; i*=2)
            if(n & i)
            {
                ans=n+i;
                break;
            }

        int diff=cnt(n)-cnt(ans);

        for(int i=0; i<diff; i++)
            ans+=(1<<i);

        printf("Case %d: %lld\n",++cases,ans);
    }
    return 0;
}

(UVa) 12462 – Rectangle

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;
}