Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4acca24

Browse files
Building Bridges
1 parent 4ec3154 commit 4acca24

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/* Amit Bansal - @amitbansal7 */
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
5+
struct Pair
6+
{
7+
int x, y;
8+
};
9+
10+
bool compare(struct Pair a, struct Pair b)
11+
{
12+
if (a.y == b.y)
13+
return a.x < b.x;
14+
15+
return a.y < b.y;
16+
}
17+
18+
int main()
19+
{
20+
struct Pair CityPairs[] = {{6, 2}, {4, 3}, {2, 6}, {1, 5}};
21+
int n = sizeof(CityPairs)/sizeof(CityPairs[0]);
22+
23+
sort(CityPairs, CityPairs + n, compare);
24+
25+
int maxbridges[n];
26+
for (int i = 0; i < n; i++)
27+
maxbridges[i] = 1;
28+
29+
for (int i = 1; i < n; i++)
30+
for (int j = 0; j < i; j++)
31+
if (CityPairs[j].x <= CityPairs[i].x)
32+
maxbridges[i] = max(maxbridges[i], maxbridges[j] + 1);
33+
34+
int ans = 0;
35+
for (int i = 0; i < n; i++)
36+
ans = max(ans, maxbridges[i]);
37+
38+
printf("Maximum number of bridges are : %d\n", ans);
39+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /