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

469ms...低空飘过

Posted by KatrineYang at 2016-11-15 14:29:44 on Problem 1833 and last updated at 2016-11-15 14:30:17
貌似可以平攤分析复杂度的?
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>

using namespace std;

int shu[1024];
int n,k;

void sp(int i1, int i2){
	int temp = shu[i1];
	shu[i1] = shu[i2];
	shu[i2] = temp;
}

void getNext(){
	int plc = -1;
	for(int i = n-1; i > 0; i--){
		if(shu[i] > shu[i-1]){
			plc = i;
			break;
		}
	}
	if(plc == -1){
		for(int i = 0; i < n/2; i++){
			sp(i, n-1-i);
		}
		return;
	}
	int tar = shu[plc-1];
	int l = plc, u = n-1;
	while(u > l){
		int m = (l+u+1)/2;
		if(shu[m] > tar) l = m;
		else u = m-1;
	}
	sp(plc-1, l);
	for(int i = plc, j = n-1; i < j; i++, j--){
		sp(i, j);
	}
}

int main(int argc, char **argv){
	int c;
	scanf("%d", &c);
	while(c--){
		scanf("%d%d",&n,&k);
		for(int i = 0; i < n; i++){
			scanf("%d", &shu[i]);
		}
		for(int i = 0; i < k; i++){
			getNext();
		}
		for(int i = 0; i < n; i++){
			printf(i != n-1? "%d ": "%d\n", shu[i]);
		}
	}
	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