2.STL Practice

A - {A} + {B} HDU - 1412 (set)

给你两个集合,要求{A} + {B}.
注:同一个集合中不会有两个相同的元素.
Input
每组输入数据分为三行,第一行有两个数字n,m(0<n,m<=10000),分别表示集合A和集合B的元素个数.后两行分别表示集合A和集合B.每个元素为不超出int范围的整数,每个元素之间有一个空格隔开.
Output
针对每组数据输出一行数据,表示合并后的集合,要求从小到大输出,每个元素之间有一个空格隔开.
Sample Input
1 2
1
2 3
1 2
1
1 2
Sample Output
1 2 3
1 2

#include <iostream>
#include <set>
using namespace std;
int main()
{
	int m,n;
	set<int> s;		//set集合,是一个内部自动有序且不含重复元素的容器
	while(cin>>n>>m)
	{
		s.clear();
		while(n+m--)
		{
			int t;
			cin>>t;
			s.insert(t);
		}
		set<int>::iterator i;	//set只能通过迭代器(iterator)访问,并用*it来访问其中的元
		i=s.begin();
		cout<<*i;
		for(i++;i!=s.end();i++)
			cout<<' '<<*i;
		cout<<endl;
	}
	return 0;
}

B - Train Problem I HDU - 1022 (stack)

As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of student want to get back to school by train(because the trains in the Ignatius Train Station is the fastest all over the world v). But here comes a problem, there is only one railway where all the trains stop. So all the trains come in from one side and get out from the other side. For this problem, if train A gets into the railway first, and then train B gets into the railway before train A leaves, train A can’t leave until train B leaves. The pictures below figure out the problem. Now the problem for you is, there are at most 9 trains in the station, all the trains has an ID(numbered from 1 to n), the trains get into the railway in an order O1, your task is to determine whether the trains can get out in an order O2.

Input
The input contains several test cases. Each test case consists of an integer, the number of trains, and two strings, the order of the trains come in:O1, and the order of the trains leave:O2. The input is terminated by the end of file. More details in the Sample Input.
Output
The output contains a string “No.” if you can’t exchange O2 to O1, or you should output a line contains “Yes.”, and then output your way in exchanging the order(you should output “in” for a train getting into the railway, and “out” for a train getting out of the railway). Print a line contains “FINISH” after each test case. More details in the Sample Output.
Sample Input
3 123 321
3 123 312
Sample Output
Yes.
in
in
in
out
out
out
FINISH
No.
FINISH

#include <iostream>
#include <stack>
//stack翻译为栈,是STL中实现的一个后进先出的容器,
//只能通过top()来访问栈顶元素。
#include <cstring>
using namespace std;
int main()
{
	int n;
	string s1,s2;
	while(cin>>n>>s1>>s2)
	{
		stack<char> s;	
		int k=0;
		int flag[111];	//flag记录进出状态 
		int step=0;
		for(int i=0;i<n;i++)
		{
		   s.push(s1[i]);	//向栈中丢入一个元素 
		   flag[step++]=1;		//将flag标记为1,即进入火车站 
//		   cout<<'i'<<endl;
		   while(!s.empty()&&s.top()==s2[k])	//如果栈顶与O2标号相同 
		   {
		   		s.pop();	//将栈顶pop出去 
		   		flag[step++]=0;		
//		   		cout<<'o'<<endl;
		   		k++;	//遍历O2 
		   }
		}	
		if(s.empty())
		{
			cout<<"Yes."<<endl;
			for(int i=0;i<step;i++)
			{
				if(flag[i])	cout<<"in"<<endl;
				else	cout<<"out"<<endl;
			}
		}
		else	cout<<"No."<<endl;
		cout<<"FINISH"<<endl;
	}
	return 0;
}

C - Alice, Bob and Candies CodeForces - 1352D (deque)

There are n candies in a row, they are numbered from left to right from 1 to n. The size of the i-th candy is ai.

Alice and Bob play an interesting and tasty game: they eat candy. Alice will eat candy from left to right, and Bob — from right to left. The game ends if all the candies are eaten.

The process consists of moves. During a move, the player eats one or more sweets from her/his side (Alice eats from the left, Bob — from the right).

Alice makes the first move. During the first move, she will eat 1 candy (its size is a1). Then, each successive move the players alternate — that is, Bob makes the second move, then Alice, then again Bob and so on.

On each move, a player counts the total size of candies eaten during the current move. Once this number becomes strictly greater than the total size of candies eaten by the other player on their previous move, the current player stops eating and the move ends. In other words, on a move, a player eats the smallest possible number of candies such that the sum of the sizes of candies eaten on this move is strictly greater than the sum of the sizes of candies that the other player ate on the previous move. If there are not enough candies to make a move this way, then the player eats up all the remaining candies and the game ends.

For example, if n=11 and a=[3,1,4,1,5,9,2,6,5,3,5], then:

move 1: Alice eats one candy of size 3 and the sequence of candies becomes [1,4,1,5,9,2,6,5,3,5].
move 2: Alice ate 3 on the previous move, which means Bob must eat 4 or more. Bob eats one candy of size 5 and the sequence of candies becomes [1,4,1,5,9,2,6,5,3].
move 3: Bob ate 5 on the previous move, which means Alice must eat 6 or more. Alice eats three candies with the total size of 1+4+1=6 and the sequence of candies becomes [5,9,2,6,5,3].
move 4: Alice ate 6 on the previous move, which means Bob must eat 7 or more. Bob eats two candies with the total size of 3+5=8 and the sequence of candies becomes [5,9,2,6].
move 5: Bob ate 8 on the previous move, which means Alice must eat 9 or more. Alice eats two candies with the total size of 5+9=14 and the sequence of candies becomes [2,6].
move 6 (the last): Alice ate 14 on the previous move, which means Bob must eat 15 or more. It is impossible, so Bob eats the two remaining candies and the game ends.
Print the number of moves in the game and two numbers:

a — the total size of all sweets eaten by Alice during the game;
b — the total size of all sweets eaten by Bob during the game.
Input
The first line contains an integer t (1≤t≤5000) — the number of test cases in the input. The following are descriptions of the t test cases.

Each test case consists of two lines. The first line contains an integer n (1≤n≤1000) — the number of candies. The second line contains a sequence of integers a1,a2,…,an (1≤ai≤1000) — the sizes of candies in the order they are arranged from left to right.

It is guaranteed that the sum of the values of n for all sets of input data in a test does not exceed 2⋅105.

Output
For each set of input data print three integers — the number of moves in the game and the required values a and b.

Example
Input

7
11
3 1 4 1 5 9 2 6 5 3 5
1
1000
3
1 1 1
13
1 2 3 4 5 6 7 8 9 10 11 12 13
2
2 1
6
1 1 1 1 1 1
7
1 1 1 1 1 1 1
Output
6 23 21
1 1000 0
2 1 2
6 45 46
2 2 1
3 4 2
4 4 3
AC代码

#include <iostream>
#include <deque>
using namespace std;
deque<int> dq;  //双端队列,能够高效地插入和删除容器的尾部和头部元素。
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		dq.clear();
		int n;
		cin>>n;
		int x;
		while(n--)
		{
			cin>>x;
			dq.push_back(x);
		}
		int last=0,sum=0;	// last记录上一位吃的糖果总数,sum记录自己本次移动吃的糖果个数 
		int vis=1,step=0;	//vis用于判断该谁吃糖果,step记录进行了多少次移动
		int a=0,b=0;	//吃糖果的总数 
		while(!dq.empty())	//只要队列非空,就一直循环 
		{
			if(vis&&!dq.empty()) //Alice先移动 
			{
				while(sum<=last)	//当Alice这次移动吃的糖果总数小于Bob上次移动吃的糖果总数 
				{
					sum+=dq.front();	//Alice从队列左端吃 
					dq.pop_front();
					if(dq.empty())	break;
				}
				a+=sum;
				last=sum;
				sum=0;
				vis=0;
				step++;
			}
			if(!vis&&!dq.empty())
			{
				while(sum<=last)
				{
					sum+=dq.back();
					dq.pop_back();
					if(dq.empty())	break;
				}
				b+=sum;
				last=sum;
				sum=0;
				vis=1;
				step++;
			}
		}
		cout<<step<<' '<<a<<' '<<b<<endl;
	}
	return 0;
}

D - Let the Balloon Rise HDU - 1004 (map)

Input
Input contains multiple test cases. Each test case starts with a number N (0 < N <= 1000) – the total number of balloons distributed. The next N lines contain one color each. The color of a balloon is a string of up to 15 lower-case letters.

A test case with N = 0 terminates the input and this test case is not to be processed.
Output
For each case, print the color of balloon for the most popular problem on a single line. It is guaranteed that there is a unique solution for each test case.
Sample Input
5
green
red
blue
red
red
3
pink
orange
pink
0
Sample Output
red
pink
AC代码

#include <iostream>
#include <map>
//map,关联容器,主要用于建立一种映射关系。
//first为关键字(key),每个关键字只能在map中出现一次;
//second为该关键字的值(value);
#include <string>
using namespace std;
int main()
{
	int n;
	map<string,int> mp;
	while(cin>>n)
	{
		if(n==0)	break;
		mp["green"]=0;
		mp["red"]=0;
		mp["blue"]=0;
		mp["pink"]=0;
		mp["orange"]=0;
		//每一种颜色的初值为0 
		//mp[key]=value
		string s;
		while(n--)
		{
			cin>>s;
			mp[s]++;
		}
		map<string,int>::iterator i;	//map的迭代器 
		int max=0;
		string ans;
		for(i=mp.begin();i!=mp.end();i++)	//遍历mp 
		{
			if(i->second>max)
			{
				max=i->second;	//key
				ans=i->first;	//value
			}	
		}
		cout<<ans<<endl;
	}
	return 0;
}

E - 全排列 51Nod - 1384

给出一个字符串S(可能有重复的字符),按照字典序从小到大,输出S包括的字符组成的所有排列。例如:S = “131”,
输出为:
113
131
311
Input
输入一个字符串S(S的长度 <= 9,且只包括0 - 9的阿拉伯数字)
Output
输出S所包含的字符组成的所有排列
AC代码

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
int main()
{
	char s[11];
	cin>>s;
	int n=strlen(s);
	sort(s,s+n);	//先排序 
/*
	for(int i=0;i<n;i++)
		cout<<s[i];
	cout<<endl;
	while(next_permutation(s,s+n))
	{
		for(int i=0;i<n;i++)
			cout<<s[i];
		cout<<endl;
	}
*/
	do{
		for(int i=0;i<n;i++)
			cout<<s[i];
		cout<<endl;
	}while(next_permutation(s,s+n));	//求当前排列的下一个排列
	return 0;
}

F - 水果 HDU - 1263 (map嵌套)

Input
第一行正整数N(0<N<=10)表示有N组测试数据.
每组测试数据的第一行是一个整数M(0<M<=100),表示工有M次成功的交易.其后有M行数据,每行表示一次交易,由水果名称(小写字母组成,长度不超过80),水果产地(小写字母组成,长度不超过80)和交易的水果数目(正整数,不超过100)组成.
Output
对于每一组测试数据,请你输出一份排版格式正确(请分析样本输出)的水果销售情况明细表.这份明细表包括所有水果的产地,名称和销售数目的信息.水果先按产地分类,产地按字母顺序排列;同一产地的水果按照名称排序,名称按字母顺序排序.
两组测试数据之间有一个空行.最后一组测试数据之后没有空行.
Sample Input
1
5
apple shandong 3
pineapple guangdong 1
sugarcane guangdong 1
pineapple guangdong 3
pineapple guangdong 1
Sample Output
guangdong
|----pineapple(5)
|----sugarcane(1)
shandong
|----apple(3)

#include <iostream>
#include <map>
#include <cstring>
#include <algorithm>
#include  <cstdio>
using namespace std;
int main()
{
	int n;
	cin>>n;
	map<string,map<string,int> > mp;	//map的嵌套 
	while(n--)
	{
		int m;
		cin>>m;
		string s1,s2;
		int t;
		mp.clear();
		while(m--)
		{
			cin>>s1>>s2>>t;
			mp[s2][s1]+=t;
		}
		map<string,map<string,int> >::iterator i;
		for(i=mp.begin();i!=mp.end();i++)
		{
			cout<<i->first<<endl;
			map<string,int>::iterator j;
			for(j=i->second.begin();j!=i->second.end();j++)
				cout<<"   |----"<<j->first<<"("<<j->second<<")"<<endl;
		}
		if(n>0)	cout<<endl; 
	}
	return 0;
}

G - Running Median HDU - 3282(优先队列)

For this problem, you will write a program that reads in a sequence of 32-bit signed integers. After each odd-indexed value is read, output the median (middle value) of the elements received so far.
Input
The first line of input contains a single integer P, (1 ≤ P ≤ 1000), which is the number of data sets that follow. The first line of each data set contains the data set number, followed by a space, followed by an odd decimal integer M, (1 ≤ M ≤ 9999), giving the total number of signed integers to be processed.
The remaining line(s) in the dataset consists of the values, 10 per line, separated by a single space.
The last line in the dataset may contain less than 10 values.
Output
For each data set the first line of output contains the data set number, a single space and the number of medians output (which should be one-half the number of input values plus one). The output medians will be on the following lines, 10 per line separated by a single space. The last line may have less than 10 elements, but at least 1 element. There should be no blank lines in the output.
Sample Input
3
1 9
1 2 3 4 5 6 7 8 9
2 9
9 8 7 6 5 4 3 2 1
3 23
23 41 13 22 -3 24 -31 -11 -8 -7
3 5 103 211 -311 -45 -67 -73 -81 -99
-33 24 56
Sample Output
1 5
1 2 3 4 5
2 5
9 8 7 6 5
3 12
23 23 22 22 13 3 5 5 3 -3
-7 -3
AC代码

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main()
{
	int n;
	cin>>n;
	while(n--)
	{	
		priority_queue<int,vector<int>,greater<int> > gq;	//升序序列 ,小先出 123->23->3 
		priority_queue<int,vector<int>,less<int> > lq;	//降序序列 ,大先出
		
		int num,k;
		cin>>num>>k;
		
		cout<<num<<" "<<(k+1)/2<<endl;
		//队列,先进先出 
 
		int cnt=0;
		int x;
		for(int i=1;i<=k;i++)
		{
			
			cin>>x;
			if(i==1)	gq.push(x);	
			else
			{
				if(x<gq.top())	lq.push(x);
				else	gq.push(x);
			}
			
			if((int)lq.size()-(int)gq.size()>=1)
			{
				int temp=lq.top();
				gq.push(temp);
				lq.pop();
			}
			if((int)gq.size()-(int)lq.size()>1)
			{
				int temp=gq.top();
				lq.push(temp);
				gq.pop();
			}
			if(i%2==1)	
			{
				cnt++;
				cout<<gq.top();
			
				if(cnt%10==0)	cout<<endl;
				else if(i!=k)	cout<<" ";
				else	cout<<endl;
			}	
		}
	}
	return 0;
}

H - 稳定排序 HDU - 1872

大家都知道,快速排序是不稳定的排序方法。
如果对于数组中出现的任意a[i],aj,其中a[i]==a[j],在进行排序以后a[i]一定出现在a[j]之前,则认为该排序是稳定的。

某高校招生办得到一份成绩列表,上面记录了考生名字和考生成绩。并且对其使用了某排序算法按成绩进行递减排序。现在请你判断一下该排序算法是否正确,如果正确的话,则判断该排序算法是否为稳定的。
Input
本题目包含多组输入,请处理到文件结束。
对于每组数据,第一行有一个正整数N(0<N<300),代表成绩列表中的考生数目。
接下来有N行,每一行有一个字符串代表考生名字(长度不超过50,仅包含’a’~‘z’),和一个整数代表考生分数(小于500)。其中名字和成绩用一个空格隔开。
再接下来又有N行,是上述列表经过某排序算法以后生成的一个序列。格式同上。
Output
对于每组数据,如果算法是正确并且稳定的,就在一行里面输出"Right"。如果算法是正确的但不是稳定的,就在一行里面输出"Not Stable",并且在下面输出正确稳定排序的列表,格式同输入。如果该算法是错误的,就在一行里面输出"Error",并且在下面输出正确稳定排序的列表,格式同输入。

注意,本题目不考虑该排序算法是错误的,但结果是正确的这样的意外情况。
Sample Input
3
aa 10
bb 10
cc 20
cc 20
bb 10
aa 10
3
aa 10
bb 10
cc 20
cc 20
aa 10
bb 10
3
aa 10
bb 10
cc 20
aa 10
bb 10
cc 20
Sample Output
Not Stable
cc 20
aa 10
bb 10
Right
Error
cc 20
aa 10
bb 10
AC代码

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
struct student
{
	int num;
	char name[55];
	int score;
}p1[333],p2[333];
bool cmp(student s1,student s2)
{
	if(s1.score!=s2.score)	return s1.score>s2.score;
	else	return s1.num<s2.num;
}
int main()
{
	int n;
	while(cin>>n)
	{
		for(int i=0;i<n;i++)
		{
			p1[i].num=i;
			cin>>p1[i].name>>p1[i].score;
		}	
		for(int i=0;i<n;i++)	cin>>p2[i].name>>p2[i].score;
		sort(p1,p1+n,cmp);
		int right=1,stable=1;
		for(int i=0;i<n;i++)
		{
			if(p1[i].score!=p2[i].score)//将稳定排序的成绩与案例比较 
			{
				right=0;
				break;
				//如果成绩不同一定是排序错误 ,可以直接break 
			}
			if(strcmp(p1[i].name,p2[i].name)!=0)
			{
				stable=0;
				//注意这里不能break出去,二者成绩相同名字不同时
				//可能是不稳定也可能是排序错误  
			}
		}
		if(right&&stable)	cout<<"Right"<<endl;
		else
		{
			if(!right)	cout<<"Error"<<endl;
			else	cout<<"Not Stable"<<endl;
			for(int i=0;i<n;i++)
			{
				cout<<p1[i].name<<" "<<p1[i].score<<endl;
			}
		}
	}
	return 0;
}

I - Constructing the Array CodeForces - 1353D

You are given an array a of length n consisting of zeros. You perform n actions with this array: during the i-th action, the following sequence of operations appears:

Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one;
Let this segment be [l;r]. If r−l+1 is odd (not divisible by 2) then assign (set) a[l+r2]:=i (where i is the number of the current action), otherwise (if r−l+1 is even) assign (set) a[l+r−12]:=i.
Consider the array a of length 5 (initially a=[0,0,0,0,0]). Then it changes as follows:

Firstly, we choose the segment [1;5] and assign a[3]:=1, so a becomes [0,0,1,0,0];
then we choose the segment [1;2] and assign a[1]:=2, so a becomes [2,0,1,0,0];
then we choose the segment [4;5] and assign a[4]:=3, so a becomes [2,0,1,3,0];
then we choose the segment [2;2] and assign a[2]:=4, so a becomes [2,4,1,3,0];
and at last we choose the segment [5;5] and assign a[5]:=5, so a becomes [2,4,1,3,5].
Your task is to find the array a of length n after performing all n actions. Note that the answer exists and unique.

You have to answer t independent test cases.

Input
The first line of the input contains one integer t (1≤t≤104) — the number of test cases. Then t test cases follow.

The only line of the test case contains one integer n (1≤n≤2⋅105) — the length of a.

It is guaranteed that the sum of n over all test cases does not exceed 2⋅105 (∑n≤2⋅105).

Output
For each test case, print the answer — the array a of length n after performing n actions described in the problem statement. Note that the answer exists and unique.

Example
Input

6
1
2
3
4
5
6
Output
1
1 2
2 1 3
3 1 2 4
2 4 1 3 5
3 4 1 5 2 6
AC代码

#include <iostream>
#include <queue>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
const int N=2e5+10;
struct node
{
	int l,r;
	//运算符重载 
	bool operator<(const node & o)const		//重载小于号 
	{
		if(r-l==o.r-o.l)	return l>o.r;	//如果两段区间长度相等,先排左边的 
		else	return r-l<o.r-o.l;		//否则,先输出长度大的 
	}
};
priority_queue<node> q;	//大根堆,大先出 
int cnt[N];
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		cin>>n;
		int l=1,r=n;
		q.push({l,r});
		memset(cnt,0,sizeof(cnt));
		int t=1;
		while(!q.empty())
		{
			l=q.top().l;
			r=q.top().r;
			int m=(l+r)/2;
			cnt[m]=t;
			t++;
			q.pop();
			if(l!=r)
			{
				if(m==l)
					q.push({m+1,r});	//这个时候cnt[m]已经操作过了,不需要再push({m,m}) 
				else
				{
					q.push({l,m-1});
					q.push({m+1,r});
				}
			}
		}
		cout<<cnt[1];
		for(int i=2;i<=n;i++)
			cout<<" "<<cnt[i];
		cout<<endl;
	}
	return 0;
}

J - What Are You Talking About HDU - 1075

Input
The problem has only one test case, the test case consists of two parts, the dictionary part and the book part. The dictionary part starts with a single line contains a string “START”, this string should be ignored, then some lines follow, each line contains two strings, the first one is a word in English, the second one is the corresponding word in Martian’s language. A line with a single string “END” indicates the end of the directory part, and this string should be ignored. The book part starts with a single line contains a string “START”, this string should be ignored, then an article written in Martian’s language. You should translate the article into English with the dictionary. If you find the word in the dictionary you should translate it and write the new word into your translation, if you can’t find the word in the dictionary you do not have to translate it, and just copy the old word to your translation. Space(’ ‘), tab(’\t’), enter(’\n’) and all the punctuation should not be translated. A line with a single string “END” indicates the end of the book part, and that’s also the end of the input. All the words are in the lowercase, and each word will contain at most 10 characters, and each line will contain at most 3000 characters.
Output
In this problem, you have to output the translation of the history book.
Sample Input
START
from fiwo
hello difh
mars riwosf
earth fnnvk
like fiiwj
END
START
difh, i’m fiwo riwosf.
i fiiwj fnnvk!
END
Sample Output
hello, i’m from mars.
i like earth!
AC代码

#include <iostream>
#include <cstring>
#include <map>
#include <algorithm>
using namespace std;
map<string,string> mp;
string s1,s2,s;
int judge(char c)
{
	if(c>='a'&&c<='z')	return 1;
	else return 0;
}
int main()
{
	while(cin>>s1)
	{
		if(s1=="START")	continue;
		if(s1=="END")	break;
		cin>>s2;
		mp[s2]=s1;
	}
	getchar(); //cin读不入回车,需要加个getchar读入回车,否则会直接getline一行空白 
	while(getline(cin,s))
	{
		if(s=="START")	continue;
		if(s=="END")	break;
		int i=0;
		while(i<s.size())
		{
			if(judge(s[i]))
			{
				string str="";
				while(judge(s[i]))
				{
					str=str+s[i];
					i++;
				}
				if(mp[str]!="")	cout<<mp[str];
				else	cout<<str;
			}
			else
			{
				cout<<s[i];
				i++;
			}	
		}
		cout<<endl;
	}
	return 0;
}

K - 度度熊学队列 HDU - 6375

度度熊正在学习双端队列,他对其翻转和合并产生了很大的兴趣。

初始时有 N 个空的双端队列(编号为 1 到 N ),你要支持度度熊的 Q 次操作。

①1 u w val 在编号为 u 的队列里加入一个权值为 val 的元素。(w=0 表示加在最前面,w=1 表示加在最后面)。

②2 u w 询问编号为 u 的队列里的某个元素并删除它。( w=0 表示询问并操作最前面的元素,w=1 表示最后面)

③3 u v w 把编号为 v 的队列“接在”编号为 u 的队列的最后面。w=0 表示顺序接(队列 v 的开头和队列 u 的结尾连在一起,队列v 的结尾作为新队列的结尾), w=1 表示逆序接(先将队列 v 翻转,再顺序接在队列 u 后面)。且该操作完成后,队列 v 被清空。
Input
有多组数据。

对于每一组数据,第一行读入两个数 N 和 Q。

接下来有 Q 行,每行 3~4 个数,意义如上。

N≤150000,Q≤400000

1≤u,v≤N,0≤w≤1,1≤val≤100000

所有数据里 Q 的和不超过500000
Output
对于每组数据的每一个操作②,输出一行表示答案。

注意,如果操作②的队列是空的,就输出−1且不执行删除操作。
Sample Input
2 10
1 1 1 23
1 1 0 233
2 1 1
1 2 1 2333
1 2 1 23333
3 1 2 1
2 2 0
2 1 1
2 1 0
2 1 1
Sample Output
23
-1
2333
233
23333

//cin会超时
#include <iostream>
#include <deque>
#include <algorithm>
#include <cstdio>
using namespace std;
int main()
{
	int n,q;
	while(scanf("%d%d",&n,&q)!=EOF)
	{
		
		deque<int> dq[n+10];
		while(q--)
		{
			int x;
			int u,v,w,val;
			scanf("%d",&x);
			if(x==1)
			{
				scanf("%d%d%d",&u,&w,&val);
				if(!w)	dq[u].push_front(val);
				else	dq[u].push_back(val);
			}
			else if(x==2)
			{
				scanf("%d%d",&u,&w);
				if(!dq[u].empty())
				{
					if(!w)
					{
						printf("%d\n",dq[u].front());
						dq[u].pop_front();
					}	
					else
					{
						printf("%d\n",dq[u].back());
						dq[u].pop_back();
					}	
				}
				else	printf("-1\n");
			}
			else
			{
				scanf("%d%d%d",&u,&v,&w);
				if(!w)
				{
					while(!dq[v].empty())
					{
						dq[u].push_back(dq[v].front());
						dq[v].pop_front();
					}
				}
				else
				{
					while(!dq[v].empty())
					{
						dq[u].push_back(dq[v].back());
						dq[v].pop_back();
					}
				}
			}
		}
		
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值