排完序之后dp。
dp[ i ] 表示 i 作为最里面一层的最小花费。
way[ i ] 表示得到最小花费的方案数。
然后用Map维护最小值就好了。
//#pragma GCC optimize(2) //#pragma GCC optimize(3) //#pragma GCC optimize(4) #include<bits/stdc++.h> #define LL long long #define LD long double #define ull unsigned long long #define fi first #define se second #define mk make_pair #define PLL pair<LL, LL> #define PLI pair<LL, int> #define PII pair<int, int> #define SZ(x) ((int)x.size()) #define ALL(x) (x).begin(), (x).end() #define fio ios::sync_with_stdio(false); cin.tie(0); using namespace std; const int N = 5e5 + 7; const int inf = 0x3f3f3f3f; const LL INF = 0x3f3f3f3f3f3f3f3f; const int mod = 1e9 + 7; const double eps = 1e-8; const double PI = acos(-1); template<class T, class S> inline void add(T &a, S b) {a += b; if(a >= mod) a -= mod;} template<class T, class S> inline void sub(T &a, S b) {a -= b; if(a < 0) a += mod;} template<class T, class S> inline bool chkmax(T &a, S b) {return a < b ? a = b, true : false;} template<class T, class S> inline bool chkmin(T &a, S b) {return a > b ? a = b, true : false;} mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int n; int dp[N]; int way[N]; PII a[N]; priority_queue<pair<PII, PII>> q1; map<int, int> Map; int main() { scanf("%d", &n); for(int i = 1; i <= n; i++) { scanf("%d%d", &a[i].fi, &a[i].se); } sort(a + 1, a + 1 + n); reverse(a + 1, a + 1 + n); int maxIn = -inf; for(int i = 1; i <= n; i++) { int out = a[i].fi, in = a[i].se; if(maxIn < out) { dp[i] = 0; way[i] = 1; } else { while(SZ(q1) && q1.top().fi.fi >= out) { add(Map[q1.top().se.fi], q1.top().se.se); q1.pop(); } assert(SZ(Map)); auto t = *Map.begin(); dp[i] = t.fi - out; way[i] = t.se; } chkmax(maxIn, in); q1.push(mk(mk(in, out), mk(dp[i] + in, way[i]))); } int ans = inf; for(int i = 1; i <= n; i++) { dp[i] += a[i].se; chkmin(ans, dp[i]); } int ret = 0; for(int i = 1; i <= n; i++) { if(dp[i] == ans) { add(ret, way[i]); } } printf("%d ", ret); return 0; } /* */