Блог пользователя Tahlil

Автор Tahlil, 13 лет назад, По-английски

You will be given several intervals. Every input of an interval consists of 3 numbers ai,bi,ci where a and b are the starting and ending of the interval. From all the intervals you have to find a set of integers Z which have ci number of common integers between ai and bi . You have to find out the minimum size of Z. 


Input:

6 8 3
8 10 3
1 3 1

output : 6


I saw this problem in many sites. But could not solve it. I tried but could not find any solution. Can anyone please explain how to solve this. Thanks very much :)


EDIT : The link of the problem is here http://acm.tju.edu.cn/toj/showp1664.html
  • Проголосовать: нравится
  • -4
  • Проголосовать: не нравится

13 лет назад, # |
  Проголосовать: нравится +5 Проголосовать: не нравится
sort intervel by bi and look how many number are not enough and add number as large number for each interval.

1 3 1 - not enough 1, add 3
6 8 3 - not enough 3, add 8, 7, 6
8 10 3, - not enough 2, add 10, 9

answer |{3,6,7,8,9,10}| = 6


  • 13 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится
    Thanks very much. So I need to search in log(n) time to find if there is enough number for each interval because a,b,c each can be 50000. I will try it now.
    Thanks again :)
  • 13 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится
    What if the input is like this,

    2
    5 6 2
    3 8 6

    • 13 лет назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится
      sort by bi
      for 5 6 2 not enough two number, is 6 and 5
      for 3 8 6 not enough four number, is 8,7,4,3
      answer 6
      • 13 лет назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится
        But the problem is how to search how many numbers are already taken from an interval. I used binary search because I could store the numbers as sorted without any sort operation for cases unlike that. But cases like this make storing numbers as sorted impossible without doing a sort operation. So I can't Binary search now. What should I do ? 

        Thanks very much for your  reply.
13 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится
Nice problem!
I used following idea to solve this:
Intervals are like:[a,b,c]. Our resulting integer set-Z. Set of intervals-S.
While S is not empty{
  Choose the interval with minimum b value(ending of the interval).If there are several possible choices,choose any of them.Let denote this interval as I.In our resulting set Z,we must have at least c(I) numbers between a(I) and b(I).Let K denote the current count of numbers we already chose from a(I) to b(I).Then we must choose additional c(I)-K numbers from interval I.(If c(I)>K of course).Choose these numbers starting from right of the interval(iterate from right to left).If there is a number which we didn't use in our resulting set Z,add this number to the Z and continue until c(I)-K numbers added.Delete interval I and continue.
Adding from right of the interval is the key of the solution.Because we choose the interval I with minimum b time,other intervals in the set S\{b} have ending time>=b.
}
For above approach sorting of the intervals according to b value must be done.
After that , binary indexed tree can be used to check c(I)>K within one interval.
Of course,we must think additional quick operations to choose c(I)-K numbers from right side of the interval....
  • 13 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится
    I used your approach but instead of BIT I used binary search. But giving me WA :(
13 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится
you can use the system of system of difference constraints to solve it.