跳到正文

题解

CF22D 题解

Vitamin_B约 3 分钟
原文
文章信息与版本

文章信息

发布
2024年6月20日
收录
2025年12月12日
更新
2026年11月9日
篇幅
约 3 分钟
题目
CF22D

版本

  1. v1当前

思路

首先在看这题前你得学会差分约束,类似 P1250。

先建立几个不等式(axa_x 表示 1∼x1\sim x 中最少有几个点):

  1. ax+1−ax≥0a_{x+1}-a_x\ge0(相邻的点最少不加点);
  2. ax−ax+1≥−1a_x-a_{x+1}\ge-1(相邻的点里最多 11 个点);
  3. ar−al−1≥1a_r-a_{l-1}\ge1(线段里至少 11 个点)。

就建这么几条边跑一个 SPFA 就行啦!

注意事项

首先可能 l>rl>r,建边前要先交换。

然后 l,rl,r 可能是负数,可以先在建边时 +10001+10001,输出时减掉。

代码

C++
# include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
int n, m, x, y, dis[20005];
vector <pii> v[20005];
bitset <20005> vis;
queue <int> q;
int main () {
	ios::sync_with_stdio (0);
	cin.tie (0);
	cout.tie (0);
	cin >> m;
	while (m --) {
		cin >> x >> y;
		if (x > y)
			swap (x, y);
		x += 10001, y += 10001;
		v[x - 1].push_back ({y, 1});
		n = max (n, y);
	}
	for (int i = 0; i < n; ++ i)
		v[i].push_back ({i + 1, 0}), v[i + 1].push_back ({i, -1});
	fill (dis + 1, dis + 1 + n, -1);
	q.push (0);
	while (! q.empty ()) {
		x = q.front ();
		q.pop ();
		vis[x] = 0;
		for (pii& i : v[x])
			if (i.second + dis[x] > dis[i.first]) {
				dis[i.first] = dis[x] + i.second;
				if (! vis[i.first])
					q.push (i.first), vis[i.first] = 1;
			}
	}
	cout << dis[n] << '\n';
	for (int i = 1; i <= n; ++ i)
		if (dis[i] > dis[i - 1])
			cout << i - 10001 << ' ';
	return 0;
}
原文