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

TLE 3次 用的字典序+sort 超时的根本原因是cin 用scanf就过了

Posted by flowertree_xiaoxiang at 2015-09-19 14:59:17 on Problem 1833
//============================================================================
//
// 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:
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