abc 216 G.01Sequence 【差分约束 | SLF优化 | 双端队列deque】

G - 01Sequence (atcoder.jp)

给定长度为 n 的 01 序列 ar,m 组限制,为 [l, r, x],问能否满足所有的
i, 使得xi <= sum[r] - sum[l - 1]

优化:

  • 如果有超级源点,直接把所有点push进去。
  • deque的slf做法,一般出题人只卡spfa或slf
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N = 2e5 + 10, M = 2e6 + 10;
int h[N], ne[M], e[M], idx, w[M];
int n, m;
void add(int a, int b, int c)
{
	e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
}
int dis[N];
bool st[N];
void spfa()
{
	deque<int> q;
	for (int i = 0; i <= n; i ++ ) {
		st[i] = 1;
		q.push_back(i);
	}
	while(q.size())
	{
		int u = q.front();q.pop_front();
		st[u] = 0;
		for (int i = h[u]; ~ i; i = ne[i])
		{
			int j = e[i];
			if(dis[j] < dis[u] + w[i]) {
				dis[j] = dis[u] + w[i];
				if(!st[j]) {
					if(!q.empty() && dis[j] > dis[q.front()]) q.push_back(j);
					else q.push_front(j);
					st[j] = 1;
				}
			}
		}
	}
}


int main(){
	//std::ios::sync_with_stdio(false);
    //std::cin.tie(nullptr);
    memset(h, -1, sizeof h);
    cin >> n;
    for (int i = 1; i <= n; i ++ ) {
    	add(i - 1, i, 0);
    	add(i, i - 1, -1);
    }
    cin >> m;
    while(m -- ) 
    {
    	int a, b, c;
    	cin >> a >> b >> c;
    	add(a - 1, b, c);
    }

    spfa();
    for (int i = 1; i <= n; i ++ ) {
    	if(dis[i] - dis[i - 1] == 1) cout << "1";
    	else cout << "0";
    	cout << ' ';
    }
    cout << '\n';
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值