546C - Soldier and Cards - CodeForces Solution


brute force dfs and similar games *1400

Please click on ads to support us..

Python Code:

n = int(input())

d1 = [int(x) for x in input().split()][1:]
d2 = [int(x) for x in input().split()][1:]

n_fights = 0
tolerance = 1000000

for i in range(tolerance):
	c1 = d1.pop(0)
	c2 = d2.pop(0)
	if c1 > c2:
		d1 += [c2, c1]
	else:
		d2 += [c1, c2]
	n_fights += 1
	if not d1 or not d2:
		break

if not d1:
	print(n_fights, 2)
elif not d2:
	print(n_fights, 1)
else:
	print(-1)

C++ Code:

#include<bits/stdc++.h>
using namespace std;
// queue<int>st1,st2;
queue<int> insertToBottom(queue<int>s,int data){
	queue<int>temp;
	while(!s.empty()){
		temp.push(s.front());
		s.pop();
	}
	s.push(data);
	while(!temp.empty()){
		s.push(temp.front());
		temp.pop();
	}
	return s;
}
int32_t main(){
	int n;	cin >> n;
	int k1;	cin >> k1;
	queue<int>st1;
	for(int i=0;i<k1;i++){
		int x;	cin >> x;
		st1.push(x);
	}
	int k2;	cin >> k2;
	queue<int>st2;
	for(int i=0;i<k2;i++){
		int x;	cin >> x;
		st2.push(x);
	}
	int cunt = 0;
	while(1){
		if(st1.size()==0 || st2.size()==0) break;
	 	int x = st1.front();
	 	st1.pop();
	 	int y = st2.front();
	 	st2.pop();
	 	if(x>y){
	 		// st1 = insertToBottom(st1,x);
	 		// st1 = insertToBottom(st1,y);
			st1.push(y);
			st1.push(x);
	 		cunt++;
	 	}
	 	else if(x<y){
	 		// st2 = insertToBottom(st2,y);
	 		// st2 = insertToBottom(st2,x);
			st2.push(x);
			st2.push(y);
	 		cunt++;
	 	}
		if(cunt>=n*n*n){
			cunt = -1;
			break;
		}
	 }
	//  cout << st1.size() << " " << st2.size() << endl;
	//  while(!st2.empty()){
	//  	cout << st2.front() << " ";
	//  	st2.pop();
	//  }
	 if(cunt!=0) cout << cunt << " ";
	//  else cout << -1 << " ";
	 if(st1.size()!=0 && cunt!=-1) cout << 1 << endl;
	 else if(cunt!=-1) cout << 2 << endl;
}
		  		  		 	    	    	   			  	


Comments

Submit
0 Comments
More Questions

62. Unique Paths
50. Pow(x, n)
43. Multiply Strings
34. Find First and Last Position of Element in Sorted Array
33. Search in Rotated Sorted Array
17. Letter Combinations of a Phone Number
5. Longest Palindromic Substring
3. Longest Substring Without Repeating Characters
1312. Minimum Insertion Steps to Make a String Palindrome
1092. Shortest Common Supersequence
1044. Longest Duplicate Substring
1032. Stream of Characters
987. Vertical Order Traversal of a Binary Tree
952. Largest Component Size by Common Factor
212. Word Search II
174. Dungeon Game
127. Word Ladder
123. Best Time to Buy and Sell Stock III
85. Maximal Rectangle
84. Largest Rectangle in Histogram
60. Permutation Sequence
42. Trapping Rain Water
32. Longest Valid Parentheses
Cutting a material
Bubble Sort
Number of triangles
AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments