10.6 国庆中秋 提高组 热身赛改错
栅栏密码 原题
找规律,发现当
第二行有俩种情况:
- 第奇数个时下一个字符是
2*(h-i) - 第偶数个时下一个字符是
2*(i-1)
所以跟据规律可以得出:当在第一和
#include<bits/stdc++.h>
using namespace std;
string s;
char ans[100080];
int han = 0;
int h, len, pd1, pd2;
int check() {
if (han == 1 || han == h)return pd1 + (h - 1) * 2;
if (pd2 % 2 == 1)return pd1 + (h - han) * 2;
else return pd1 + 2 * (han - 1);
}
int main() {
cin >> h >> s;
len = s.size();
s = " " + s;
han = pd1 = pd2 = 1;
for (int i = 1; i <= len; i++) {
ans[pd1] = s[i];
pd1 = check();
pd2++;
if (pd1 > len) {
han++;
pd1 = han;
pd2 = 1;
}
}
for (int i = 1; i <= len; i++)
cout << ans[i];
return 0;
}
旅行计划 原题
在有向无环图中求经过点最多路线的点数。
第一次想用递推,因为是有向图,所以可以得出后一个点的点数为前一个点的点数加一。但是发现有些样例的答案会小。重新读题发现第
考虑动态规划:
先用数组记录入度为0的点获取最先到达的城市。再根据它的道路扩展其他城市的
#include<bits/stdc++.h>
using namespace std;
int ans, a, b, n, m, dep[1000005], fa[1000005], dp[1000005];
vector<int>edge[1005];
void dfs(int x, int father) {
fa[x] = father;
for (auto it : edge[x]) {
if (it != father) {
dep[it] = dep[x] + 1;
dfs(it, x);
}
}
}
int lca(int x, int y) {
while (dep[x] > dep[y])
x = fa[x];
while (dep[y] > dep[x])
y = fa[y];
while (x != y) {
x = fa[x];
y = fa[y];
}
return x;
}
int main() {
cin >> n;
for (int i = 1; i < n; i++) {
cin >> a >> b;
edge[a].push_back(b);
edge[b].push_back(a);
}
dfs(1, 0);
cin >> m >> b;
ans += dep[1] - dep[lca(1, b)] + dep[b] - dep[lca(1, b)];
a = b;
for (int i = 2; i <= m; i++) {
cin >> b;
ans += dep[a] - dep[lca(a, b)] + dep[b] - dep[lca(a, b)];
a = b;
}
cout << ans;
return 0;
}
鬼子进村 原题
给定
考试时看到查询,修改,删除就想到线段树了,但是没有打出来。所以先打了一个暴力代码
#include<bits/stdc++.h>
using namespace std;
const int N = 5e4+5;
int n, m, x, vis[N], ti = 1;
char c;
int main() {
cin >> n >> m;
while (m--) {
cin >> c;
if (c == 'D') {
cin >> x;
vis[x] = ti;
ti++;
}
if (c == 'R') {
int mx = -1e9;
for (int i = 1; i <= n; i++)
mx = max(mx, vis[i]);
for (int i = 1; i <= n; i++) {
if (vis[i] == mx) {
vis[i] = 0;
break;
}
}
}
if (c == 'Q') {
cin >> x;
int ls = 0, rs = 0;
if (vis[x]) {
cout << 0 << endl;
continue;
}
for (int i = x - 1; i >= 1; i--) {
if (vis[i]) break;
ls++;
}
for (int i = x + 1; i <= n; i++) {
if (vis[i]) break;
rs++;
}
cout << ls + 1 + rs << endl;
}
}
return 0;
}
要维护每个点的状态和查询连通块大小,可以使用set维护点的状态,因为可以直接使用函数求。再用一个栈维护修复的点。如果一个点被摧毁,那它的最大连通块为0。
#include<bits/stdc++.h>
using namespace std;
const int N = 5e4+5;
int n, m, a[N], x;
char op;
stack<int>sta;
set<int>st;
int main() {
cin >> n >> m;
st.insert(0), st.insert(n + 1); //从1到n,方便计算
for (int i = 1; i <= m; i++) {
cin >> op;
if (op == 'D') {
cin >> x;
sta.push(x);
st.insert(x);
}
if (op == 'R') {
int t = sta.top();
sta.pop();
st.erase(t);
}
if (op == 'Q') {
cin >> x;
auto t = lower_bound(st.begin(), st.end(), x); //查询第一个大于等于x的数
if (*t == x) //x在st中,已经被摧毁,直接输出0
cout << "0" << endl;
else
cout << *t - *(--t) - 1 << endl;
}
}
return 0;
}
无线传输 原题
给定一个字符串
因为
时间复杂度:
#include<bits/stdc++.h>
using namespace std;
const int N = 1e6+5;
char s[N], t[N];
int nxt[N], lens, lent, n;
int main() {
cin >> n >> s + 1;
for (int i = 2, j = 0; i <= n; i++) {
while (j && s[j + 1] != s[i]) j = nxt[j] ;
if (s[j + 1] == s[i]) j++;
nxt[i] = j;
}
cout << n - nxt[n];
return 0;
}