P1645 序列

Portal.

差分约束。

限制条件可以转化成不等关系。设 s i s_i si 表示 i i i 的前缀和,显然有 s R − s L − 1 ≥ C s_R-s_{L-1}\geq C sR−sL−1≥C。又因为每个相邻的前缀和至多差 1 1 1 至少差 0 0 0,即 0 ≤ s R − s R − 1 ≤ 1 0\leq s_R-s_{R-1}\leq 1 0≤sR−sR−1≤1,所以有总式:
{ s R − s L − 1 ≥ C s R − s R − 1 ≥ − 1 s R − s R − 1 ≥ 0 \begin{cases} s_R-s_{L-1}\geq C\\ s_R-s_{R-1}\geq -1\\ s_R-s_{R-1}\geq 0 \end{cases} ⎩ ⎨ ⎧sR−sL−1≥CsR−sR−1≥−1sR−sR−1≥0

直接差分约束跑最长路即可。

注意最后的终点不是 N N N,而是 R max ⁡ R_{\max} Rmax。

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
#define int long long

const int maxn=3e4+5;
int head[maxn],cnt,dis[maxn];
struct edge{int to,nxt,w;}e[maxn];
bool vis[maxn];

void add(int x,int y,int z){e[++cnt]={y,head[x],z},head[x]=cnt;}

void spfa(int s)
{
	queue<int> q;
	q.push(s),dis[s]=0,vis[s]=1;
	while(!q.empty())
	{
		int x=q.front();q.pop();vis[x]=0;
		for(int i=head[x];i;i=e[i].nxt)
			if(dis[e[i].to]<dis[x]+e[i].w)
			{
				dis[e[i].to]=dis[x]+e[i].w;
				if(!vis[e[i].to]) q.push(e[i].to),vis[e[i].to]=1;
			}
	}
}

signed main()
{
	int N;cin>>N;
	int mxr=N;
	for(int i=1,L,R,C;i<=N;i++) cin>>L>>R>>C,add(L-1,R,C),mxr=max(mxr,R);
	for(int i=1;i<=mxr;i++) add(i,i-1,-1),add(i-1,i,0);
	for(int i=1;i<=mxr;i++) dis[i]=-INT_MAX;
	spfa(0);
	cout<<dis[mxr];
	return 0;
}
相关推荐
闻缺陷则喜何志丹1 天前
【图论】P9661 [ICPC 2021 Macao R] Sandpile on Clique|普及+
c++·算法·图论·洛谷
拼好饭和她皆失1 天前
《图论算法入门:掌握DFS和BFS,理解图与树的遍历》
深度优先·图论·宽度优先
罗湖老棍子2 天前
强迫症冒险家的任务清单:字典序最小拓扑排序
数据结构·算法·图论·拓扑排序
燃于AC之乐3 天前
我的算法修炼之路--8——预处理、滑窗优化、前缀和哈希同余,线性dp,图+并查集与逆向图
算法·哈希算法·图论·滑动窗口·哈希表·线性dp
2401_827499993 天前
代码随想录-图论28
算法·深度优先·图论
ValhallaCoder3 天前
Day51-图论
数据结构·python·算法·图论
ValhallaCoder4 天前
Day53-图论
数据结构·python·算法·图论
(❁´◡`❁)Jimmy(❁´◡`❁)5 天前
Atcoder abc441A~F 题解
算法·深度优先·图论
ValhallaCoder5 天前
Day50-图论
数据结构·python·算法·图论
ValhallaCoder5 天前
Day49-图论
数据结构·python·算法·图论