跳到正文

个人记录

3.27

TchongS1010C约 9 分钟
原文
文章信息与版本

文章信息

发布
2026年3月27日
收录
2026年3月31日
更新
2026年11月8日
篇幅
约 9 分钟

版本

  1. v2当前
  2. v1

还不错起码比之前多了一名

ABCDE总排
2020808088000010810866

A. P2021

话说数据挺水的我骗样例都有2020

我们这里的正操作是第一张牌放在最后,第二张牌输出然后扔掉,所以我们可以逆着推:第一张放进去,第二张放在最前面,我们这里可以用deque,没学过可以用queue(我用后者)

C++
#include <bits/stdc++.h>
using namespace std ;
int n , cnt , a[1000005] ;
queue<int> q ;
int main()
{
	cin >> n;
	for(int i = n ; i >= 1; i --)
	{
		q.push(i) ;
		q.push(q.front()) ;
		q.pop() ;
	}
	while(q.size())
	{
		a[++ cnt] = q.front() ;
		q.pop() ;
	}
	for(int i = n ; i >= 1 ; i --)
    {
		cout << a[i] << " " ;
    }
	return 0;
}

B P8641

模拟,没有什么太好说的,我们可以搞一个备份bak,这样就不用队列每次都清空,加数了

INFO

如果再也得不到任何卡牌,我们只需要枚举nn次,因为没有一张卡片的数>n>n

C++
#include <bits/stdc++.h>
using namespace std;
int n , x , ans ;
queue <int> bak , q ;
int main()
{
	cin >> n ;
	for(int i = 1; i <= n; i++)
    {
        cin >> x;
        bak.push(x);
    }
    for(int i = 1; i <= n; i++)
    {
        bak.push(bak.front()) ;
        bak.pop() ;
        q = bak ;
        int idx = 0 , sum = 0 ;
        while(idx <= n && q.size())
        {
            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;
}

3.P13512

方法1:我们肯定知道只要取越大的数量就越少,我们可以从aa复制数组bb,然后每次sort(b + 1 , b + i + 1) ;然后每次取最大的加在sumsum里,什么时候sum≥psum \ge p就输出cntcnt,如果∑j=1iai<p\sum_{j=1}^{i}a_i<p输出−1-1,时间复杂度O(n2×log⁡2n)O(n^2\times \log_{2} n)

方法2:我们可以用优先队列大根堆来代替数组,不用排序了,这样子时间复杂度是O(n2)O(n^2)

方法3:我们可以先把所有的数字都加起来(想啥啊,当然是一次加一个数啊),然后把最小的minnminn给搞出去(当然是用小跟堆啦),当然前提是sum−minn>=psum-minn >= p,然后最后面直接输出优先队列的长度(如果sum<psum<p输出−1-1)(保证AC)

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

4.U504636

一看数据范围:这可以模拟! 这里的桌子满足先进后出的规律,所以可以用栈,而A和B的牌满足先进先出,用队列,我们判断有没有重复用map

C++
#include <bits/stdc++.h>
using namespace std ;
int n , m , x ;
queue <int> a , b ;
int op = 1 ;
stack <int> desk ;
map <int , int> mp ;
int main()
{
	cin >> n >> m ;
	for(int i = 1 ; i <= n ; i ++)
	{
		cin >> x ;
		a.push(x) ;
	}
	for(int i = 1 ; i <= m ; i ++)
	{
		cin >> x ;
		b.push(x) ;
	}
	while(a.size() && b.size())
	{
		if(op == 1)
		{
			x = a.front() ;
			desk.push(x) ;
			a.pop() ;
            mp[x] ++ ;
			if(mp[x] >= 2)
			{
                mp[x] -= 2 ;
                desk.pop() ;
                a.push(x) ;
                while(desk.top() != x)
                {
                    a.push(desk.top()) ;
                    mp[desk.top()] -- ;
                    desk.pop() ;
                }
                a.push(x) ;
                desk.pop() ;
			}
		}
		else
		{
			x = b.front() ;
			desk.push(x) ;
			b.pop() ;
            mp[x] ++ ;
		}
        op = op % 2 + 1 ;
	}
	if(a.size())
	{
		cout << "A\n" ;
		while(a.size())
		{
			cout << a.front() << " " ;
            a.pop() ;
		}
	}
	else
	{
		cout << "B\n" ;
		while(b.size())
		{
			cout << b.front() << " " ;
            b.pop() ;
        }
	}
	return 0 ;
}

5. P2107

其实就是一个反悔贪心

假设你,小Z是神犇,走到某个机房必须要AK才走,但是如果时间不够,你还可以把你之前AK的机房瞬间变AU,然后夺得之前浪费的时间。

那么如果时间超过了mm,你会选择让时间最多的AU,还是最少的AU?

那必须是耗时最多的啊!

所以机房自动排成从近到远静静地等待着你的AK,然后你遇到一个机房就AK,但是如果时间>m>m,那么一直重复把时间最多的AU,然后总时间减少耗时的时间

C++
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 100005;
ll n , m , tim , ans , cnt ;
struct Node
{
    ll x, t;
    bool operator < (const Node &other) const
    {
        return x < other.x;
    }
} cr[MAXN];
priority_queue<ll> q;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> m;
    for (int i = 1; i <= n; ++i)
    {
        cin >> cr[i].x >> cr[i].t;
    }
    sort(cr + 1, cr + n + 1);
    for (int i = 1; i <= n; ++i)
    {
        tim += cr[i].x - cr[i-1].x;
        q.push(cr[i].t);
        cnt++;
        tim += cr[i].t;
        while (!q.empty() && tim > m)
        {
            cnt--;
            tim -= q.top();
            q.pop();
        }
        if (tim > m) break;
        ans = max(ans, cnt);
    }
    cout << ans << endl;
    return 0;
}
原文