gpt4 book ai didi

algorithm - 以下问题的 O(N*LogN) 算法

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:28:44 24 4
gpt4 key购买 nike

存在以下问题:

The most prestigious sports club in one city has exactly N members. Each of its members is strong and beautiful. More precisely, i-th member of this club (members being numbered by the time they entered the club) has strength Si and beauty Bi. Since this is a very prestigious club, its members are very rich and therefore extraordinary people, so they often extremely hate each other. Strictly speaking, i-th member of the club Mr X hates j-th member of the club Mr Y if Si ≤ Sj and Bi ≥ Bj or if Si ≥ Sj and Bi ≤ Bj (if both properties of Mr X are greater then corresponding properties of Mr Y, he doesn't even notice him, on the other hand, if both of his properties are less, he respects Mr Y very much).

To celebrate a new 2003 year, the administration of the club is planning to organize a party. However they are afraid that if two people who hate each other would simultaneouly attend the party, after a drink or two they would start a fight. So no two people who hate each other should be invited. On the other hand, to keep the club prestige at the apropriate level, administration wants to invite as many people as possible.

Being the only one among administration who is not afraid of touching a computer, you are to write a program which would find out whom to invite to the party.

Input

*The first line of the input file contains integer N — the number of members of the club. ( 2 ≤ N ≤ 100,000 ). Next N lines contain two numbers each — Si and Bi respectively ( 1 ≤ Si, Bi ≤ 109 ).*

Output

On the first line of the output file print the maximum number of the people that can be invited to the party. On the second line output N integers — numbers of members to be invited in arbitrary order. If several solutions exist, output any one.

Sample test(s)

Input

  4
1 1
1 2
2 1
2 2

Output

  2
1 4

我正在尝试解决一个问题,但我的算法的复杂度为 O(N^2),并且由于 2<=N<=100000,因此需要改进算法。我正在使用具有 O(N^2) 复杂度的最长递增子序列动态规划算法来解决问题。有人知道如何改进算法吗?

最佳答案

我认为您的 O(n^2) 解决方案甚至都不正确,更不用说高效了。如果你有这样的事情:

3
2 2
1 1
3 3

答案是 3。然而,经典的 LIS 算法会给出 2。你考虑过这个吗?

您可以做的是按 Si 排序,并在 O(n log n) 时间内在 Bi 上应用 LIS。为此,您可以使用线段树或涉及二进制搜索的更简单的算法。如果您需要更多帮助,请告诉我。

总的复杂度是O(n log n):此时可以完成排序,LIS也可以。

关于algorithm - 以下问题的 O(N*LogN) 算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5912253/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com