10.6集训改错

P10312 [SHUPC 2024] 栅栏密码

可以暴力模拟,由于不管怎么变化,在密文中的位置都是不变的所以得到长度之后可以预处理密文在图中的位置,反推得到明文

#include<bits/stdc++.h>
using namespace std;
int h;
string str;
int mp[20][100005];
int main() {
	cin >> h >> str;
	int l = str.size();
	str = " " + str;
	int f = 1;
	int y = 0;
	for (int j = 1; j <= l; j++) {
		y += f;
		mp[y][j] = 114514;
		if (y == h)f = -1;
		else if (y == 1)f = 1;
	}
	int cnt = 0;
	for (int i = 1; i <= h; i++) {
		for (int j = 1; j <= l; j++) {
			if (mp[i][j] == 114514) {
				mp[i][j] = ++cnt;
			}
//			cout<<mp[i][j]<<" ";
		}
//		cout<<'\n';
	}
	f = 1;
	y=0;
	for (int j = 1; j <= l; j++) {
		y += f;
		cout << str[mp[y][j]];
		if (y == h)f = -1;
		else if (y == 1)f = 1;
	}
	return 0;
}

P1137 旅行计划

本题有一个神奇的思路就是spfa 的最长路但是经过观察就会发现,这是一个有向图,而且没说是否只有一个点入度为0,拿咋整?好办,把入度为零的全部先入进去,其他的就是模板了
正确性:
(1) 初始化​
每个节点的初始最长路径长度 dis[i] = 1(至少包含自己)。

将所有节点加入队列,确保每个节点有机会作为路径起点。

​​(2) 松弛操作​
对于节点 u的出边 u→v,若通过 u可以获得更长的路径(dis[u] + 1 > dis[v]),则更新 dis[v]并将 v加入队列。

​关键性质​:由于图是有向无环图,任何节点的更新操作不会导致无限松弛。每次更新都是基于已知的更长路径。

​​(3) 终止条件​
当队列为空时,所有可能的路径已被探索。此时 dis[i]即为以 i为终点的最长路径长度。

#include<bits/stdc++.h>
using namespace std;
const int N = 1e6+5;
vector<int> to[100010];
bool vis[100010];
int dis[100010];
int n, m, x, y, j;
int s;
int ind[N];
int main( ) {
	cin >> n >> m;
	while (m--) {
		cin >> x >> y;
		to[x].push_back(y);
		ind[y]++;
	}	queue<int>q;
	for (int i = 1; i <= n; i++)
		if (ind[i] == 0) {
			q.push(i);
			vis[i]=1;
			dis[i]=1;
		}
	while (!q.empty()) {
		x = q.front();
		q.pop();
		vis[x] = 0;
		for (auto i : to[x])
			if (dis[i] < dis[x] + 1) {
				dis[i] = dis[x] + 1;
				if (!vis[i]) {
					q.push(i);
					vis[i] = 1;
				}
			}
	}
	for (j = 1; j <= n; j++)
		cout << dis[j] << "\n";
}

鬼子进村

这题有一个很简单的方法就是使用STL模板的set,记录每个被摧毁得点,存入栈内,要修复是从栈顶取,查到一个点就查前驱和后继

#include<bits/stdc++.h>
using namespace std;
const int N = 1e6+5;
int n, m;
char lx;
int x;
set<int >st;
int sta[N], top;
set<int>:: iterator y;
int main() {
	cin >> n >> m;
	st.insert(0);
	st.insert(n+1);
	while (m--) {
		cin >> lx;
		if (lx == 'D') {
			cin >> x;
			st.insert(x);
			sta[++top] = x;
		} else if (lx == 'R') {
			y = st.find(sta[top--]);
			st.erase(y);
		} else {
			cin >> x;
			y = st.lower_bound(x);
			if (*y == x) {
				cout << 0 << '\n';
			} else {
				cout << *y - *(--y) -1 << '\n';
			}
		}
	}
	return 0;
}
posted @ 2025-10-06 18:57  ppi_SAMA  阅读(14)  评论(0)    收藏  举报