传送门
题意
分析
这个题,太妙了,太妙了
首先我们去转化一下给的式子
i
n
i
1
+
(
i
n
i
2
−
o
u
t
i
1
)
+
⋅
⋅
⋅
+
(
i
n
i
k
−
o
u
t
i
k
−
1
)
in_{i1} + (in_{i2} - out_{i1}) + ··· + (in_{ik} - out_{ik - 1})
ini1+(ini2−outi1)+⋅⋅⋅+(inik−outik−1)
等价于
i
n
i
k
+
∑
p
=
1
k
−
1
(
i
n
i
p
−
o
u
t
i
p
)
in_{ik} + \sum\nolimits_{p = 1}^{k - 1}{(in_{ip} - out_{ip})}
inik+∑p=1k−1(inip−outip)
题目要求的是求这个的最小值,所以,等价于
∑
p
=
1
k
−
1
(
i
n
i
p
−
o
u
t
i
p
)
\sum\nolimits_{p = 1}^{k - 1}{(in_{ip} - out_{ip})}
∑p=1k−1(inip−outip)最小
我们把所有点按照
i
n
i
in_{i}
ini从小到大排序,设置点0,然后相邻两个点之间连边,权重为
i
n
i
−
i
n
i
−
1
in_{i} - in{i - 1}
ini−ini−1,这样,
1
−
i
1 - i
1−i之间的的路径和就是
i
n
i
−
i
n
i
−
1
+
i
n
i
−
1
−
i
n
i
−
2
.
.
.
+
i
n
1
−
i
n
0
in_{i} - in_{i - 1} + in_{i -1} - in_{i - 2} ... + in_{1} - in_{0}
ini−ini−1+ini−1−ini−2...+in1−in0 ,最后等价于
i
n
i
in_{i}
ini,也就是
i
n
i
k
in_{ik}
inik
那么后面那一坨我们怎么处理呢,我们可以把每个点
i
i
i后面第一个满足
i
n
j
>
o
u
t
i
in_{j} > out_{i}
inj>outi的点找到,然后
i
−
j
i - j
i−j之间连一条边,权值为
i
n
j
−
o
u
t
i
in_{j} - out_{i}
inj−outi,这样,
∑
p
=
1
k
−
1
(
i
n
i
p
−
o
u
t
i
p
)
\sum\nolimits_{p = 1}^{k - 1}{(in_{ip} - out_{ip})}
∑p=1k−1(inip−outip)也处理出来了
剩下的就是跑最短路了,并且因为要求方案数,所以还要最短路计数
因为这张图内存在边权为0的边,所有
d
i
j
k
s
t
r
a
dijkstra
dijkstra和
s
p
f
a
spfa
spfa都无法处理这个问题,但因为是拓扑图,所以我们可以顺着推
最后需要注意的是,如果
i
i
i点的
o
u
t
out
out小于
n
n
n点的
i
n
in
in的话,说明还可以套娃,说明当前答案不符合要求
代码
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define dl(x) printf("%lld\n",x);
#define di(x) printf("%d\n",x);
#define _CRT_SECURE_NO_WARNINGS
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef vector<int> VI;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int N = 2e5 + 10,M = N << 1;
const ll mod = 1000000007;
const double eps = 1e-9;
const double PI = acos(-1);
template<typename T>inline void read(T &a) {
char c = getchar(); T x = 0, f = 1; while (!isdigit(c)) {if (c == '-')f = -1; c = getchar();}
while (isdigit(c)) {x = (x << 1) + (x << 3) + c - '0'; c = getchar();} a = f * x;
}
int gcd(int a, int b) {return (b > 0) ? gcd(b, a % b) : a;}
struct Node{
int out,in;
bool operator <(Node const &A) const{
return in < A.in;
}
}a[N];
int n;
int h[N],ne[M],e[M],w[M],idx;
ll d[N];
bool st[N];
ll f[N];
void add(int x,int y,int z){
ne[idx] = h[x],e[idx] = y,w[idx] = z,h[x] = idx++;
}
int check(int x){
int l = 1,r = n;
while(l < r){
int mid = (l + r) >> 1;
if(a[mid].in >= x) r = mid;
else l = mid + 1;
}
if(a[l].in >= x) return l;
return -1;
}
void spfa(){
memset(d,0x3f,sizeof d);
d[0] = 0;
f[0] = 1;
for(int i = 0;i <= n;i++)
for(int j = h[i];~j;j = ne[j]){
int v = e[j];
if(d[v] > d[i] + w[j]){
d[v] = d[i] + w[j];
f[v] = f[i];
}
else if(d[v] == d[i] + w[j]) f[v] = (f[v] + f[i]) % mod;
}
}
int main() {
memset(h,-1,sizeof h);
read(n);
for(int i = 1;i <= n;i++) read(a[i].out),read(a[i].in);
sort(a + 1,a + 1 + n);
for(int i = 0;i < n;i++) add(i,i + 1,a[i + 1].in - a[i].in);
for(int i = 1;i <= n;i++){
int p = check(a[i].out);
if(p == -1) continue;
add(i,p,a[p].in - a[i].out);
}
spfa();
ll ans = 0,res = INF;
for(int i = 1;i <= n;i++)
if(a[i].out > a[n].in) res = min(res,d[i]);
for(int i = 1;i <= n;i++)
if(a[i].out > a[n].in && res == d[i])
ans = (ans + f[i]) % mod;
dl(ans);
return 0;
}