跳到正文

个人记录

2026.3.27

Hyr_lu约 6 分钟
原文

这是 2026年3月27日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年3月27日
收录
2026年3月27日
更新
2026年10月3日
篇幅
约 6 分钟

版本

  1. v2当前
  2. v1

faebdc玩扑克

这是一到很简单的模拟题,我选择用数组来模拟。

首先while(1)因为不知道什么时候结束,每一次循环1到n,每找到两个没有存储答案的a[i]就将目前的a[i]存为数s,s从1开始,每找到一次s就+1,若发现a数组已经满了,就条数循环输出答案。

100pts

C++
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5;
int n,a[N];
int main()
{
	cin>>n;
	int s=1,j=0;
	while(1)
	{
		for(int i=1;i<=n;i++)
		{
			if(!a[i])j++;
			if(j==2)a[i]=s,s++,j=0;
		} 
		int f=0;
		for(int i=1;i<=n;i++)if(!a[i])f=1;
		if(!f)break;
	}
	for(int i=1;i<=n;i++){
		cout<<a[i]<<" ";
	}
	return 0;
} 

赢球票

这段代码通过枚举每一个起始位置,用队列模拟卡片顺时针轮转的过程,每次数到与队首卡片相同的数字就累加得分并重置计数,最终找出所有起始位置中能得到的最大分数。

100pts

C++
#include<bits/stdc++.h>
using namespace std;
int n,ans=0;
queue<int>q,x;
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
    {
        int a;
        cin>>a;
        q.push(a);
    }
    for(int i=1;i<=n;i++)
    {
        q.push(q.front());
        q.pop();
        x=q;
        int idx=0,sum=0;
        //模拟游戏过程:直到无法再收取卡片或所有卡片都被收完
        while(idx<=n&&!q.empty())
        {
            idx++;
            if(idx==x.front())//数到的数字和队首卡片数字相同
            {
                sum+=idx; //将该卡片数字加入总和
                x.pop();
                idx=0;//从下一张卡片重新开始数数,重置计数
            }
            else//不匹配,将队首卡片移到队尾,继续顺时针数数
            {
                x.push(x.front());
                x.pop();
            }
        }
        ans=max(ans,sum);
    }
    cout<<ans;
	return 0;
} 

稻草人

我们用贪心 + 小根堆的思路来解决问题:要让箭在位置i或左侧停下,知道最少需要选几个稻草人,就是用最少数量凑出总和≥P的防御力。遍历每个位置时,把当前稻草人加入小根堆然后累加起来算出总和,只要总和减去堆里最小防御力后还是≥P,就移除这个最小的稻草人(因为保留更大的能减少数量)。最后如果总和≥P,堆的大小就是最少稻草人数量;否则永远停不下来,输出-1。

100pts

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
int a[500005];
priority_queue<int,vector<int>,greater<int>>q;
signed main()
{
	int n,p,ans=0;
	cin>>n>>p;
	for(int i=1;i<=n;i++)cin>>a[i]; 
    for(int i=1;i<=n;i++)
    {
        q.push(a[i]);
        ans+=a[i];
        while(ans-q.top()>=p)
        {
            ans-=q.top();
            q.pop();
        }
        if(ans<p)cout<<-1<<" ";
        else cout<<q.size()<<" ";
    }
	return 0;
}

拖板车

这其实是一个真实的游戏。我们可以通过队列模拟 A、B 两人的手牌顺序,用栈模拟桌面牌堆,并借助map快速检测重复牌,实现游戏:循环中 A、B 轮流从各自手牌队首出牌,若桌面没有重复的牌,则压入栈并标记,若有重复的牌,则将新牌放回手牌队尾,再从栈顶开始收牌直到找到重复牌,然后将收回的牌按桌面从逆序放回手牌队尾并清除标记;若果某一方手牌出完了,那么另一方获胜,并输出获胜方剩余手牌。

100pts

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