Acwing 362.区间
题意
给定
n
n
n 个区间
[
a
i
,
b
i
]
\left[a_{i}, b_{i}\right]
[ai,bi] 和
n
n
n 个整数
c
i
。
c_{i \text { 。 }}
ci 。
你需要构造一个整数集合
Z
Z
Z, 使得
∀
i
∈
[
1
,
n
]
,
Z
\forall i \in[1, n], Z
∀i∈[1,n],Z 中满足
a
i
≤
x
≤
b
i
a_{i} \leq x \leq b_{i}
ai≤x≤bi 的整数
x
x
x 不少于
c
i
c_{i}
ci 个。 求这样的整数集合
Z
Z
Z 最少包含多少个数。
数据范围
1
≤
n
≤
50000
1 \leq n \leq 50000
1≤n≤50000
0
≤
a
i
,
b
i
≤
50000
0 \leq a_{i}, b_{i} \leq 50000
0≤ai,bi≤50000
1
≤
c
i
≤
b
i
−
a
i
+
1
1 \leq c_{i} \leq b_{i}-a_{i}+1
1≤ci≤bi−ai+1
思路
设 x i x_i xi 表示前 i i i 个数中选择了 x i x_i xi个数,那么存在以下不等式关系
① x i ≥ x i + 1 x_i \geq x_{i + 1} xi≥xi+1
② x i + 1 − x i ≤ 1 x_{i + 1} - x_i \leq 1 xi+1−xi≤1
③ x b i − x a i − 1 > = c i x_{bi} - x_{ai - 1} >= c_i xbi−xai−1>=ci
因为 a , b a,b a,b 都是从 0 开始的,不方便使用前缀和,所以将所有 a , b a,b a,b 整体右移 1 1 1 从 1 1 1 开始
因为要求 Z Z Z 中最少包含多少个数,所以应该用差分约束的最长路模型。上述不等式转换如下:
① x i + 1 ≥ x i x_{i + 1} \geq x_{i} xi+1≥xi
② x i ≥ x i + 1 − 1 x_i \geq x_{i + 1} - 1 xi≥xi+1−1
③ x b i ≥ x a i − 1 + c i x_{bi} \geq x_{ai - 1} + c_i xbi≥xai−1+ci
因为这些不等式中只有相对关系没有绝对关系,所以增加④ x i ≥ x 0 + 0 x_i \geq x_0 + 0 xi≥x0+0,其中 x 0 x_0 x0 = = = 0 0 0
代码
// Author:zzqwtcc
// Problem: 区间
// Contest: AcWing
// Time:2021-10-28 22:50:45
// URL: https://www.acwing.com/problem/content/364/
// Memory Limit: 64 MB
// Time Limit: 1000 ms
#include<bits/stdc++.h>
#include<unordered_map>
// #define int long long
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3f
#define mod 1000000007
#define MOD 998244353
#define rep(i, st, ed) for (int (i) = (st); (i) <= (ed);++(i))
#define pre(i, ed, st) for (int (i) = (ed); (i) >= (st);--(i))
// #define debug(x,y) cerr << (x) << " == " << (y) << endl;
#define endl '\n'
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
template<typename T> inline T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template<typename T> inline T lowbit(T x) { return x & -x; }
template<typename S,typename T>void debug(S s, T t){cerr << s << " == " << t << endl;}
template<typename T>void debug(T t){cerr << t << endl;}
template<typename T>void debug(T t[],int st,int ed){for(int i = st; i <=ed;++i){cerr << t[i] << " ";}cerr << endl;}
template<typename T>void debug(const vector<T>&t){for(int i =0 ; i < t.size();++i)cerr << t[i] << " ";cerr << endl;}
// template<typename T> T qmi(T a, T b = mod - 2, T p = mod) { T res = 1; b %= (p - 1 == 0 ? p : p - 1); while (b) { if (b & 1) { res = (LL)res * a % p; }b >>= 1; a = (LL)a * a % p; }return res % mod; }
const int N = 5e4 + 10, M = 4 * N;
int h[N],e[M],ne[M],w[M],idx;
int d[N], cnt[N];
bool st[N];
int n;
void add(int a,int b, int c){
e[idx] = b,w[idx] = c,ne[idx] = h[a],h[a] = idx++;
}
bool spfa(){
memset(d,-0x3f,sizeof d);
stack<int>q;
d[0] = 0;
q.push(0);
while(q.size()){
int u = q.top();
q.pop();
st[u] = false;
for(int i =h[u];~i;i = ne[i]){
int j = e[i];
int dis = w[i];
if(d[j] < d[u] + dis){
d[j] = d[u] + dis;
cnt[j] = cnt[u] + 1;
if(cnt[j] >= 50001)return false;
if(!st[j]){
st[j] = true;
q.push(j);
}
}
}
}
return true;
}
void solve() {
cin >> n;
memset(h,-1,sizeof h);
while(n--){
int a,b,c;scanf("%d%d%d",&a,&b,&c);
add(a,b + 1,c);
}
for(int i = 1;i <= 50001;++i){
add(0,i,0);
}
for(int i = 1; i <= 50001;++i){
add(i - 1,i,0);
}
for(int i = 1; i <= 50001;++i){
add(i,i - 1,-1);
}
spfa();
cout << d[50001] << endl;
}
signed main() {
//int _; cin >> _;
//while (_--)
solve();
return 0;
}