谈谈贪吃蛇
2026-04-11 17:17:02
发布于:浙江
#include<bits/stdc++.h>
using namespace std;
int read() {
char ch=getchar();
int f=1,x=0;
while(ch<'0'||ch>'9') {
if(ch=='-')
f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9') {
x=x10+ch-'0';
ch=getchar();
}
return fx;
}
const int maxn=1e6+10;
int n;
struct snakes {
int id,len;
bool operator <(snakes b) const {
if(len!=b.len) {
return len<b.len;
}
return id<b.id;
}
snakes operator -(snakes b) {
snakes ret;
ret.len=len-b.len;
ret.id=id;
return ret;
}
}a[maxn];
deque<snakes> q1,q2,q;
void work() {
q1.clear();
q2.clear();//记得多组数据初始化
q.clear();
for (int i = 1; i <= n; ++i) {
q1.push_back(a[i]);
}
while(1) {
if(q1.size()+q2.size()==2) {
printf("1\n"); //还剩下 2 条蛇直接输出
return ;
}
snakes st=q1.front();
q1.pop_front();
snakes ed=q1.back();
if(!q2.empty()&&ed<q2.back()) {
ed=q2.back();
q2.pop_back();//如果 q2 中的蛇较长,取 q2 中的蛇
}
else {
q1.pop_back();
}
snakes tmp;
tmp.len=ed.len-st.len;
tmp.id=ed.id;
if(q1.front()<tmp) {
q2.push_front(tmp);
}//将新蛇根据单调性放入队列中
else {
q1.push_front(tmp);
break;
}
这里空空如也


















有帮助,赞一个