CSES - Datatähti 2016 alku - Results
Submission details
Task:Kirjat
Sender:tomivah
Submission time:2015-10-07 21:22:39 +0300
Language:C++
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:52:9: error: use of deleted function 'std::basic_stringstream<char>& std::basic_stringstream<char>::operator=(const std::basic_stringstream<char>&)'
  stream = std::stringstream( input2.substr( cursor, input2.npos ) );
         ^
In file included from input/code.cpp:3:0:
/usr/include/c++/4.9/sstream:502:11: note: 'std::basic_stringstream<char>& std::basic_stringstream<char>::operator=(const std::basic_stringstream<char>&)' is implicitly deleted because the default definition would be ill-formed:
     class basic_stringstream : public basic_iostream<_CharT, _Traits>
           ^
/usr/include/c++/4.9/sstream:502:11: error: use of deleted function 'std::basic_iostream<char>& std::basic_iostream<char>::operator=(const std::basic_iostream<char>&)'
In file included from /usr/include/c++/4.9/iostream:40:0,
                 from input/code.cpp:1:
/usr/include/c++/4.9/istream:795:11: note: 'std::basic_iostream<char>& std::basic_iostream<...

Code

#include <iostream>
#include <string>
#include <sstream>

int main()
{
	std::ios_base::sync_with_stdio( false );

	unsigned long long bookCount;
	std::string input1;
	std::string input2;

	std::cin >> bookCount;
	std::cin.get();
	std::getline( std::cin, input1 );
	std::getline( std::cin, input2 );

	unsigned long long* sequence1 = new unsigned long long[ bookCount ];
	unsigned long long* sequence2 = new unsigned long long[ bookCount ];
	unsigned long long* sequence3 = new unsigned long long[ bookCount ];

	unsigned long long index = 0;
	unsigned long long cursor = 0;

	for ( unsigned long long i = 0; i < input1.length(); ++i )
	{
		if ( input1[ i ] == ' ' )
		{
			std::stringstream stream( input1.substr( cursor, i - cursor ) );
			stream >> sequence1[ index ];
			index++;
			cursor = i + 1;
		}
	}

	std::stringstream stream( input1.substr( cursor, input1.npos ) );
	stream >> sequence1[ index ];
	index = 0;
	cursor = 0;

	for ( unsigned long long i = 0; i < input2.length(); ++i )
	{
		if ( input2[ i ] == ' ' )
		{
			std::stringstream stream( input2.substr( cursor, i - cursor ) );
			stream >> sequence2[ index ];
			index++;
			cursor = i + 1;
		}
	}

	stream = std::stringstream( input2.substr( cursor, input2.npos ) );
	stream >> sequence2[ index ];
	sequence2[ index ] = atol( input2.substr( cursor, input2.npos ).c_str() );

	unsigned long long lowestUnread = 1;
	bool* booksRead = new bool[ bookCount ];

	for ( unsigned long long i = 0; i < bookCount; ++i )
	{
		booksRead[ i ] = false;
	}

	for ( unsigned long long i = 0; i < bookCount; ++i )
	{
		if ( lowestUnread != sequence1[ i ] && lowestUnread != sequence2[ i ] )
		{
			sequence3[ i ] = lowestUnread;
			booksRead[ lowestUnread - 1 ] = true;

			while ( lowestUnread < bookCount && booksRead[ lowestUnread - 1 ] )
			{
				lowestUnread++;
			}			
		}
		else
		{
			unsigned long long j = lowestUnread + 1;
			bool mustFlip = true;

			while ( j <= bookCount )
			{
				if ( j != sequence1[ i ] && j != sequence2[ i ] && !booksRead[ j - 1 ] )
				{
					sequence3[ i ] = j;
					booksRead[ j - 1 ] = true;

					if ( j == lowestUnread )
					{
						while ( booksRead[ lowestUnread - 1 ] )
						{
							lowestUnread++;
						}
					}

					mustFlip = false;
					break;
				}

				j++;
			}

			if ( mustFlip )
			{
				for ( unsigned long long k = 0; k < bookCount; ++k )
				{
					if ( lowestUnread != sequence1[ k ] && lowestUnread != sequence2[ k ]  &&
						sequence3[ k ] != sequence1[ i ] && sequence3[ k ] != sequence2[ i ] )
					{
						sequence3[ i ] = sequence3[ k ];
						sequence3[ k ] = lowestUnread;
						booksRead[ lowestUnread - 1 ] = true;
						break;
					}
				}
			}
		}		
	}

	for ( unsigned long long i = 0; i < bookCount; ++i )
	{
		if ( sequence3[ i ] == sequence1[ i ] || sequence3[ i ] == sequence2[ i ] )
		{
			for ( unsigned long long j = 0; j < bookCount; ++j )
			{
				if ( j != i && sequence3[ i ] != sequence1[ j ] && sequence3[ i ] != sequence2[ j ] &&
					sequence3[ j ] != sequence1[ i ] && sequence3[ j ] != sequence2[ i ] )
				{
					unsigned long long temp = sequence3[ i ];
					sequence3[ i ] = sequence3[ j ];
					sequence3[ j ] = temp;
					break;
				}
			}
		}
	}

	for ( unsigned long long i = 0; i < bookCount; ++i )
	{
		std::cout << sequence3[ i ] << " ";
	}

	delete[] sequence1;
	delete[] sequence2;
	delete[] sequence3;
	delete[] booksRead;
	return 0;
}