跳到正文

个人记录

3.27总结

Cabbage_Squirrel约 10 分钟
原文
文章信息与版本

文章信息

收录
2026年3月28日
更新
2026年5月23日
篇幅
约 10 分钟

版本

  1. v2当前
  2. v1

P2021 faebdc玩扑克

Meaning

zky 有 nn 个扑克牌,编号从 11 到 nn,zky 把它排成一个序列,每次把最上方的扑克牌放在牌堆底,然后把下一张扑克牌拿出来输出,最终输出的序列恰好是从 11 到 nn,faebdc 问你原序列是什么,因为 faebdc 神犇早已在 O(1)\mathcal O (1) 的时间得出结果,如果你在 1 s 内答不出来,faebdc 会吃了你。

Solution

因为我不知道除了模拟还能怎么做这道题,且模拟不会超时,所以我们可以来模拟,显然我们用双端队列写最简单。

Code

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e6 + 10;
int n,head,tail;
deque<int>dq;
signed main() {
	cin >> n;
	for(int i = n;i >= 1;i --) {
		dq.push_front(i);
		dq.push_front(dq.back());
		dq.pop_back();
	}
	while(dq.size()) {
		cout << dq.front() << ' ';
		dq.pop_front();
	}
	return 0;
}

P8641 [蓝桥杯 2016 国 C] 赢球票

Meaning

拿出 NN 张卡片(上面写着 1∼N1 \sim N 的数字),打乱顺序,排成一个圆圈。

你可以从任意一张卡片开始顺时针数数 :1,2,3⋯:1,2,3 \cdots 。

如果数到的数字刚好和卡片上的数字相同,则把该卡片收入囊中,从下一个卡片重新数数。

直到再无法收获任何卡片,游戏结束。囊中卡片数字的和就是赢得球票的张数。

Solution

我们可以用 22 个队列来模拟,反正超时不了。枚举从每个位置开始能得多少球票,记录最大的,最后输出即可。

Code

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 10,M = 1e6 + 5;
queue<int>bak,q;
int n;
signed main() {
	cin >> n;
	for(int i = n;i >= 1;i --) {
		int x;
		cin >> x;
		bak.push(x);
	}
	int ans = 0;
	for(int i = 1;i <= n;i ++) {
		bak.push(bak.front());
		bak.pop();
		q = bak;
		int idx = 0,sum = 0;
		while(idx <= n && !q.empty()) {
			idx ++;
			if(idx == q.front()) {
				sum += idx;
				q.pop();
				idx = 0;
			}else {
				q.push(q.front());
				q.pop();
			}
		}
		ans = max(ans,sum);
	}
	cout << ans;
	return 0;
}

P13512 [KOI 2025 #1] 稻草人

Meaning

数轴上有 NN 个位置,每个位置 ii 可以放一个防御力为 AiA_i 的稻草人。箭从 00 出发向右射,力量为 PP。碰到稻草人时,如果箭的力量 ≤\le 稻草人防御力,箭停下;否则箭的力量减去 AiA_i 继续前进。

f(i)f(i) 表示“让箭在位置 ii 或它左边停下,最少需要放几个稻草人”。如果不可能停下就输出 −1-1。

输出 f(1)f(1) 到 f(N)f(N)。

Solution

从左往右看,每到一个位置,我们就考虑从 11 到 ii 里选一些稻草人,让它们的防御力和 ≥P\ge P,并且数量最少。

要想数量最少,肯定优先选防御力大的。所以我们维护一个小根堆,存当前选中的稻草人。

每走到位置 ii:

  1. 把 AiA_i 加进堆里,总和 ss 加上 AiA_i。
  2. 如果去掉堆里最小的那个,总和仍然 ≥P\ge P,那就去掉它(因为少用一个稻草人当然更好)。
  3. 最后看总和够不够 PP:够的话堆的大小就是答案,不够就输出 −1-1。

Code

C++
#include <bits/stdc++.h>
using namespace std;
int N, P;
long long s;
int A[500005];
priority_queue<int, vector<int>, greater<int> > q;

int main() {
	cin >> N >> P;
	for (int i = 1; i <= N; i++) cin >> A[i];

	for (int i = 1; i <= N; i++) {
		s += A[i];
		q.push(A[i]);
		while (q.size() && s - q.top() >= P) {
			s -= q.top();
			q.pop();
		}
		if (s < P) cout << -1 << ' ';
		else cout << q.size() << ' ';
	}
	return 0;
}

U504636 拖板车

Meaning

两个人 AA 和 BB 玩拖板车。每人手里一叠牌,轮流出牌,AA 先出。出的牌放在桌面堆叠,后出的在上面。出牌时,如果桌面已经有这张牌,出牌的人就把两张相同牌及它们中间的所有牌收走,放在自己手牌底部,然后对方继续出牌。如果一方手牌出完,对方获胜。输入 A、BA、B的初始手牌,输出胜者和胜者的手牌。

Solution

直接模拟整个过程。用两个队列存手牌,一个栈模拟桌面,一个 map 记录桌面上的牌是否出现过。轮流操作,每次从当前玩家队列取队首牌 xx。如果 map 里没有 xx,就压入栈顶并标记出现过。如果出现过,就把 xx 放回自己手牌队尾,然后从栈顶不断弹出牌放到手牌队尾,直到弹出的牌也是 xx。这样收牌就完成了。每次操作后检查自己手牌是否空,空则对方胜利,输出对方手牌。

Code

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 15,P = 1e9 + 7;
queue<int>qa,qb;
stack<int>stk;
map<int,int>vis;
int n,m;
signed main() {
	cin >> n >> m;
	for(int i = 1;i <= n;i ++) {
		int x;
		cin >> x;
		qa.push(x);
	}
	for(int i = 1;i <= m;i ++) {
		int x;
		cin >> x;
		qb.push(x);
	}
	bool snowane = 0,yuxiao = 0;
	while(qa.size() && qb.size()) {
		int x = qa.front();
		qa.pop();
		if(vis[x] == 0) {
			stk.push(x);
			vis[x] = 1;
		} else {
			qa.push(x);
			while(1)
			{
				int y = stk.top();
				stk.pop();
				qa.push(y);
				vis[y] = 0;
				if(y == x) break;
			}
		}
		if(qa.empty())
		{
			cout << "B\n";
			while(qb.size())
			{
				cout << qb.front() << ' ';
				qb.pop();
			}
			return 0;
		}
		x = qb.front();
		qb.pop();
		if(vis[x] == 0) stk.push(x), vis[x] = 1;
		else
		{
			qb.push(x);
			while(1)
			{
				int y = stk.top();
				stk.pop();
				qb.push(y);
				vis[y] = 0;
				if(y == x) break;
			}
		}
		if(qb.empty())
		{
			cout << "A\n";
			while(qa.size())
			{
				cout << qa.front() << ' ';
				qa.pop();
			}
			return 0;
		}
	}
	return 0;
}

P2107 小 Z 的 AK 计划

Meaning

小 Z 从家(坐标 00)出发,街上依次有 nn 个机房,第 ii 个机房坐标 xix_i。移动速度是 11,所以从上一个位置到 xix_i 需要 ∣xi−xprev∣|x_i - x_{prev}| 的时间。到达机房后,可以选择花 tit_i 的时间“想题”,然后瞬间 AK,也可以直接路过。总时间限制为 mm,问最多能在多少个机房 AK。

Solution

首先,机房在一条直线上,小 Z 肯定只会往一个方向走(因为折返会浪费更多时间),所以我们可以把机房按坐标从小到大排序,然后依次考虑。

我们用一个优先队列来维护当前已经决定要 AK 的机房,队列里存的是每个机房的 tit_i。

按顺序处理每个机房 ii:

  • 先加上从上一个机房(或家)走到当前机房的移动时间 xi−xi−1x_i - x_{i-1}(x0=0x_0=0)。
  • 把这个机房加入候选,总时间加上它的 tit_i,候选数量加 11。
  • 如果此时总时间超过了 mm,就说明我们选得太多了,需要去掉一些。因为移动时间已经固定,只能从已经选的机房中踢掉一个最耗时的(tt 最大的),这样总时间减少得最多,能保留尽可能多的机房。
  • 从队列里不断弹出最大的 tt,直到总时间 ≤m\le m 为止。如果弹出所有还是超时,那后面的机房也没法再选了,直接结束。

这样做下来,每次处理完一个机房后,当前候选数量就是“以当前机房为最远点时能选的最多机房数”,用 ansans 记录最大值即可。

Code

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
pair<int,int> a[100005];
priority_queue<pair<int,int> >q;
int ti,problem,ans;
signed main() {
	int n,m;
	cin >> n >> m;
	for(int i=1; i<=n; i++) {
		int x,y;
		cin >> x >> y ;
		a[i] = make_pair(x,y);
	}
	sort(a + 1,a + n + 1);
	for(int i = 1;i <= n;i ++) {
		ti += a[i].first - a[i - 1].first;
		q.push(make_pair(a[i].second,a[i].first));
		problem ++;
		ti += a[i].second;
		if(ti > m) {
			while(ti > m && q.size()) {
				problem --;
				ti -= q.top().first;
				q.pop();
			}
		}
		if(ti > m) {
			break;
		}
		ans = max(ans,problem);
	}
	cout<< ans;
	return 0;
}
原文