hdu4286(vector)

Data Handler
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1854 Accepted Submission(s): 453
Problem Description
You are in charge of data in a company, so you are called "Data Handler". Different from the data in computer, the data you have are really in huge volume, and each data contains only one integer. All
the data are placed in a line from left to right. There are two "hand" to handle the data, call hand "L" and hand "R". Every hand is between two adjacent data or at the end of the data line.
In one day, the company gives you many commands to handle these data, so you should finish them one by one. At the beginning, there are N data, and hand "L" and "R" are in some positions. Each command is one the following formats:
(1)MoveLeft L/R: it means that you should move the hand "L"/"R" left one data unit;
(2)MoveRight L/R: it means that you should move the hand "L"/"R" right one data unit;
(3)Insert L X: it means that you should insert the data that contains X at the right of the hand "L";
(4)Insert R X: it means that you should insert the data that contains X at the left of the hand "R";
(5)Delete L: it means that you should delete the one data at the right of the hand "L";
(6)Delete R: it means that you should delete the one data at the left of the hand "R";
(7)Reverse: it means that you should reverse all the data between hand "L" and hand "R".
After finish all the commands, you should record all the data from left to right. So please do it.
Input
The first line contains an integer T(1<=T<=10), the number of test cases.
Then T test cases follow. For each test case, the first line contains an integer N(1<=N<=500000), the number of data at the beginning. The second line contains N integers, means the integer in each data, from left to right. The third line contains two integers
L and R (1<=L<=R<=N), the positions of hand "L" and hand "R". It means that hand "L" is at the left of the L-th data and hand "R" is at the right of the R-th data. The fourth line contains one integer M(1<=M<=500000), the number of commands. Then M lines follow,
each line contains a command in the above format. All the integers in the data will in range [-10000,10000].
It is guaranteed that there are always some data between hand "L" and "R", and if the hand is at the left/right end of the data line, it will not receive the command MoveLeft/MoveRight.
Because of large input, please use scanf instead of cin.
Output
For each test case, output the integers in the data from left to right in one line, separated in a single space.
Because of large output, please use printf instead of cout.
Sample Input
2
5
1 2 3 4 5
1 5
5
MoveLeft R
Insert R 6
Reverse
Delete R
Insert L 7
5
6536 5207 2609 6604 -4046
1 3
5
Delete L
Insert R -9221
Reverse
Delete L
MoveRight L
Sample Output
7 6 4 3 2 5
2609 5207 6604 -4046
Source
2012 ACM/ICPC Asia Regional Tianjin Online
Recommend
liuyiding
本题是个模拟题。思路还是比较简单的，我用的vector容器，终于过了测试样例，但提交上去后Runtime Error(ACCESS_VIOLATION)，这下没折了。求解啊！ #include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cstdio>
#include<cstring>
using namespace std;
/*
Runtime Error
(ACCESS_VIOLATION)
*/
vector<int>Vec;
int main()
{
int cas,n,i,tmp,l,r,m;
int Size;
char cmd[10],dec[5];
//freopen("in.txt","r",stdin);
cin>>cas;
while(cas--)
{
Vec.clear();
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&tmp);
Vec.push_back(tmp);
}
vector<int>::iterator left,right;
scanf("%d%d",&l,&r);
l--;r--;
left=Vec.begin()+l;
right=Vec.begin()+r+1;
scanf("%d",&m);
while(m--)
{
scanf("%s",cmd);
// printf("***********(left=%d,",*left);
// if(right!=Vec.end())printf("***********right=%d)",*right);
// printf("\n");
if(strcmp(cmd,"MoveLeft")==0)
{
scanf("%s",dec);
if(dec[0]=='L')left--;
else if(dec[0]=='R')right--;
}
else if(strcmp(cmd,"MoveRight")==0)
{
scanf("%s",dec);
if(dec[0]=='L')left++;
else if(dec[0]=='R')right++;
}
else if(strcmp(cmd,"Insert")==0)
{
scanf("%s%d",dec,&tmp);
if(dec[0]=='L')
{
Vec.insert(left,tmp);
right++;//
}
else if(dec[0]=='R')
{
Vec.insert(right,tmp);
right++;
}
}
else if(strcmp(cmd,"Delete")==0)
{
scanf("%s",dec);
if(dec[0]=='L')
{
Vec.erase(left);
right--;//
}
else if(dec[0]=='R')
{
Vec.erase(right-1);
}
}
else if(strcmp(cmd,"Reverse")==0)
{
reverse(left,right);
}
}
/* printf("***********(left=%d,",*left);
if(right!=Vec.end())printf("***********right=%d)",*right);
printf("\n");*/
Size=Vec.size();
for(i=0;i<Size;i++)
{
if(i!=0)printf(" %d",Vec[i]);
else printf("%d",Vec[i]);
}
printf("\n");
}
return 0;
}
在网上搜了一下，有人说用双端队列，于是自己模仿着做，终于做出来了！