358C - Dima and Containers - CodeForces Solution


constructive algorithms greedy implementation *2000

Please click on ads to support us..

Python Code:

n = int(input())
last_zero, largest, nd_largest, rd_largest = -1, -1, -1, -1
arr = [int(input()) for _ in range(n)]
for i in range(n):
    if arr[i] == 0:
        filled_stk, filled_queue, filled_front = False, False, False
        for j in range(last_zero + 1, i):
            if arr[j] == largest and not filled_stk:
                filled_stk = True
                print('pushStack')
            elif arr[j] == nd_largest and not filled_queue:
                filled_queue = True
                print('pushQueue')
            elif arr[j] == rd_largest and not filled_front:
                filled_front = True
                print('pushFront')
            else:
                print('pushBack')
        print(int(filled_stk) + int(filled_queue) + int(filled_front), end='')
        if filled_stk:
            print(' popStack', end='')
        if filled_queue:
            print(' popQueue', end='')
        if filled_front:
            print(' popFront', end='')
        print('\n', end='')
        last_zero = i
        largest = nd_largest = rd_largest = -1
    else:
        if arr[i] >= largest:
            largest, nd_largest, rd_largest = arr[i], largest, nd_largest
        elif arr[i] >= nd_largest:
            nd_largest, rd_largest = arr[i], nd_largest
        elif arr[i] > rd_largest:
            rd_largest = arr[i]
for i in range(last_zero + 1, n):
    print('pushStack')

			 		    					 		 			 	 	 	 	

C++ Code:

#define _GLIBCXX_FILESYSTEM
#include <bits/stdc++.h>
using namespace std;

#define ll long long

int main()
{
	//freopen("reduce.in", "r", stdin);
    //freopen("reduce.out", "w", stdout);
	ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n,x,k=0;
	vector<pair<int,int>> v;
    cin>>n;
    string in[]={"pushStack","pushQueue","pushFront","pushBack"},out[]={"popStack","popQueue","popFront","popBack"};
    while (n--){
		cin>>x;
		if (x!=0) v.push_back({x,k++});
		else {
			if (k<=3){
				for (int i=0;i<k;i++) cout<<in[i]<<'\n';
				cout<<k;
				for (int i=0;i<k;i++) cout<<' '<<out[i];
				cout<<'\n';
				v.clear();
				k=0;
				continue;
			}
			sort(v.begin(),v.end());
			vector<int> id(k);
			for (int i=0;i<k;i++) id[v[i].second]=i;
			for (int i=0;i<k;i++){
				if (id[i]==k-3) cout<<in[2]<<'\n';
				else if (id[i]==k-2) cout<<in[1]<<'\n';
				else if (id[i]==k-1) cout<<in[0]<<'\n';
				else cout<<in[3]<<'\n';
			}
			cout<<3<<' '<<out[0]<<' '<<out[1]<<' '<<out[2]<<'\n';
			k=0;
			v.clear();
		}
    }
    while(k--) cout<<in[0]<<'\n';
}


Comments

Submit
0 Comments
More Questions

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
Cyclic shifts
Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety
Partitioning binary strings
Special sets
Smallest chosen word