Quantcast
Channel: CodeChef Discuss - latest questions
Viewing all articles
Browse latest Browse all 39796

ONEKING - Editorial

$
0
0

PROBLEM LINK:

Practice
Contest

Author:Snigdha Chanda
Tester:Shiplu Hawlader
Editorialist:Lalit Kundu

DIFFICULTY:

EASY-MEDIUM

PRE-REQUISITES:

Greedy

PROBLEM:

Given N (≤100000) intervals [Ai, Bi], one such interval can be deleted by placing a bomb at x if Ai≤ x ≤ Bi. Find minimum number of bombs required to delete all intervals.

EXPLANATION:

First we sort all intervals according to increasing Bi.
Now, let's say we have placed a bomb at position x on the line. All intervals such that their starting point Ai≤ x will get destroyed. So we'll greedily place the bombs when required.

Pseudo code:

n,a,b = input
ar=[]    //ar[i] denotes maximum starting point for intervals ending at i

for i=1 to N:
    ar[b[i]]=max(ar[b[i]], a[i])

ans=0
max=-1  //denotes the latest value of x where we placed a bomb

for i=0 to 2000:
    //we need a bomb to all intervals ending at i'th position
    if max < ar[i]:
        ans += 1
        max = i
print ans

Complexity: O(N+2000).

SOLUTIONS:

Setter's solution
Tester's solution


Viewing all articles
Browse latest Browse all 39796

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>