Cod sursa(job #1994772)

Utilizator trifangrobertRobert Trifan trifangrobert Data 25 iunie 2017 23:02:28
Problema Subsecventa de suma maxima Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1 kb
#include <iostream>
#include <stack>

#ifndef ONLINE_JUDGE
#include <fstream>
#include <conio.h>
#endif

using namespace std;

int n;
char s[15];
deque<int> st;
int changes = 0;

int main()
{
#ifndef ONLINE_JUDGE
	ifstream cin("test.in");
#endif
#ifdef ONLINE_JUDGE
	ios_base::sync_with_stdio(false);
#endif
	cin >> n;
	int m = 2 * n;
	int top = 1;
	cin.get();
	for (int i = 1;i <= m;i++)
	{
		cin.getline(s, 16);
		if (s[0] == 'a')
		{
			int x = 0;
			for (int j = 4;s[j];j++)
				x = x * 10 + s[j] - '0';
			st.push_back(x);
		}
		if (s[0] == 'r')
		{
			if (st.back() == top)
			{
				st.pop_back();
				top++;
			}
			else
			{
				while (st.back() != top && !st.empty())
				{
					changes++;
					st.push_front(st.back());
					st.pop_back();
				}
				top++;
				st.pop_back();
			}
		}
	}
	// pica pe exemplul 5, 3, 2, 1, 4
	// trebuie sa fac cu un vector din ala jmecher
	cout << changes;
#ifndef ONLINE_JUDGE
	cin.close();
	_getch();
#endif
	return 0;
}