【题目链接】
There are famous Russian nesting dolls named matryoshkas sold in one of the souvenir stores nearby, and you’d like to buy several of them. The store has n different matryoshkas. Any matryoshka is a figure of volume outi with an empty space inside of volume ini (of course, outi>ini).
You don’t have much free space inside your bag, but, fortunately, you know that matryoshkas can be nested one inside another. Formally, let’s call a set of matryoshkas nested if we can rearrange dolls in such a way, that the first doll can be nested inside the second one, the second doll — inside the third one and so on. Matryoshka i can be nested inside matryoshka j if outi≤inj. So only the last doll will take space inside your bag.
Let’s call extra space of a nested set of dolls as a total volume of empty space inside this structure. Obviously, it’s equal to ini1+(ini2−outi1)+(ini3−outi2)+⋯+(inik−outik−1), where i1, i2, …, ik are the indices of the chosen dolls in the order they are nested in each other.
Finally, let’s call a nested subset of the given sequence as big enough if there isn’t any doll from the sequence that can be added to the nested subset without breaking its nested property.
You want to buy many matryoshkas, so you should choose a big enough nested subset to buy it. But you will be disappointed if too much space in your bag will be wasted, so you want to choose a big enough subset so that its extra space is minimum possible among all big enough subsets. Now you wonder, how many different nested subsets meet these conditions (they are big enough, and there is no big enough subset such that its extra space is less than the extra space of the chosen subset). Two subsets are considered different if there exists at least one index i such that one of the subsets contains the i-th doll, and another subset doesn’t.
Since the answer can be large, print it modulo 1e9+7.
The first line contains a single integer n (1≤n≤2e5) — the number of matryoshkas.
The next n lines contain a description of each doll: two integers outi and ini (1≤ini Print one integer — the number of big enough nested subsets such that extra space of each of these subsets is minimum possible. Since the answer can be large, print it modulo 109+7. input output Note There are 6 big enough nested subsets with minimum possible extra space in the example: {1,5}: we can’t add any other matryoshka and keep it nested; it’s extra space is 1; 俄罗斯套娃,每个套娃有一个in,一个out,in表示里面空腔的大小,out表示整体大小,(out-in)就是套娃皮的厚度,out[i]<=in[j]时i可以放进j,给n个套娃,问满足条件的组合有多少种。 思路Output
Example
7
4 1
4 2
4 2
2 1
5 4
6 4
3 2
6
{1,6};
{2,4,5};
{2,4,6};
{3,4,5};
{3,4,6}.
There are no more “good” subsets because, for example, subset {6,7} is not big enough (we can add the 4-th matryoshka to it) or subset {4,6,7} has extra space equal to 2.
题目大意与思路
条件1:是内外嵌套的一套;
条件2:足够大,这一套不能放进其他套娃里,也就是这一套最外层的out要大于所有的in;
条件3:空气的体积之和最小。
Input
第一行n,范围[1,2e5]
n行,每行out[i] int[i] ,范围[1,1e9]
output
一个数字表示种类数,对1e9+7取模。
按in排列后从大往小取,新加一个较小的套娃在当前的一套里就是当前空气体积减去新套娃皮的厚度。
dp[i].v表示第[i,n]个组成的一套套娃的最小空气体积,
dp[i].num表示在上一行的情况下的种类数。
更新:
if(第i个套娃足够大,满足条件2)dp[i]=(in[i], 1);
else
对于所有( j > i && in[j] >= out[i] ),比较dp[i]和dp[j].v-out[i]+in[i]进行更新。
以上是O(n^2)的做法。
else部分可以通过线段树来O(logn)地更新,首先因为按in排序,所以符合( j > i && in[j] >= out[i] )必定是某个pos之后连续的一段,只需求该段所有dp[j].v最小的点的dp[j].num的和。
于是定义线段树上的合并操作为,v不同时取v小的一方,v相同时取num的和。
先二分找到pos,再用线段树查询[pos,n]。
官方题解用了一个空间大小为2n的线段树,[0,n-1]是套娃的下标,套娃[i]的值保存在节点[i+n],父节点[i]的子节点为[2*i]和[2*i+1], 根节点为1。
AC代码
#include