Interval Selection
Problem Statement :
Given a set of n intervals, find the size of its largest possible subset of intervals such that no three intervals in the subset share a common point. Input Format The first line contains an integer, s, denoting the number of interval sets you must find answers for. The s.(n+1) subsequent lines describe each of the s interval sets as follows: 1.The first line contains an integer, n, denoting the number of intervals in the list. Each line i of the n subsequent lines contains two space-separated integers describing the respective starting (ai) and ending (bi) boundaries of an interval. Constraints 1 <= s <= 100 2 <= n <= 1000 1 <= ai <= bi <= 10^9 Output Format For each of the s interval sets, print an integer denoting the size of the largest possible subset of intervals in the given set such that no three points in the subset overlap.
Solution :
Solution in C :
In C++ :
#include <algorithm>
#include <cstdio>
using namespace std;
pair<int,int> a[1002];
int main() {
int i,m,n,x,y,z,may;
for (scanf("%d",&z);z;--z) {
scanf("%d",&n);
for (i=1;i<=n;++i) {
scanf("%d%d",&a[i].second,&a[i].first);
}
sort(a+1,a+n+1);
a[x=y=m=0]=make_pair(-1,-1);
for (i=1;i<=n;++i) {
may=-1;
if (a[x].first<a[i].second) {
may=x;
}
if ((y>may) && (a[y].first<a[i].second)) {
may=y;
}
if (may==x) {
x=i;
++m;
}
else if (may==y) {
y=i;
++m;
}
}
printf("%d\n",m);
}
return 0;
}
In Java :
import java.util.*;
import java.io.*;
import static java.lang.Math.*;
public class Solution {
static class Foo53 {
void main() {
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine().trim());
for (int i = 0; i < T; i++) {
int N = Integer.parseInt(br.readLine().trim());
int[][] arr = new int[N][2];
for (int j = 0; j < N; j++) {
String[] s = br.readLine().trim().split("\\s+");
arr[j][0] = Integer.parseInt(s[0].trim());
arr[j][1] = Integer.parseInt(s[1].trim());
}
int res = foo(arr);
System.out.println(res);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (br != null) {
try { br.close(); } catch (Exception e) { e.printStackTrace(); }
}
}
}
Deque<Integer> deque;
int foo(int[][] arr) {
int res = 0;
int n = arr.length;
deque = new LinkedList<Integer>();
Arrays.sort(arr, new Comparator<int[]>() {
public int compare(int[] a, int[] b) {
return a[0] - b[0];
}
});
for (int[] a : arr) {
int left = a[0], right = a[1];
while (!deque.isEmpty() && deque.getFirst() < left)
deque.removeFirst();
if (deque.size() < 2) {
res++;
add(right);
} else {
if (deque.getLast() > right) {
deque.removeLast();
add(right);
}
}
}
return res;
}
void add(int val) {
if (!deque.isEmpty() && deque.getLast() > val) {
deque.addFirst(val);
} else {
deque.addLast(val);
}
}
}
public static void main(String[] args) {
Foo53 foo = new Foo53();
foo.main();
}
}
In C :
#include <stdio.h>
#include <stdlib.h>
long long v,zas[1100],d[3000][3],ii,jj,kk,s[10000][2];
long long aa[1100],a[1100][1100],i,j,k,l,m,n,t,tt;
int com(const void*x, const void *y)
{
long long *xx,*yy;
xx = (long long *)x;
yy = (long long *)y;
if(xx[0]>yy[0]) return 1;
if(xx[0]==yy[0] && xx[2]==1) return 1;
return -1;
}
int main()
{
scanf("%lld",&t);
for(tt=0;tt<t;tt++)
{
scanf("%lld",&n);
for(i=0;i<n;i++) scanf("%lld %lld", &s[i][0], &s[i][1]);
// s[0][0] = -5;
// s[0][1] = -1;
// for(i=0;i<n;i++)
// for(j=0;j<n;j++) a[i][j] =0;
for(i=0;i<n;i++)
{
d[2*i][0]=s[i][0];
d[2*i][1]=i;
d[2*i][2]=0;
d[2*i+1][0]=s[i][1];
d[2*i+1][1]=i;
d[2*i+1][2]=1;
}
qsort(d,n*2,sizeof(d[0]),com);
l=0;
m=0;
v=0;
for(i=0;i<2*n;i++)
{
ii = d[i][1];
if(d[i][2]==0)
{
for(j=0;j<l;j++)
{
a[ii][zas[j]] = a[zas[j]][ii] = aa[zas[j]]+1;
if(a[ii][zas[j]]>v) v = a[ii][zas[j]];
}
zas[l] = ii;
l++;
aa[ii] = m+1;
if(aa[ii]>v) v = aa[ii];
}
else
{
if(aa[ii]>m) m=aa[ii];
j=0;
while(zas[j]!=ii) j++;
l--;
zas[j] = zas[l];
for(j=0;j<l;j++)
if(a[ii][zas[j]] > aa[zas[j]])
aa[zas[j]] = a[ii][zas[j]];
}
}
printf("%lld\n",v);
//for(i=0;i<2*n;i++) printf("%lld %lld %lld\n",d[i][0],d[i][1],d[i][2]);
}
return 0;
}
In Python3 :
t = int(input())
for _ in range(t):
n = int(input())
I = []
for _ in range(n):
a, b = list(map(int, input().split()))
I.append((a, b))
I.sort(key = lambda x: (x[1], x[0]))
temp = -1
lasta = 0
lastb = 0
res = 0
for a, b in I:
if a>lastb:
res+=1
lasta = a
lastb = b
elif a>temp:
res+=1
temp = lastb
lasta = a
lastb = b
print(res)
View More Similar Problems
Kundu and Tree
Kundu is true tree lover. Tree is a connected graph having N vertices and N-1 edges. Today when he got a tree, he colored each edge with one of either red(r) or black(b) color. He is interested in knowing how many triplets(a,b,c) of vertices are there , such that, there is atleast one edge having red color on all the three paths i.e. from vertex a to b, vertex b to c and vertex c to a . Note that
View Solution →Super Maximum Cost Queries
Victoria has a tree, T , consisting of N nodes numbered from 1 to N. Each edge from node Ui to Vi in tree T has an integer weight, Wi. Let's define the cost, C, of a path from some node X to some other node Y as the maximum weight ( W ) for any edge in the unique path from node X to Y node . Victoria wants your help processing Q queries on tree T, where each query contains 2 integers, L and
View Solution →Contacts
We're going to make our own Contacts application! The application must perform two types of operations: 1 . add name, where name is a string denoting a contact name. This must store name as a new contact in the application. find partial, where partial is a string denoting a partial name to search the application for. It must count the number of contacts starting partial with and print the co
View Solution →No Prefix Set
There is a given list of strings where each string contains only lowercase letters from a - j, inclusive. The set of strings is said to be a GOOD SET if no string is a prefix of another string. In this case, print GOOD SET. Otherwise, print BAD SET on the first line followed by the string being checked. Note If two strings are identical, they are prefixes of each other. Function Descriptio
View Solution →Cube Summation
You are given a 3-D Matrix in which each block contains 0 initially. The first block is defined by the coordinate (1,1,1) and the last block is defined by the coordinate (N,N,N). There are two types of queries. UPDATE x y z W updates the value of block (x,y,z) to W. QUERY x1 y1 z1 x2 y2 z2 calculates the sum of the value of blocks whose x coordinate is between x1 and x2 (inclusive), y coor
View Solution →Direct Connections
Enter-View ( EV ) is a linear, street-like country. By linear, we mean all the cities of the country are placed on a single straight line - the x -axis. Thus every city's position can be defined by a single coordinate, xi, the distance from the left borderline of the country. You can treat all cities as single points. Unfortunately, the dictator of telecommunication of EV (Mr. S. Treat Jr.) do
View Solution →