Wednesday, 8 May 2013

UVa 10684 - The jackpot Solution


#include<iostream>
#include<cstdio>
#include<list>
#include<string>
#include<cstring>
#include<sstream>
#include<cctype>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<stack>
#include<fstream>
#include<cstdlib>
#include<vector>
#include<map>
#include<set>
#include<utility>
#include<iomanip>
#include<queue>
#include<deque>
#include<iterator>
#include<assert.h>
#include<bitset>
#include<climits>
#include<ctime>
#include<complex>

using namespace std;

#define SET(a) memset(a,-1,sizeof(a))
#define ALL(a) a.begin(),a.end()
#define CLR(a) memset(a,0,sizeof(a))
#define PB push_back
#define PI acos(-1.0)
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define READ freopen("input.txt", "r", stdin)
#define WRITE freopen("output.txt", "w", stdout)
#define LL long long

#define S(a) scanf("%d",&a)
#define S2(a,b) scanf("%d%d",&a,&b)
#define KS printf("Case %d: ",kk++)

#define MOD 1000000007
#define MX 100010

int main()
{
    int tmp,mx,n,x;
    while(cin>>n && n)
    {
        mx=tmp=0;
        for(int i=0;i<n;i++)
        {
            cin>>x;
            tmp+=x;
            if(tmp<0) tmp=0;
            mx=max(mx, tmp);
        }
        if(mx)  cout<<"The maximum winning streak is "<<mx<<"."<<endl;
        else cout<<"Losing streak."<<endl;
    }
    return 0;
}

UVa 10667 - Largest Block Solution


#include<iostream>
#include<cstdio>
#include<list>
#include<string>
#include<cstring>
#include<sstream>
#include<cctype>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<stack>
#include<fstream>
#include<cstdlib>
#include<vector>
#include<map>
#include<set>
#include<utility>
#include<iomanip>
#include<queue>
#include<deque>
#include<iterator>
#include<assert.h>
#include<bitset>
#include<climits>
#include<ctime>
#include<complex>

using namespace std;

#define SET(a) memset(a,-1,sizeof(a))
#define ALL(a) a.begin(),a.end()
#define CLR(a) memset(a,0,sizeof(a))
#define PB push_back
#define PI acos(-1.0)
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define READ freopen("input.txt", "r", stdin)
#define WRITE freopen("output.txt", "w", stdout)
#define LL long long

#define S(a) scanf("%d",&a)
#define S2(a,b) scanf("%d%d",&a,&b)
#define KS printf("Case %d: ",kk++)

#define MOD 1000000007
#define MX 100010


int row[110][110],mat[110][110],n,m;

int func(int start, int end)
{
    int i,j,curnt=0, mx=-1270000;
    for(i=1;i<=n;i++)
    {
        j=row[i][end]-row[i][start-1];
        curnt+=j;
        if(curnt>mx) mx=curnt;
        if(curnt<0) curnt=0;
    }
return mx;
}

int main()
{
    int i,j,temp,tc,x1,x2,y1,y2;
    cin>>tc;
    while(tc--)
    {
        cin>>n;
        for(i=0;i<=n;i++)
        row[i][0]=0;

        for(i=1;i<=n;i++)
            for(j=1;j<=n;j++)
                mat[i][j]=1;
        cin>>m;
        while(m--)
        {
            cin>>x1>>y1>>x2>>y2;
            for(i=x1;i<=x2;i++)
                for(j=y1;j<=y2;j++)
                    mat[i][j]=-1e5;
        }

        for(i=1;i<=n;i++)
            for(j=1;j<=n;j++)
            {
                //cin>>mat[i][j];
                row[i][j]=row[i][j-1]+mat[i][j];
            }


        temp=0;
        for(i=1;i<=n;i++)
            for(j=i;j<=n;j++)
                temp=max(temp,func(i,j));

        cout<<temp<<endl;
    }
return 0;
}

UVa 10656 - Maximum Sum (II) Solution


#include<iostream>
#include<vector>
using namespace std;
int main()
{
    int a,n,s,i;
    while(cin>>n)
    {
        vector<int>v;
        if(n==0)
        return 0;
        for(i=0;i<n;i++)
        {
            cin>>a;
            if(a) v.push_back(a);
        }
        int l=v.size();
        if(!l) cout<<"0";
        else
        {
            for(int i=0;i<l;i++)
            {
                if(i) cout<<" ";
                cout<<v[i];
            }

        }
        cout<<endl;
    }
return 0;
}

UVa 10074 - Take the Land Solution


#include<iostream>
#include<cstdio>
#include<list>
#include<string>
#include<cstring>
#include<sstream>
#include<cctype>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<stack>
#include<fstream>
#include<cstdlib>
#include<vector>
#include<map>
#include<set>
#include<utility>
#include<iomanip>
#include<queue>
#include<deque>
#include<iterator>
#include<assert.h>
#include<bitset>
#include<climits>
#include<ctime>
#include<complex>

using namespace std;

#define SET(a) memset(a,-1,sizeof(a))
#define ALL(a) a.begin(),a.end()
#define CLR(a) memset(a,0,sizeof(a))
#define PB push_back
#define PI acos(-1.0)
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define READ freopen("input.txt", "r", stdin)
#define WRITE freopen("output.txt", "w", stdout)
#define LL long long

#define S(a) scanf("%d",&a)
#define S2(a,b) scanf("%d%d",&a,&b)
#define KS printf("Case %d: ",kk++)

#define MOD 1000000007
#define MX 100010


int row[110][110],mat[110][110],n,m;

int func(int start, int end)
{
    int i,j,curnt=0, mx=-1270000;
    for(i=1;i<=n;i++)
    {
        j=row[i][end]-row[i][start-1];
        curnt+=j;
        if(curnt>mx) mx=curnt;
        if(curnt<0) curnt=0;
    }
return mx;
}

int main()
{
    int i,j,temp;
    while(cin>>n>>m)
    {
        if(!n && !m) break;
        for(i=0;i<=n;i++)
            row[i][0]=0;


        for(i=1;i<=n;i++)
            for(j=1;j<=m;j++)
            {
                cin>>mat[i][j];
                if(mat[i][j]==0)
                    mat[i][j]=1;
                else mat[i][j]=-1e5;
                row[i][j]=row[i][j-1]+mat[i][j];
            }

        temp=0;
        for(i=1;i<=m;i++)
            for(j=i;j<=m;j++)
                temp=max(temp,func(i,j));

        cout<<temp<<endl;
        //if(tc) cout<<endl;
    }
return 0;
}


Tuesday, 7 May 2013

UVa 836 - Largest Submatrix Solution


#include<iostream>
#include<cstdio>
#include<list>
#include<string>
#include<cstring>
#include<sstream>
#include<cctype>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<stack>
#include<fstream>
#include<cstdlib>
#include<vector>
#include<map>
#include<set>
#include<utility>
#include<iomanip>
#include<queue>
#include<deque>
#include<iterator>
#include<assert.h>
#include<bitset>
#include<climits>
#include<ctime>
#include<complex>

using namespace std;

#define SET(a) memset(a,-1,sizeof(a))
#define ALL(a) a.begin(),a.end()
#define CLR(a) memset(a,0,sizeof(a))
#define PB push_back
#define PI acos(-1.0)
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define READ freopen("input.txt", "r", stdin)
#define WRITE freopen("output.txt", "w", stdout)
#define LL long long

#define S(a) scanf("%d",&a)
#define S2(a,b) scanf("%d%d",&a,&b)
#define KS printf("Case %d: ",kk++)

#define MOD 1000000007
#define MX 100010


int row[110][110],mat[110][110],n,m;

int func(int start, int end)
{
    int i,j,curnt=0, mx=-1270000;
    for(i=1;i<=n;i++)
    {
        j=row[i][end]-row[i][start-1];
        curnt+=j;
        if(curnt>mx) mx=curnt;
        if(curnt<0) curnt=0;
    }
return mx;
}

int main()
{
    int i,j,temp,tc;
    string s[30];
    cin>>tc;
    while(tc--)
    {
        cin>>s[0];
        n=s[0].size();
        for(i=0;i<=n;i++)
        row[i][0]=0;


        for(int i=0;i<n;i++)
            if(s[0][i]=='0')
                mat[1][i+1]=-1e5;
            else mat[1][i+1]=1;

        for(int i=1;i<n;i++)
        {
            cin>>s[i];
            for(int j=0;j<n;j++)
                if(s[i][j]=='0')
                   mat[i+1][j+1]=-1e5;
                else mat[i+1][j+1]=1;
        }

        for(i=1;i<=n;i++)
            for(j=1;j<=n;j++)
                row[i][j]=row[i][j-1]+mat[i][j];

        temp=0;
        for(i=1;i<=n;i++)
            for(j=i;j<=n;j++)
                temp=max(temp,func(i,j));


        cout<<temp<<endl;
        if(tc) cout<<endl;
    }
return 0;
}


UVa 507 - Jill Rides Again Solution


#include<iostream>
#include<cstdio>
#include<list>
#include<string>
#include<cstring>
#include<sstream>
#include<cctype>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<stack>
#include<fstream>
#include<cstdlib>
#include<vector>
#include<map>
#include<set>
#include<utility>
#include<iomanip>
#include<queue>
#include<deque>
#include<iterator>
#include<assert.h>
#include<bitset>
#include<climits>
#include<ctime>
#include<complex>

using namespace std;

#define SET(a) memset(a,-1,sizeof(a))
#define ALL(a) a.begin(),a.end()
#define CLR(a) memset(a,0,sizeof(a))
#define PB push_back
#define PI acos(-1.0)
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define READ freopen("input.txt", "r", stdin)
#define WRITE freopen("output.txt", "w", stdout)
#define LL long long

#define S(a) scanf("%d",&a)
#define S2(a,b) scanf("%d%d",&a,&b)
#define KS printf("Case %d: ",kk++)

#define MOD 1000000007
#define MX 100010


int main()
{
    int tc,kk=1,n,m,arr[20005];
    cin>>tc;
    while(tc--)
    {
        cin>>n;
        int tmp=0,mx=0,st=1,stf=1,edf=-1;

        for(int i=1;i<n;i++)
        {
            S(arr[i]);
            tmp+=arr[i];
            if(tmp<0) tmp=0,st=i+1;
            if(tmp>mx || (tmp==mx && i-st>edf-stf)) mx=tmp, edf=i,stf=st;
        }

        if(!mx) cout<<"Route "<<kk++<<" has no nice parts"<<endl;
        else    cout<<"The nicest part of route "<<kk++<<" is between stops "<<stf<<" and "<<edf+1<<endl;
    }
    return 0;
}

Tutorial Links on Different Topics. BUET

Graph: BFS: http://www.personal.kent.edu/~rmuhamma/Algorithms/MyAlgorithms/GraphAlgor/breadthSearch.htm DFS: http://www.personal.kent.edu/~rmuhamma/Algorithms/MyAlgorithms/GraphAlgor/depthSearch.htm http://www.shafaetsplanet.com/planetcoding/?p=973 TREE: Diameter: http://www.shafaetsplanet.com/planetcoding/?p=521 Articulation point: http://sketchingdream.wordpress.com/as-artpoint/ http://www.ibluemojo.com/school/articul_algorithm.html Union Find: http://www.shafaetsplanet.com/planetcoding/?p=763 Topological sort: http://www.shafaetsplanet.com/planetcoding/?p=848 https://sites.google.com/site/smilitude/topsort Shortest Path: https://sites.google.com/site/smilitude/shortestpath https://sites.google.com/site/smilitude/shortestpath_problems Probabilities Expectection: http://www.codechef.com/wiki/tutorial-expectation Probabilities: http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=probabilities Combinatorics : http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=probabilities Binary Search & Bisection: http://itsfaiyaz.wordpress.com/2012/05/21/bisection/ Computational Geometry: http://www.mediafire.com/?g137w6qc9pz93al http://www.mediafire.com/?rtp2ioz4al62lsm 
http://www.mediafire.com/?6u61fn4zj6cpu05 DP: http://web.archive.org/web/20100726235908/http://www.comp.nus.edu.sg/~stevenha/myteaching/notes/8_dynamic_programming.html http://www.shafaetsplanet.com/planetcoding/?tag=%E0%A6%A1%E0%A6%BF%E0%A6%AA%E0%A6%BF https://sites.google.com/site/smilitude/recursion_and_dp Knapsack: https://sites.google.com/site/programinggconcept/0-1-knapsack http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=dynProg Matrix Expo: http://zobayer.blogspot.com/2010/11/matrix-exponentiation.html STL: https://sites.google.com/site/smilitude/cpp https://sites.google.com/site/smilitude/stl Backtracking: http://www.shafaetsplanet.com/planetcoding/?tag=%E0%A6%AC%E0%A7%8D%E0%A6%AF%E0%A6%BE%E0%A6%95%E0%A6%9F%E0%A7%8D%E0%A6%B0%E0%A7%8D%E0%A6%AF%E0%A6%BE%E0%A6%95%E0%A6%BF%E0%A6%82 Other lists of collections: http://www.shafaetsplanet.com/planetcoding/?p=879 http://wcipeg.com/wiki/Special:AllPages CTRL + Q to Enable/Disable GoPhoto.it http://itsfaiyaz.wordpress.com/category/%E0%A6%85%E0%A7%8D%E0%A6%AF%E0%A6%BE%E0%A6%B2%E0%A6%97%E0%A7%8B%E0%A6%B0%E0%A6%BF%E0%A6%A6%E0%A6%AE-%E0%A6%9F%E0%A6%BF%E0%A6%89%E0%A6%9F%E0%A7%8B%E0%A6%B0%E0%A6%BF%E0%A7%9F%E0%A6%BE%E0%A6%B2/