社区讨论

双端队列 20 分求救

P9748[CSP-J 2023] 小苹果参与者 2已保存回复 2

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
2 条
当前快照
1 份
快照标识符
@mhke2osh
此快照首次捕获于
2025/11/04 17:51
4 个月前
此快照最后确认于
2025/11/04 17:51
4 个月前
查看原帖
完全依照题意模拟,为什么 WA 了?
CPP
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
deque<ll> q;
ll n;
ll ans1=-1,ans2=-1;
bool f1=false,f2=false;
void work(ll dy){
	if(q.front()==n) ans2=dy;
	q.pop_front();
	ll len=q.size();
	for(ll i = 1;i <= len/3;++i){
		ll fir,sec,thi;
		fir=q.front();
		q.pop_front();
		sec=q.front();
		q.pop_front();
		thi=q.front();
		if(thi==n) {
			ans2=dy;
		}
		q.pop_front();
		q.push_front(sec);
		q.push_front(fir);
	}	
	
	if(q.size()==0) {
		ans1=dy;
	}
}
int main(){
#ifdef LOCAL
	freopen("apple.txt","r",stdin);
	freopen("apple.txt","w",stdout);
#endif
	cin>>n;
	for(ll i = 1;i <= n;++i) q.push_back(i);
	for(ll i = 1;i <= n;++i){
		work(i);
		if(ans1!=-1&&!f1) {
			cout << ans1 << " ";
			f1=true;
		}
		if(ans2!=-1&&!f2){
			cout << ans2;
			f2=true;
		}
		if(f1&&f2) break;
	}
	return 0;
}

回复

2 条回复,欢迎继续交流。

正在加载回复...