
May 28th, 2013, 01:21 PM
#1
Building bridges
**Problem:**
There is a river that runs horizontally through an area. There are a set of cities above and below the river. Each city above the river is matched with a city below the river, and you are given this matching as a set of pairs.
You are interested in building a set of bridges across the river to connect the largest number of the matching pairs of cities, but you must do so in a way that no two bridges intersect one another.
**My Approach:**
I am sorting the first set and then finding the largest increasing subsequence from second set despite trying all test cases. I am getting wrong answer. Please help if there is something wrong with the approach or any test case i am missing.
Here is the link to the original problem: http://www.spoj.com/problems/BRIDGE/
#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
struct dj{
int x;
int y;
}a[1000];
inline int myf(dj dj1,dj dj2)
{
return dj1.x<dj2.x;
}
int main()
{
int n,t,L[1000],len;
scanf("%d",&t);
while(t)
{
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",&a[i].x);
for(int i=0;i<n;i++)
scanf("%d",&a[i].y);
sort(a,a+n,myf);
L[0]=1;
len=1;
for(int i=1;i<n;i++)
{
L[i]=1;
for(int j=0;j<i;j++)
{
if((a[i].y>a[j].y)&&(L[i]<L[j]+1))
L[i]=L[j]+1;
}
if(len<L[i])
len=L[i];
}
printf("%d\n",len);
}
return 0;
}

May 28th, 2013, 04:01 PM
#2
Re: Building bridges
Why can't you have just one bridge? That bridge would connect all cities above the river with all cities below.

May 28th, 2013, 04:23 PM
#3
Re: Building bridges
Originally Posted by nuzzle
Why can't you have just one bridge? That bridge would connect all cities above the river with all cities below.
The full problem description explains the scenario and the constraints.
All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/
C++17 Compiler: Microsoft VS2017 (15.4.0)

May 29th, 2013, 01:16 AM
#4
Re: Building bridges
Originally Posted by 2kaud
The full problem description explains the scenario and the constraints.
Well, if you get it please explain why bridging the third and fourth pairs is the correct solution?
2 5 8 10
6 4 1 2

May 29th, 2013, 07:26 AM
#5
Re: Building bridges
Originally we have 10 points.
1 2 3 4 5 6 7 8 9 10
< Cities on the first bank of river>

< River>

1 2 3 4 5 6 7 8 9 10
< Cities on second bank of river>
Test Case:
2 5 8 10
6 4 1 2
We can connect 8th city on first bank to the 1st city on second bank . This gives us one bridge. explained by (8,1) pair.
secondly, we can connect 10th city on first bank to the 2nd city on second bank. This gives us 2 Non overlapping(non cutting) bridges.
But if we consider connecting 2nd city on first bank to 6th city on second bank. Then this will cut all the three remaining bridges.
Similary while connecting 5th city on first bank to 4th city on second bank.
Thefore, maximum number of overlapping bridges are 2.

May 29th, 2013, 07:48 AM
#6
Re: Building bridges
got AC
Anyone having error in the above program, can check out this working code.
#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
struct dj{
int x;
int y;
}a[1000];
inline int myf(dj dj1,dj dj2)
{
if(dj1.x<dj2.x)
return 1;
if(dj1.x==dj2.x)
if(dj1.y<dj2.y)
return 1;
return 0;
}
int main()
{
int n,t,L[1005],len;
scanf("%d",&t);
while(t)
{
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",&a[i].x);
for(int i=0;i<n;i++)
scanf("%d",&a[i].y);
sort(a,a+n,myf);
L[0]=1;
len=1;
for(int i=1;i<n;i++)
{
L[i]=1;
for(int j=0;j<i;j++)
{
if((a[i].y>=a[j].y)&&(L[i]<L[j]+1))
L[i]=L[j]+1;
}
if(len<L[i])
len=L[i];
}
printf("%d\n",len);
}
return 0;
}

May 31st, 2013, 02:09 AM
#7
Re: Building bridges
Originally Posted by codedhrj
Thefore, maximum number of overlapping bridges are 2.
Thank you, I get it now.
I think you've come up with a nice solution strategy. First sorting the bridges according to "from" and then finding the longest increasing subsequence among the "to". That would correspond to the largest number of nonintersecting bridges.
The sorting is O(N*logN) but it looks like you have an O(N^2) algorithm for the subsequence part. According to this link, O(N*logN) is possible for the latter also,
http://en.wikipedia.org/wiki/Longest...ng_subsequence
That means a solution with a total complexity of O(N*logN) is possible for this problem.
Last edited by nuzzle; May 31st, 2013 at 02:37 AM.
Tags for this Thread
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
This a Codeguru.com survey!
OnDemand Webinars (sponsored)
