跳到正文

题解

CF2147I 题解

EricWan约 11 分钟
原文
文章信息与版本

文章信息

发布
2025年9月22日
收录
2026年9月17日
更新
2026年11月16日
篇幅
约 11 分钟
获赞
3
题目
CF2147I2

版本

  1. v1当前

水题!

速度太慢没有场切。

可能这篇题解写得不太好懂,我语文水平太差了。


官解看不懂,但是手玩一下发现我们有一种类似 x y x-1 y+1 x-2 y+2... 这样的密集的两段的“配对”,于是我们思考,假如我们允许自由使用若干长度为 BB 的连续段(假设块数为 kk),我们就可以把两两段之间“配对”,如果块直接的距离没有重复的,我们就可以一下子获得 k×(k−1)×Bk\times(k-1)\times B 的答案数组长度。

如何选这些块的位置,使得方便处理和移动到下一对间隔略大的一对块?我的思路是,相邻块的间隔是块长的倍数,且间隔大小指数级增长。我构造第 ii 段为 [(3×2i−1−3)×B,(3×2i−1−2)×B−1][(3\times2^{i-1}-3)\times B,(3\times2^{i-1}-2)\times B-1],供参考。我的构造有这样一个好处,块对按间隔从小到大排后为 (1,2),(2,3),(1,3),(3,4),(2,4),(1,4),(4,5),…(1,2),(2,3),(1,3),(3,4),(2,4),(1,4),(4,5),\dots,我们每次“配对”完必定会出现在左块的左端点上,我们发现,在这个位置暴力向右挪动两次尽量小的长度后挪动回右块的左端点之后就可以直接进入下一步的“配对”,这个需要画图理解一下。这也是我的这个块的位置的构造的最巧妙的地方。当需要让块对的右块改变的时候,我们一直向右挪,直到不得不一步到达新的右块的左端点时停下即可。

于是我们的流程就比较清晰了,先把前两个块配对,然后依次让右块右移并从大到小扫左块进行配对。

没理解的话就看代码主函数吧,我已经尽力讲明白了。


复杂度分析:这里分析的是 mm 关于 n=100000n=100000 或 n=300000n=300000 和值域 V=2×1018+1V=2\times10^{18}+1 的关系。定义 BB 为块长,kk 为块数。首先,我们有我们的值域限制,导致 2k×B≤V22^{k}\times B\le\frac V2。其次,我们有我们的 nn 的限制,也就是 k×(k−1)×(B+1)≥nk\times(k-1)\times(B+1)\ge n。最后,我们的 m=k×B+k×k×2m=k\times B+k\times k\times 2 左右。(粗略计算,若式子中的系数不对请指出,如果有较小的项忘记算了,那就忽略吧。)

于是我们的 kk 越大越好(因为在有关 nn 的限制中 kk 带个平方),则 kk 取 5050(尽量顶着值域的限制,再留一点点给 BB)时算出 BB 大约在 150150 左右,于是写完代码就过了。

经尝试,B=113B=113 时在 I2 中获得我这个算法的最优秀的 mm(大概是吧),为 71827182,远小于限制。B=40B=40 时在 I1 中获得我这个算法的最优秀的 mm(大概是吧),为 32223222,远小于限制。

代码

请先阅读主函数,之后可以阅读 100 行后的那些辅助函数。

C++
// Problem: I1. Longest Increasing Path (Easy Version)
// Contest: Codeforces - Codeforces Global Round 29 (Div. 1 + Div. 2)
// URL: https://codeforces.com/contest/2147/problem/I1
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
using namespace std;
template<size_t buffer_size = 65536>
struct fast_writer_t {
	// short, int, long, long long, __int128
	// unsigned short, int, long, long long, __int128
	size_t cursor;
	unsigned char buffer[buffer_size];
	inline fast_writer_t() : cursor(0) {}
	inline ~fast_writer_t() {
		if (cursor) {
			flush();
		}
	}
	inline void flush() {
		__builtin_fwrite(buffer, 1, cursor, stdout);
		cursor = 0;
	}
	inline void put(unsigned char c) {
		buffer[cursor++] = c;
		#ifndef fast_os_buffer_enough
		if (cursor == buffer_size) {
			flush();
		}
		#endif
	}
	template<typename T>
	typename enable_if<is_integral<T>::value
					&& is_unsigned<T>::value
					&& !is_same<unsigned char, T>::value,
					fast_writer_t&>::type
	inline operator<<(const T &x) {
		if (x >= T(10)) {
			*this << x / T(10);
		}
		put('0' ^ x % T(10));
		return *this;
	}
	template<typename T>
	typename enable_if<is_integral<T>::value
					&& is_signed<T>::value
					&& !is_same<char, T>::value,
					fast_writer_t&>::type
	inline operator<<(const T &x) {
		if (x >= T(0)) {
			return *this << (typename make_unsigned<T>::type)x;
		} else {
			put('-');
			return *this << (typename make_unsigned<T>::type)(-x);
		}
	}
	inline fast_writer_t &operator<<(char x) {
		put(x);
		return *this;
	}
	inline fast_writer_t &operator<<(unsigned char x) {
		put(x);
		return *this;
	}
	inline fast_writer_t &operator<<(const string &x) {
		for (string::const_iterator it = x.cbegin(); it != x.cend(); it++) {
			put(*it);
		}
		return *this;
	}
	inline fast_writer_t &operator<<(const char *x) {
		while (*x) {
			put(*(x++));
		}
		return *this;
	}
	inline fast_writer_t &operator<<(const unsigned char *x) {
		while (*x) {
			put(*(x++));
		}
		return *this;
	}
};
fast_writer_t<> ewout;
#define int long long
#define abs(x) ((x) >= 0 ? (x) : -(x))
// #define check_m
#ifndef check_m
	#define cout ewout
#endif
int p, x, N, M, count_print;
vector<int> points[100];
#ifdef check_m
	set<int> st;
#endif
void force_move(int new_p) {
	#ifdef check_m
		st.insert(new_p);
	#else
		cout << ' ' << new_p;
	#endif
	if (new_p > 1000000000000000000ll || new_p < -1000000000000000000ll) exit(1);
	if (abs(p - new_p) < x) exit(2);
	count_print++;
	if (count_print == N) {
		#ifdef check_m
			cout << "m = " << st.size() << endl;
		#endif
		exit(0);
	}
	x = abs(p - new_p) + 1;
	p = new_p;
}
void short_right_move() {
	force_move(p + x);
}
void pairs(int l_block, int r_block) { // 模拟在两个块间往返
	#ifdef check_m
		cout << "pairs(" << l_block << ',' << r_block << ")\n";
	#endif
	while (1) {
		if (p >= points[r_block][0]) {
			int new_p = p - x;
			if (new_p < points[l_block][0])
				return;
			new_p = *--upper_bound(all(points[l_block]), new_p);
			force_move(new_p);
		} else {
			int new_p = p + x;
			if (new_p > points[r_block].back())
				return;
			new_p = *lower_bound(all(points[r_block]), new_p);
			force_move(new_p);
		}
	}
}
signed main() {
	int B, B_num;
	#ifndef check_m
	cin >> N >> M;
	#else
		#if check_m == 2
			N = 300000;
		#elif check_m == 1
			N = 100000;
		#endif
	#endif
	if (N == 300000) {
		B = 113;
		B_num = 52;
		// m = 7182
	} else if (N == 100000) {
		B = 40;
		B_num = 50;
		// m = 3222
	} else if (N == 8) {
		cout << "1 1 3 6 10 3 11 1";
		return 0;
	}
	for (int i = 1, t = 1; t <= B_num; t++, i = i * 2 + 2)
		for (int j = (i - 1) * B; j < i * B; j++)
			points[t].push_back(j - 1000000000000000000ll);
	cout << (p = points[1].back());
	count_print = 1;
	force_move(p);
	pairs(1, 2);
	for (int i = 3; i <= B_num; i++) {
		if (p + 2 * x + 1 <= points[i][0])
			short_right_move();
		force_move(points[i][0]);
		for (int j = i - 1; j >= 1; j--) {
			pairs(j, i);
			if (j > 1) {
				while (p - x < points[i][0])
					short_right_move();
				force_move(points[i][0]);
			}
		}
	}
	return 0;
}

突然感觉我没有学过语文。

原文
存档评论5 条抓取于