Acwing 362.区间(差分约束)

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} aixbi 的整数 x x x 不少于 c i c_{i} ci 个。 求这样的整数集合 Z Z Z 最少包含多少个数。

数据范围

1 ≤ n ≤ 50000 1 \leq n \leq 50000 1n50000
0 ≤ a i , b i ≤ 50000 0 \leq a_{i}, b_{i} \leq 50000 0ai,bi50000
1 ≤ c i ≤ b i − a i + 1 1 \leq c_{i} \leq b_{i}-a_{i}+1 1cibiai+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} xixi+1

x i + 1 − x i ≤ 1 x_{i + 1} - x_i \leq 1 xi+1xi1

x b i − x a i − 1 > = c i x_{bi} - x_{ai - 1} >= c_i xbixai1>=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+1xi

x i ≥ x i + 1 − 1 x_i \geq x_{i + 1} - 1 xixi+11

x b i ≥ x a i − 1 + c i x_{bi} \geq x_{ai - 1} + c_i xbixai1+ci

因为这些不等式中只有相对关系没有绝对关系,所以增加④ x i ≥ x 0 + 0 x_i \geq x_0 + 0 xix0+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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zzqwtc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值