| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
请教:Time Limit Exceeded#include <iostream>
#include <cstdlib>
using namespace std;
int n;
int *Permutation;
bool IsSequen( void )
{
for (int i=1; i<n; i++)
if ((Permutation[i] - Permutation[i-1]) != 1)
return false;
return true;
}
int FindMinEnd(int end)
{
while (end > 0)
{
if (Permutation[end] > end )
end --;
else
return end;
}
return end;
}
int FindMaxBegin(int begin,int end)
{
while ( begin < n-1)
{
if (Permutation[begin] < Permutation[end])
begin ++;
else
return begin;
}
return begin;
}
int main()
{
int t;
int i;
cin >> t;
for (int j=0; j < t; j++)
{
cin >> n;
Permutation = new int [n];
int count = 0;
for (i=0; i<n; i++)
cin >> Permutation[i];
if (IsSequen( ))
{
cout << 0 << endl;
continue;
}
else
{
int MaxBegin = 0, MinEnd = n-1;
do{
MinEnd = FindMinEnd( n - 1);
MaxBegin = FindMaxBegin ( 0 , MinEnd);
if (MaxBegin == n-1 || MinEnd == 0)
break;
else
{
int temp = 0;
temp = Permutation[MaxBegin];
Permutation[MaxBegin] = Permutation[MinEnd];
Permutation[MinEnd] = temp;
count ++;
}
if (IsSequen())
{
cout << count << endl;
break;
}
}
while (MaxBegin != MinEnd);
}
delete Permutation;
}
system("PAUSE");
return 0;
}
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator