Cod sursa(job #3001118)

Utilizator alexdmnDamian Alexandru alexdmn Data 13 martie 2023 11:33:55
Problema Tije Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.48 kb
#include <fstream>
#include <stack>

using namespace std;
stack <int> v[1005];
int f[1005][1005];
int main()
{
	ifstream cin("tije.in");
	ofstream cout("tije.out");

		int n, st=1, ok=0;
		cin>>n;
		int cnt=n;

		for(int i=1;i<=n;i++)
		{
			for(int j=0;j<n;j++)
			{
				v[i].push(i);
				f[i][v[i].top()]++;
			}

		}

		while(cnt>1)
		{
			for(int i=n;i>0;i--)
			{
				if(i<st)
				{
					v[n+1].push(v[n-(st-i)].top());
					f[n-(st-i)][v[n-(st-i)].top()]--;
					f[n+1][v[n+1].top()]++;
					v[n-(st-i)].pop();
					cout<<n-(st-i)<<" "<<n+1<<'\n';
				}
				else
				{
					v[n+1].push(v[i].top());
					f[i][v[i].top()]--;
					f[n+1][v[n+1].top()]++;
					v[i].pop();
					cout<<i<<" "<<n+1<<'\n';
				}

			}
			for(int i=st+1;i<n+st;i++)
			{
				if(i>n)
				{
					v[st+(i-n)].push(v[st].top());
					f[st][v[st].top()]--;
					f[st+(i-n)][v[st+(i-n)].top()]++;
					v[st].pop();
					cout<<st<<" "<<st+(n-1)<<'\n';
				}
				else
				{
					v[i].push(v[st].top());
					f[st][v[st].top()]--;
					f[i][v[i].top()]++;
					v[st].pop();
					cout<<st<<" "<<i<<'\n';
				}
			}

			while(!v[n+1].empty())
			{
				v[st].push(v[n+1].top());
				f[n+1][v[n+1].top()]--;
				f[st][v[st].top()]++;
				v[n+1].pop();
				cout<<n+1<<" "<<st<<'\n';
			}

			ok=0;
			for(int i=1;i<=n;i++)
			{
				if(f[st][i]!=1)
				{
					ok=1;
					break;
				}
			}
			if(ok==0)
			{
				cnt--;
				st++;
			}
		}


    return 0;
}