跳到正文

题解

P3751题解

zhantaiming2014约 9 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 9 分钟
题目
P3751

版本

  1. v1当前

首先,这道题可以写一个纯暴力枚举每一分钟。 纯暴力代码(30分):

C++
#include <bits/stdc++.h>
//万能头
using namespace std;
int n, m;
vector <int> a[100005];
//直接数组会MLE
vector <int> v_x, v_y;
int k[100005];
void cls(vector <int> & v) {
	vector <int> v2;
	swap(v2, v);
	return ;
//	v.clear
//	但好像比clear快
}
void in(int x, vector <int> & v) {
	cls(v);
	v.push_back(a[x][0]);
	for (int i = 1; i < a[x].size(); i++) {
		if (a[x][i - 1] <= a[x][i]) {
			for (int  j = a[x][i - 1] + 1; j <= a[x][i]; j++) {
				v.push_back(j);
//				cout << j << " ";
			}
		} else {
			for (int j = a[x][i - 1] - 1; j >= a[x][i]; j--) {
				v.push_back(j);
//				cout << j << " ";
			}
		}
	}
//	cout << endl;
//	暴力 把每个时刻的位置做记录
	return ;
}
map <pair<int, int>, int> mp;
int main() {
	cin.tie(0), cout.tie(0);
	ios::sync_with_stdio(0);
	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
		cin >> k[i];
		for (int j = 1, x; j <= k[i]; j++) {
			cin >> x;
			a[i].push_back(x);
		}
	}
	while (m--) {
		int x, y;
		cin >> x >> y;
		if (mp[ {x, y}] != 0) {
			cout << mp[ {x, y}];
			continue;
		}
		in(x, v_x);
		in(y, v_y);
		int tot = 0;
//		cout << 1 << ":" << v_x[0] << " " << v_y[0] << "\n";
		for (int i = 1; i < min(v_x.size(), v_y.size()); i++) {
//			cout << i + 1 << ":";
			if (v_x[i] == v_y[i]) {
				tot++;
//				cout << i << endl;
			} else {
				if ((v_x[i] < v_y[i]) ^ (v_x[i - 1] < v_y[i - 1]) && (v_x[i - 1] != v_y[i - 1])) {
					tot++;
//					cout << i << endl;
				}
			}
//			cout << v_x[i] << " " << v_y[i] << " " << tot << "\n";
		}
		cout << tot << "\n";
		mp[ {x, y}] = tot;
		mp[ {y, x}] = tot;
//		1: 3 2  2:2 3
//		注意这样其实有
//		所以不能用==来判断
	}
	return 0;
}

容易想到对于x.kix.k_i到x.ki+1x.k_{i+1},y.kiy.k_i到y.ki+1y.k_{i+1}最多只会相遇一次,对于每一次x,yx,y进行模拟,每次x,yx,y可以将一次掉头到下一次掉头作为一个区间初始化。可以用双指针i,ji,j模拟目前在哪一个区间,然后算出在哪里相遇,什么时间相遇,再判断是否合法。 代码(80分):

C++
#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
int n, m;
vector <int> a[100005];
int k[100005];
//读入部分
vector <int> v[100005][2];
//初始化
int main() {
	cin.tie(0), cout.tie(0);
//	ios::sync_with_stdio(0);
	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
		cin >> k[i];
		for (int j = 1, x; j <= k[i]; j++) {
			cin >> x;
			a[i].push_back(x);
		}
	}
//	预处理
	for (int i = 1; i <= n; i++) {
//		cout << i << " " << k[i] << ": ";
		for (int j = 0, now = 1; j < k[i] - 1; j++) {
			v[i][0].push_back(now);
			v[i][1].push_back(now + abs(a[i][j + 1] - a[i][j]));
			now += abs(a[i][j + 1] - a[i][j]);
//			cout << now << " ";
		}
//		cout << endl;
	}
	while (m--) {
		int x, y;
		cin >> x >> y;
		int anscnt = 0,j=0;
		for (int i = 0; i < (int)v[x][0].size(); i++) {
//			cout << i << " ";
			for (j = (j ? j - 1 : 0); j < v[y][0].size() && v[y][0][j] <= v[x][1][i]; j++) {
				if ((a[y][j] < a[y][j + 1]) != (a[x][i] < a[x][i + 1])) {
//					方向一样追不上 时间对不上也不行
					if (v[y][1][j] > v[x][0][i]) {
//						在哪遇见 什么时间
						int for_x = a[x][i];
						int for_y = a[y][j];
						int will_add_abs = abs(v[y][0][j] - v[x][0][i]);
						if (v[x][0][i] < v[y][0][j]) {
							if (a[x][i] < a[x][i + 1]) {
								for_x += will_add_abs;
							} else {
								for_x -= will_add_abs;
							}
						} else {
							if (a[y][j] < a[y][j + 1]) {
								for_y += will_add_abs;
							} else {
								for_y -= will_add_abs;
							}
						}
						double will_meet_place = (for_x + for_y) / 2.0, will_meet_time = abs(will_meet_place - for_x);
//						判断是否合法
						if (a[x][i] < a[x][i + 1]) {
							if (will_meet_place < a[x][i]) {
								goto M1;
//                				goto不知道可以搜一搜
							}
						} else {
							if (will_meet_place > a[x][i]) {
								goto M1;
							}
						}
						if (a[y][j] < a[y][j + 1]) {
							if (will_meet_place < a[y][j]) {
								goto M1;
							}
						} else {
							if (will_meet_place > a[y][j]) {
								goto M1;
							}
						}
//						cout << i << " " << j << ":" << for_x << " " << for_y << " " << will_add_abs << " " << will_meet_place << "  ";
						if (will_meet_time < v[y][1][j] && will_meet_time < v[x][1][i]) {
							if (a[x][i] < a[x][i + 1]) {
								if (!(will_meet_place > a[x][i + 1] || will_meet_place < a[y][j + 1]/* || will_meet_place < a[x][i] || will_meet_place < a[y][j]*/)) {
									anscnt++;
								}
							} else {
								if (!(will_meet_place < a[x][i + 1] || will_meet_place > a[y][j + 1]/* || will_meet_place < a[y][j] || will_meet_place > a[x][i]*/)) {
									anscnt++;
								}
							}
						}
M1:
						;
					}
				}
			}
//			cout << endl;
		}
		cout << anscnt << endl;
	}
	return 0;
}
原文