题目描述
栗酱有一天在网上冲浪的时候发现了一道很有意思的数据结构题。
该数据结构形如长条形。一开始该容器为空,有以下七种操作。
1 a从前面插入元素a
2 从前面删除一个元素
3 a从后面插入一个元素
4 从后面删除一个元素
5 将整个容器头尾翻转
6 输出个数和所有元素
7 对所有元素进行从小到大排序
输入描述:
只有一组数据,第一行n≤50000,m≤200000, a≤100000 代表最大数据数目和操作次数。
接下来每一行一个操作如上描述。保证所有操作合法(不会在容器为空时删除元素)。
6、7操作共计不会超过10次。输出描述:
当执行6操作时,第一行先输出当前的个数,然后从头到尾按顺序输出,每两个元素之间用一个空格隔开,末尾不能有空格。
示例1
输入10 9
1 1
3 5
3 4
6
4
5
6
7
6输出
3
1 5 4
2
5 1
2
1 5
分析:ACM中这种要求使用数据结构的题目,stl肯定超时。。。。肯定得自己写,按照这个要求,因为头尾插入删除肯定是双向链表。
#include<bits\stdc++.h>
using namespace std;
class mySTL
{
private:
int a[100005] , head , tail;
bool lag;
public:
mySTL (int x = 50000 , int y = 50001 , bool z = 0) :head (x) , tail (y) , lag (z){};
void fisrtinsert (int x)
{
if (lag)
a[head++] = x;
else
a[head--] = x;
}
void fisrtdelete ()
{
if (lag) head--;
else head++;
}
void lastinsert (int x)
{
if (lag) a[tail--] = x;
else a[tail++] = x;
}
void lastdelet ()
{
if (lag) tail++;
else tail--;
}
void overturn ()
{
head = head ^ tail;
tail = head ^ tail;
head = head ^ tail;
lag = !lag;
}
void sort ()
{
if (lag)
{
sort (a + tail + 1 , a + head , greater<int> ());
}
else sort (a + head + 1 , a + tail);
}
void print ()
{
if (lag)
{
cout << head - tail - 1 << endl;
for (int i = head - 1; i>tail; i--)
{
cout << a[i];
i == tail + 1 ? cout << endl : cout << ' ';
}
}
else
{
cout << tail - head - 1 << endl;
for (int i = head + 1; i<tail; i++)
{
cout << a[i];
i == tail - 1 ? cout << endl : cout << ' ';
}
}
}
};
int main ()
{
ios::sync_with_stdio (false);
int n , a , temp , ew;
mySTL mySTL;
cin >> n >> a;
while (a--)
{
cin >> temp;
switch (temp)
{
case 1:
cin >> ew;
mySTL.fisrtinsert (ew);
break;
case 2:
mySTL.fisrtdelete ();
break;
case 3:
cin >> ew;
mySTL.lastinsert (ew);
break;
case 4:
mySTL.lastdelet ();
break;
case 5:
mySTL.overturn ();
break;
case 6:
mySTL.print ();
break;
default:
mySTL.sort ();
break;
}
}
}