HDU 4286 Data Handler( 数据结构,双向队列,模拟)
2014-06-17 21:07
741 查看
http://acm.hdu.edu.cn/showproblem.php?pid=4286
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2218 Accepted Submission(s): 550
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
Sample Output
Source
2012 ACM/ICPC Asia Regional Tianjin Online
直接照着题意模拟,一开始用的双向链表,T到死,reverse导致O(n×m)的复杂度,后来用双向队列记录[L,R]的元素,两边的元素可以用栈/双向队列/双向链表存,这样任何操作都只要进出容器,reverse只要记录次数,确定从哪边进出即可。
Data Handler
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2218 Accepted Submission(s): 550
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
直接照着题意模拟,一开始用的双向链表,T到死,reverse导致O(n×m)的复杂度,后来用双向队列记录[L,R]的元素,两边的元素可以用栈/双向队列/双向链表存,这样任何操作都只要进出容器,reverse只要记录次数,确定从哪边进出即可。
#include<cstdio> #include<iostream> #include<cstdlib> #include<algorithm> #include<ctime> #include<cctype> #include<cmath> #include<string> #include<cstring> #include<stack> #include<queue> #include<list> #include<vector> #include<map> #include<set> #define sqr(x) ((x)*(x)) #define LL long long #define itn int #define INF 0x3f3f3f3f #define PI 3.1415926535897932384626 #define eps 1e-10 #define mm using namespace std; deque<int> dq; stack<int> lst,rst; int res; void write() { if (res==0) { while (!lst.empty()) { dq.push_front(lst.top()); lst.pop(); } while (!rst.empty()) { dq.push_back(rst.top()); rst.pop(); } bool flag=false; while (!dq.empty()) { if (flag) printf(" "); flag=true; printf("%d",dq.front()); dq.pop_front(); } } else { while (!lst.empty()) { dq.push_back(lst.top()); lst.pop(); } while (!rst.empty()) { dq.push_front(rst.top()); rst.pop(); } bool flag=false; while (!dq.empty()) { if (flag) printf(" "); flag=true; printf("%d",dq.back()); dq.pop_back(); } } puts(""); } int main() { #ifndef ONLINE_JUDGE freopen("/home/fcbruce/文档/code/t","r",stdin); #endif // ONLINE_JUDGE int T_T; scanf("%d",&T_T); while (T_T--) { dq.clear(); res=0; int n,x; scanf("%d",&n); for (itn i=0;i<n;i++) { scanf("%d",&x); dq.push_back(x); } int l;int r; scanf("%d%d",&l,&r); for (itn i=1;i<l;i++) lst.push(dq.front()),dq.pop_front(); for (int i=n;i>r;i--) rst.push(dq.back()),dq.pop_back(); scanf("%d",&n); char op[20]; char ch; int p=0; while (n--) { scanf("%s",op); if(strcmp(op,"Reverse")==0) { res^=1; continue; } if (strcmp(op,"MoveLeft")==0) { // printf("%s\n",op); scanf(" %c",&ch); if (ch=='L') { if (res==0) { dq.push_front(lst.top()); lst.pop(); } else { dq.push_back(lst.top()); lst.pop(); } } else { if (res==0) { rst.push(dq.back()); dq.pop_back(); } else { rst.push(dq.front()); dq.pop_front(); } } continue; } if (strcmp(op,"MoveRight")==0) { // printf("%s\n",op); scanf(" %c",&ch); if (ch=='L') { if (res==0) { lst.push(dq.front()); dq.pop_front(); } else { lst.push(dq.back()); dq.pop_back(); } } else { if (res==0) { dq.push_back(rst.top()); rst.pop(); } else { dq.push_front(rst.top()); rst.pop(); } } continue; } if (strcmp(op,"Insert")==0) { // printf("%s\n",op); scanf(" %c %d",&ch,&x); if (ch=='L') { if (res==0) { dq.push_front(x); } else { dq.push_back(x); } } else { if (res==0) { dq.push_back(x); } else { dq.push_front(x); } } continue; } if (strcmp(op,"Delete")==0) { // printf("%s\n",op); scanf(" %c %d",&ch,&x); if (ch=='L') { if (res==0) { dq.pop_front(); } else { dq.pop_back(); } } else { if (res==0) { dq.pop_back(); } else { dq.pop_front(); } } continue; } } write(); } return 0; }
相关文章推荐
- HDU 4286 Data Handler (splay tree || 双向链表模拟)
- HDU 4286 Data Handler [栈,双端队列]
- HDU 4286 Data Handler --双端队列
- HDU 4286 Data Handler (双端队列)
- HDU 4286 Data Handler(12年天津网络赛-模拟)
- HDU 4286 Data Handler --双端队列
- (hdu step 8.1.6)士兵队列训练问题(数据结构,简单模拟——第一次每2个去掉1个,第二次每3个去掉1个.知道队伍中的人数<=3,输出剩下的人 )
- (hdu step 8.1.6)士兵队列训练问题(数据结构,简单模拟——第一次每2个去掉1个,第二次每3个去掉1个.知道队伍中的人数<=3,输出剩下的人 )
- 数据结构试验-银行业务队列简单模拟
- HDU 4286 Data Handler(12年天津 Splay tree)
- hdu 4286 Data Handler
- c语言版数据结构(奇迹冬瓜)-队列实战(1)离散事件模拟(银行排队)
- 数据结构作业2 ------ 用队列模拟银行排队的情况
- 数据结构03.1 队列模拟(数组)
- HDU--杭电--3415--Max Sum of Max-K-sub-sequence--队列--双向队列
- HDU--杭电--3415--Max Sum of Max-K-sub-sequence--队列--双向队列
- hdu 4286 Data Handler
- HDU 4286 Data Handler【Splay Tree】【2012年天津网络赛1009】
- 使用LinkedList模拟一个堆栈或者队列数据结构
- 用LinkedList集合模拟一个队列(先进先出)或者堆栈(先进后出)数据结构。