当前位置:网站首页>HDU - 7072 double ended queue + opposite top
HDU - 7072 double ended queue + opposite top
2022-06-23 01:24:00 【Lupin123123】
Topic link
Top two end queue .
Insert from the left to the left deque in , Insert from the right to the right deque in , And maintain in the process lsize=rsize or lsize+1=rsize.
tips: This treatment of the top is in This question It also uses .
#include<bits/stdc++.h>
#define FAST ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define INF 0x3f3f3f3f
typedef long long ll;
const int maxn = 1e7+5;
using namespace std;
deque<int> lef,rig;
int n,x,lsize,rsize;
int bel[maxn],del[maxn];
char opt[2];
void work()
{
while(lsize!=rsize && lsize+1!=rsize)
{
if (lsize<rsize && lsize+1<rsize)
{
int tmp=rig.front();
if (del[tmp])
{
rig.pop_front();
continue;
}
rig.pop_front();
lef.push_back(tmp);
bel[tmp]=0;
rsize--, lsize++;
}
else if (lsize>rsize && lsize+1>rsize)
{
int tmp=lef.back();
if (del[tmp])
{
lef.pop_back();
continue;
}
lef.pop_back();
rig.push_front(tmp);
bel[tmp]=1;
rsize++, lsize--;
}
}
}
//void print()
//{
// printf("***\n");
// for (deque<int> ::iterator it=lef.begin(); it!=lef.end(); it++) printf("%d ",*it);
// printf("\n");
// for (deque<int> ::iterator it=rig.begin(); it!=rig.end(); it++) printf("%d ",*it);
// printf("\n");
// for (deque<int> ::iterator it=lef.begin(); it!=lef.end(); it++) printf("%d ",*it);
// for (deque<int> ::iterator it=rig.begin(); it!=rig.end(); it++) printf("%d ",*it);
// printf("\n***\n");
//}
int main()
{
while(~scanf("%d",&n))
{
x=0;
while(!lef.empty()) lef.pop_front();
while(!rig.empty()) rig.pop_front();
for (int i=1; i<=n; i++)
{
scanf("%s",opt);
if (opt[0]=='L')
{
++x;
lef.push_front(x);
bel[x]=0;
lsize++;
work();
}
else if (opt[0]=='R')
{
++x;
rig.push_back(x);
bel[x]=1;
rsize++;
work();
}
else if (opt[0]=='G')
{
int num;
scanf("%d",&num);
del[num]=1;
if (bel[num]==0) lsize--;
else rsize--;
work();
}
else
{
int ans;
while(del[rig.front()]) rig.pop_front();
ans=rig.front();
printf("%d\n",ans);
}
// print();
}
}
return 0;
}
边栏推荐
- Vector 1 (classes and objects)
- 【机器学习-西瓜书】更文挑战【Day1】:1.1 引言
- [Title Fine brushing] 2023 Hesai FPGA
- Get the start and end dates of the current week
- Overview of visual object detection technology based on deep learning
- leetcode 91. Decode ways (medium)
- You can also do NLP (classification)
- SAP ui5 application development tutorial 103 - how to consume the trial version of the third-party library in SAP ui5 applications
- OSPF综合实验
- Pat class A - 1012 the best rank (PIT)
猜你喜欢

SQL programming task02 job - basic query and sorting

BGP联邦综合实验

Ros2 summer school 2022 transfer-

62. different paths

Day367: valid complete square

How about precious metal spot silver?

Quelle est la structure et la façon dont les données sont stockées dans la base de données?

OSPF综合实验

How to solve the problem that easycvr does not display the interface when RTMP streaming is used?

The road of architects starts from "storage selection"
随机推荐
B tree and b+ tree
Pat class a 1016 phone bills (time difference)
魔王冷饭||#099 魔王说西游;老板的本质;再答中年危机;专业选择
Requête linq
Const defined variables and for of and for in in JS
3D打印微组织
Graphite statsd interface data format description
The longest child sequence of the 2019 Blue Bridge Cup
Js--- SVG to png
Hierarchy selector
BGP federal comprehensive experiment
Is it safe for Hongyuan futures to open an account? Can Hongyuan futures company reduce the handling fee?
Shell 查看帮助
[sliding window] leetcode992 Subarrays with K Different Integers
Project directory navigation
Flink synchronizes MySQL data to es
JS to read the picture of the clipboard
Day575: divide candy
MySQL-Seconds_ behind_ Master accuracy error
“听觉”营销价值凸显,喜马拉雅迎来新局点