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

cin的确比scanf慢,这不是OJ的问题,但是用cin也能过

Posted by bergkamp at 2009-04-29 18:08:44 on Problem 1674
#include <iostream>
#include <memory.h>
using namespace std;
int id[100005], num[100005], hash[100005];
int main()
{
	int t, n, i, r, next;
	cin>>t;
	while (t--)
	{
		cin>>n;
		r = 0;
		memset( hash, 0, sizeof(int)*100005 );
		for ( i = 1; i <= n; i++ )
		{
			cin>>num[i];
			id[num[i]] = i;
		}
		for ( i = 1; i <= n; i++ )
		{
			if ( hash[i] == 0 )
			{
				r++;
				hash[i] = 1;
				next = id[num[i]];
				while ( hash[id[next]] == 0 )
				{
					hash[id[next]] = 1;
					next = id[next];
				}
			}
		}
		cout<<n-r<<endl;
	}
	return 1;
}

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