Help me in stacks question

anyone pls help me out …
what i am doing a mistake in this code

 #include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define umii            unordered_map<int,int >
#define umic            unordered_map<int,char >
#define umci            unordered_map<char,int >
#define usi             unordered_set<int>
#define usc             unordered_set<char>
#define mii             map<int,int >
#define mic             map<int,char >
#define mci             map<char,int >
#define si              set<int>
#define sc              set<char>
#define ll              long long
#define ff              first
#define ss              second
#define pb              push_back
#define mp              make_pair
#define pii             pair<int,int>
#define vii             vector<vector<int>>
#define vi              vector<int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int>>
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define mod             1000000007
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define mk(arr,n,type)  type *arr=new type[n];
#define w(x)            int x; cin>>x; while(x--)
void c_p_c()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}
int main()
{
	w(x)
	{
		int n ;
		cin >> n ;
		int arr[n];
		for (int i = 0 ; i < n ; i++)
			cin >> arr[i];
		multiset<int>  ms ;
		ms.insert(arr[0]);
		for (int i = 1 ; i < n ; i++ )
		{
			auto it = 	ms.upper_bound(arr[i]);
			if (*it < arr[i] )
			{
				ms.insert(arr[i]);
			}
			else
			{
				if (*it == arr[i - 1])
				{
					it++;
					if (it == ms.end())
					{
						ms.insert(arr[i]);
					}
					else
					{
							ms.erase(it);
							ms.insert(arr[i]);
					}
				}
				else
				{
					ms.erase(it);
					ms.insert(arr[i]);
				}
			}
		}
           cout<<ms.size()<<" " ; 
		for (auto x : ms )
			cout << x << " " ;
		cout << endl ;

	}

	return 0;
}

I recommend you to check the following link and rephrase your question accordingly…

1 Like