| ||||||||||
| 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 | |||||||||
TLE 3次 用的字典序+sort 超时的根本原因是cin 用scanf就过了//============================================================================
//
// File : poj1833.cpp
// Author : flowertree
// Time : 2015年9月19日
// About : 全排列问题
//
//============================================================================
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
using namespace std;
int a[1200];
void swap(int &m,int &n)
{
int temp;
temp = m;
m = n;
n = temp;
}
void pailie(int n,int k)
{
bool flag;
int temp;
int b[1200];
while(k--)
{
flag = false;
for(int i = n - 2; i >= 0&&(flag == false); i--)
{
if(a[i] < a[i + 1])
{
flag = true;
for(int j = n - 1; j > i; j--)
{
if(a[j] > a[i])
{
swap(a[j],a[i]);
break;
}
}
for(int j = n - 1; j > i; j--)
{
b[j] = a[j];
}
temp = i + 1;;
for(int j = n - 1; j > i; j--)
{
a[j] = b[temp++];
}
}
}
if(flag == false)
{
sort(a,a + n);
}
}
}
int main()
{
int m;
int time,num;
cin >> m;
while(m--)
{
scanf("%d%d",&num,&time);
for(int i = 0; i < num; i++)
{
scanf("%d",&a[i]);
}
pailie(num,time);
printf("%d",a[0]);
for(int i = 1; i < num; i++)
printf(" %d",a[i]);
printf("\n");
}
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