| ||||||||||
| 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 | |||||||||
469ms...低空飘过貌似可以平攤分析复杂度的?
#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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator