20250721
_little_duck_ · · 个人记录
A.AtCoder abc344_e
题意
给定一个长度为
接下来有
1 x y:在A 中元素x 的后面插入y 。保证在该操作时,A 中一定存在x 。2 x:从A 中删除元素x 。保证在该操作时,A 中一定存在x 。
保证每次操作后,
请输出所有操作处理完后的数列
思路
这是一道数据结构版子题,可以用链表维护数列,同时用 map 维护每个值得出现位置节省时间。
易错点
- 数组要开
n + q 的长度 - 要用 map 维护位置,否则会超时
代码
#include<bits/stdc++.h> using namespace std;
const int N = 4e5 + 10;
map<int, int> m;
int n, q, op, x, y, cnt;
struct Node{ int pre, nxt; int v; }a[N];
int h, t;
void Insert(int d, int x) { a[++cnt].v = x; m[x] = cnt; int p = m[d]; a[cnt].pre = p, a[cnt].nxt = a[p].nxt; a[p].nxt = a[a[p].nxt].pre = cnt; }
void Delete(int d){ int p = m[d]; a[a[p].pre].nxt = a[p].nxt; a[a[p].nxt].pre = a[p].pre; a[p].nxt = a[p].pre = 0; }
int main(){ cin >> n; h = 0, t = N - 5; a[h].nxt = t, a[t].pre = h; for(int i = 1, x, lat = 0; i <= n; i++){ cin >> x; Insert(lat, x); lat = x; } cin >> q; while(q--){ cin >> op; if(op == 1){ cin >> x >> y; Insert(x, y); } if(op == 2){ cin >> x; Delete(x); } } for (int i = a[h].nxt; i != t; i = a[i].nxt) { cout << a[i].v << " "; } return 0; }
# B.洛谷 P7912
## 题意
给定一个长度为 $n$ 的 01 串,每次将连续的 0 或 1 块的第一个拿出来删除,并记录答案
## 思路
与 A 题类似,用链表维护串,在输入时先将与前一个字符不同的字符下标计入数组。接着对于数组内的每个元素,看下一个元素是否会在本轮被删除,如果不会被删除并且与前一个元素不同,则记录到临时数组,并删除自己。最后将临时数组赋值答案数组
## 易错点
- 边界判断
- 要在遍历完后才标记删除
- 链表更新顺序
## 代码
```cpp
#include<bits/stdc++.h>
using namespace std;
const int N = 4e5 + 10;
int n, q, op, x, y, cnt, b[N];
struct Node{
int pre, nxt;
}a[N];
int f[N];
int h, t;
vector<int> ans, ttt;
void Insert(int d, int x){
a[a[d].nxt].pre = x, a[x].nxt = a[d].nxt;
a[d].nxt = x, a[x].pre = d;
}
void Delete(int d){
int p = d;
if(b[a[p].nxt] != -1 && b[a[p].nxt] != b[a[p].pre] && !f[a[p].nxt]){
ttt.push_back(a[p].nxt);
}
a[a[p].pre].nxt = a[p].nxt;
a[a[p].nxt].pre = a[p].pre;
}
int main(){
cin >> n;
h = 0, t = N - 1;
a[h].nxt = t, a[t].pre = h;
b[h] = b[t] = -1;
for(int i = 1, x; i <= n; i++){
cin >> b[i];
Insert(i - 1, i);
}
for(int i = 1; i <= n; i++){
if(b[i - 1] != b[i]){
ans.push_back(i);
}
}
while(!ans.empty()){
for(auto i : ans){
f[i] = 1;
}
for(int i : ans){
Delete(i);
cout << i << " ";
}
cout << "\n";
for(int i : ans){
f[i] = 0;
}
ans = ttt, ttt.clear();
}
return 0;
}
C.洛谷 P4085
题意
有
思路
用双指针维护区间范围,用 multiset 维护范围内元素。当右指针移动时,总和累加,新元素加入 multiset;当左指针移动时,总和减少,将 multiset 中对应的值弹出一个
易错点
multiset 删除时要用 find 找到第一个需要元素,不能全删
代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5 + 10;
int n, m, sum, ans = INT_MAX;
multiset<int> st;
struct P{
int f, s;
}a[N];
signed main(){
cin >> n >> m;
for(int i = 1; i <= n; i++){
cin >> a[i].f >> a[i].s;
}
for(int i = 1, j = 1; i <= n && j <= n; i++){
while(sum < m){
sum += a[j].f;
st.insert(a[j].s);
j++;
}
ans = min(ans, *st.rbegin());
st.erase(st.find(a[i].s));
sum -= a[i].f;
}
cout << ans;
return 0;
}
F.AtCoder abc359_e
题意
有
倒水操作:将第
0 个格子的水位增加1 ,如果水位超过当前挡板高度,则不断往下一个区间流水,直至水位与隔板高度平齐思路
对于每个隔板找它前面第一个比它高的隔板,可用单调栈维护。这样就可算出容水体积,也就可以算出要用几桶水,可得到公式:
## 代码 ```cpp #include<bits/stdc++.h> using namespace std; #define int long long
const int N = 2e5 + 10; int n, h[N], ans[N];
stack<int> st;
signed main(){ cin >> n; h[0] = INT_MAX; for (int i = 1; i <= n; i++) { cin >> h[i]; } st.push(0); for(int i = 1; i <= n; i++){ while(h[st.top()] < h[i]){ st.pop(); } ans[i] = ans[st.top()] + h[i] * (i - st.top()); st.push(i); } for(int i = 1; i <= n; i++){ cout << ans[i] + 1 << " "; } return 0; }