Saturday, September 29, 2012

Two Ends

In the two-player game “Two Ends”, an even number of cards is laid out in a row. On each card, face up, is written a positive integer. Players take turns removing a card from either end of the row and placing the card in their pile. The player whose cards add up to the highest number wins the game. Now one strategy is to simply pick the card at the end that is the largest — we’ll call this the greedy strategy. However, this is not always optimal, as the following example shows: (The first player would win if she would first pick the 3 instead of the 4.)

3 2 10 4

You are to determine exactly how bad the greedy strategy is for different games when the second player uses it but the first player is free to use any strategy she wishes.

Input


There will be multiple test cases. Each test case will be contained on one line. Each line will start with an even integer n followed by n positive integers. A value of n = 0 indicates end of input. You may assume that n is no more than 1000. Furthermore, you may assume that the sum of the numbers in the list does not exceed 1,000,000.

Output


For each test case you should print one line of output of the form:

In game m, the greedy strategy might lose by as many as p points.

where m is the number of the game (starting at game 1) and p is the maximum possible difference between the first player’s score and second player’s score when the second player uses the greedy strategy. When employing the greedy strategy, always take the larger end. If there is a tie, remove the left end.

Example


Input:
4 3 2 10 4
8 1 2 3 4 5 6 7 8
8 2 2 1 5 3 8 7 3
0

Output:
In game 1, the greedy strategy might lose by as many as 7 points.
In game 2, the greedy strategy might lose by as many as 4 points.
In game 3, the greedy strategy might lose by as many as 5 points.

#include<iostream>
using namespace std;
int max=0;
int main()
{
int A[100],n,game=1;
int fun(int A[],int low,int high,int s1,int s2,int turn);
while(cin>>n)
{
if(n==0)
{
break;
}
else
{
for(int i=0;i<n;i++)
{
cin>>A[i];
}
int turn=1,low=0,high=n-1,s1=0,s2=0;
::max=0;
::max=fun(A,low,high,s1,s2,turn);
}
cout<<"In game "<<game<<", the greedy strategy might lose by as many as "<<::max<<" points."<<endl;
game++;
}
return 0;
}
int fun(int A[],int low,int high,int s1,int s2,int turn)
{
//static int max=0;
if(low>high)
{
if(::max<s1-s2)
{
::max=s1-s2;
}
return ::max;
}
else if(turn==1)
{
s1+=A[low];
turn=2;
fun(A,low+1,high,s1,s2,turn);
s1-=A[low];
s1+=A[high];
fun(A,low,high-1,s1,s2,turn);
}
else if(turn==2)
{
turn=1;
if(A[low]>=A[high])
{
s2+=A[low];
fun(A,low+1,high,s1,s2,turn);
}
else
{
s2+=A[high];
fun(A,low,high-1,s1,s2,turn);
}
}
}

Counting discrete clusters of 1 in a binary 2D matrix


#include<iostream>
using namespace std;
int main()
{
int A[100][100];
int m,n;
void fun(int A[][100],int i,int j,int c,int m,int n);
cin>>m>>n;
int c=0;
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
cin>>A[i][j];
}
}
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(A[i][j]>0)
{
c++;
fun(A,i,j,c,m,n);
}
}
}
cout<<c<<endl;
return 0;
}
void fun(int A[][100],int i,int j,int c,int m,int n)
{
A[i][j]=-1*c;
if(i>0 && j>0 && A[i-1][j-1]>0)
{
fun(A,i-1,j-1,c,m,n);
}
if(i>0 && j>=0 && j<n && A[i-1][j]>0)
{
fun(A,i-1,j,c,m,n);
}
if(i>0 && j<n-1 && A[i-1][j+1]>0)
{
fun(A,i-1,j+1,c,m,n);
}
if(i>=0 && i<n && j<n-1 && A[i][j+1]>0)
{
fun(A,i,j+1,c,m,n);
}
if(i<n-1 && j<n-1 && A[i+1][j+1]>0)
{
fun(A,i+1,j+1,c,m,n);
}
if(i<n-1 && j<n && j>=0 && A[i+1][j]>0)
{
fun(A,i+1,j,c,m,n);
}
if(i<n-1 && j>0 && A[i+1][j-1]>0)
{
fun(A,i+1,j-1,c,m,n);
}
if(i<n && i>=0 && j>0 && A[i][j-1]>0)
{
fun(A,i,j-1,c,m,n);
}
}
Sample Input:
3 5
1 1 1 0 0
0 0 1 0 1
0 1 0 1 1

Output:
1

Wednesday, September 19, 2012

Counting the number of valid bracket permutations for a given length


Example:
Input: 3
Output: 5
Explanation:
The following permutations are valid:
((())),(())(),()(()),(()()),()()()
The following permutations are invalid:
)()()(,())(() etc.
Program:
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
int main()
{
int fun(char[],int,int,int);
int n;
cin>>n;
char A[100]="";
cout<<fun(A,0,0,n)<<endl;
}
int fun(char A[],int open,int close,int n)
{
static int count=0;
if(open-close<0 || open-close>n || open>n)
{
return count;
}
else if(strlen(A)==2*n)
{
//cout<<A<<endl;
count++;
//cout<<count<<" "<<endl;
return count;
}
else
{
A[strlen(A)]='(';
A[strlen(A)+1]=0;
fun(A,open+1,close,n);
A[strlen(A)-1]=0;
A[strlen(A)]=')';
A[strlen(A)+1]=0;
fun(A,open,close+1,n);
A[strlen(A)-1]=0;
return count;
}
}
Sample Input: 15
Sample Output: 9694845

Tuesday, September 11, 2012

Directory Path Parsing

Input: "C:\\a\\b\\c\\..\\.\\..\\g\\."
Output: "C:\\a\\g"

#include<iostream>
#include<string.h>
#include<signal.h>
using namespace std;
int main()
{
void fun();
char A[100]="C:\\a\\b\\c\\..\\.\\..\\g\\.\\..";
//char A[100]="C:\\a\\b";
char B[100]="";
char temp[100]="";
int top=-1;
char C[20][100];
int i=0;
while(i<strlen(A) && A[i]!='\\')
{
B[i]=A[i];
i++;
}
B[i]=0;
while(i<strlen(A))
{
int j=0;
while(i<strlen(A) && A[i]!='\\')
{
temp[j]=A[i];
i++;
j++;
}
temp[j]=0;
if(top>=0 && strcmp(temp,"..")==0)
{
top--;
}
else if(strcmp(temp,".")==0)
{
}
else
{
top++;
strcpy(C[top],temp);
}
i++;
}
for(int j=0;j<=top;j++)
{
strcat(B,C[j]);
strcat(B,"\\");
}
cout<<B<<endl;
}