Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

题目简单,陷阱不少,贴ac码

Posted by idead at 2015-09-06 17:46:32 on Problem 1033

#include "stdio.h"
#include <iostream>
using namespace std;
void cmove(int i);
bool circle = false;
int files[10001], cur, N, maxy = 0,circleout;
int main()
{

	int K, count=1,i;
	bool optineed = false;
	memset(files, 0, sizeof(files));
	cin >> N >> K;
	for (i = 0; i < K; i++)
	{
		int S,j;
		cin >> S;
		for (j = 0; j < S; j++)
		{
			int temp;
			cin >> temp;
			temp>maxy ? maxy = temp : temp;
			files[temp] = count++;
		}
	}
	files[0] = count-1;
	for (i = 1; i <= maxy; i++)
	{
		if (files[i] != i)
		{
			optineed = true;
			if (files[i] != 0)
			{
				cur = i;
				cmove(i);
				if (circle)
				{
					cmove(circleout);
					circle = false;
				}
			}
		}
		if (!optineed&&i == files[0])
		{
			cout << "No optimization needed";
			break;
		}
	}
	return 0;
}
void cmove(int i)
{
	if (files[files[i]] == 0)
	{
		files[files[i]] = files[i];
		cout << i << " " << files[i]<<"\n";
		files[i] = 0;
	}
	else{
		if (cur == files[i])
		{
			circle = true;
			for (circleout = 1; files[circleout] != 0; circleout++){}
			files[circleout] = files[i];
			cout << i << " " << circleout << "\n";
			files[i] = 0;
		}
		else{
			cmove(files[i]);
			files[files[i]] = files[i];
			cout << i << " " << files[i] << "\n";
			files[i] = 0;
		}
	}
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator