关于deque的clear()
  • 板块学术版
  • 楼主灰的积雨云
  • 当前回复5
  • 已保存回复5
  • 发布时间2022/11/22 22:07
  • 上次更新2023/10/27 01:53:28
查看原帖
关于deque的clear()
349713
灰的积雨云楼主2022/11/22 22:07

在写贪吃蛇的时候使用了deque

发现对于每组询问修改后的a

如果新建deque

就可以A

如果使用不新建,使用clear()WA

新建dequeCODE:

#include <cstdio>
#include <iostream>
#include <cmath>
#include <queue>
#define lfor(i, x, y) for (int i = (x); i <= (y); ++ i)
#define llfor(i, x, y) for (int i = (x); i < (y); ++ i)
#define rfor(i, x, y) for (int i = (x); i >= (y); -- i)
#define rlfor(i, x, y) for (int i = (x); i > (y); -- i)
#define For(i, p) for (int i = head[p]; i; i = nxt[i])
#define vfor(i, x) for (int i = 0; i < (x); ++ i)
#define vi ver[i]
#define mp(x, y) make_pair(x, y)
#define pub(x) push_back(x)
#define pob() pop_back()
#define puf(x) push_front(x)
#define pof() pop_front()
#define fi first
#define se second
using namespace std;

typedef pair<int, int> pr;
const int N = 1e6 + 10;
int a[N], n;

void work(int T)
{
	if (T == 1)
	{
		scanf("%d", &n);
		lfor (i, 1, n) scanf("%d", &a[i]);
	}
	else 
	{
		int k; scanf("%d", &k);
		lfor (i, 1, k) 
		{
			int x, y; scanf("%d %d", &x, &y);
			a[x] = y; 
		}
	}
	
	deque<pr> q1, q2;
	lfor (i, 1, n) q1.pub(mp(a[i], i));
	int ans;
	while (1)
	{
		if (q1.size() + q2.size() == 2) return puts("1"), void();
		pr wk = q1.front(); q1.pof();
		pr st;
		if (q2.empty() || (!q1.empty() && q1.back() > q2.back())) st = q1.back(), q1.pob();
		else st = q2.back(), q2.pob();
		pr now = mp(st.fi - wk.fi, st.se);
		
		if (q1.empty() || q1.front() > now)
		{
			ans = q1.size() + q2.size() + 2;
			int cnt = 0;
			while (1)
			{
				++ cnt;
				if (q1.size() + q2.size() + 1 == 2)
				{
					if (cnt % 2 == 0) -- ans;
					break;
				}
				pr smx;
				if (q2.empty() || (!q1.empty() && q1.back() > q2.back())) smx = q1.back(), q1.pob();
				else smx = q2.back(), q2.pob();
				
				now = pr(smx.fi - now.fi, smx.se);
				if (((q1.empty()) || now < q1.front()) && (q2.empty() || now < q2.front())) {;}
				else
				{
					if (cnt % 2 == 0) -- ans;
					break;
				}
			}
			break;
			
		}
		else q2.puf(now);
	}
	printf("%d\n", ans);
}

int main()
{
	int T; scanf("%d", &T);
	lfor (i, 1, T) work(i);
	return 0;
}

不新建

#include <cstdio>
#include <iostream>
#include <cmath>
#include <queue>
#define lfor(i, x, y) for (int i = (x); i <= (y); ++ i)
#define llfor(i, x, y) for (int i = (x); i < (y); ++ i)
#define rfor(i, x, y) for (int i = (x); i >= (y); -- i)
#define rlfor(i, x, y) for (int i = (x); i > (y); -- i)
#define For(i, p) for (int i = head[p]; i; i = nxt[i])
#define vfor(i, x) for (int i = 0; i < (x); ++ i)
#define vi ver[i]
#define mp(x, y) make_pair(x, y)
#define pub(x) push_back(x)
#define pob() pop_back()
#define puf(x) push_front(x)
#define pof() pop_front()
#define fi first
#define se second
using namespace std;

typedef pair<int, int> pr;
const int N = 1e6 + 10;
int a[N], n;
deque<pr> q1, q2;

void work(int T)
{
	if (T == 1)
	{
		scanf("%d", &n);
		lfor (i, 1, n) scanf("%d", &a[i]);
	}
	else 
	{
		int k; scanf("%d", &k);
		lfor (i, 1, k) 
		{
			int x, y; scanf("%d %d", &x, &y);
			a[x] = y; 
		}
	}
	
	lfor (i, 1, n) q1.pub(mp(a[i], i));
	int ans;
	while (1)
	{
		if (q1.size() + q2.size() == 2) return puts("1"), void();
		pr wk = q1.front(); q1.pof();
		pr st;
		if (q2.empty() || (!q1.empty() && q1.back() > q2.back())) st = q1.back(), q1.pob();
		else st = q2.back(), q2.pob();
		pr now = mp(st.fi - wk.fi, st.se);
		
		if (q1.empty() || q1.front() > now)
		{
			ans = q1.size() + q2.size() + 2;
			int cnt = 0;
			while (1)
			{
				++ cnt;
				if (q1.size() + q2.size() + 1 == 2)
				{
					if (cnt % 2 == 0) -- ans;
					break;
				}
				pr smx;
				if (q2.empty() || (!q1.empty() && q1.back() > q2.back())) smx = q1.back(), q1.pob();
				else smx = q2.back(), q2.pob();
				
				now = pr(smx.fi - now.fi, smx.se);
				if (((q1.empty()) || now < q1.front()) && (q2.empty() || now < q2.front())) {;}
				else
				{
					if (cnt % 2 == 0) -- ans;
					break;
				}
			}
			break;
			
		}
		else q2.puf(now);
	}
	printf("%d\n", ans);
	
	q1.clear(), q2.clear();
}

int main()
{
	int T; scanf("%d", &T);
	lfor (i, 1, T) work(i);
	return 0;
}

2022/11/22 22:07
加载中...