NC15803. Sorting Trees
描述
for(int i=1;i<n;i++) for(int j=1;j<=n-i;j++) if(a[j+k]<a[j])swap(a[j+k],a[j]);
输入描述
The first line contains two integers N and K as described above.Then the next line are N integers indicating the unsorted trees.
输出描述
A integer in a single line, indicating the first place that goes wrong. Or -1 means no mistake.
示例1
输入:
5 2 2 3 1 4 5
输出:
2
Python3(3.5.2) 解法, 执行用时: 318ms, 内存消耗: 31852K, 提交时间: 2018-04-29 15:46:27
n, k = map(int, input().split()) a = list(map(int, input().split())) s = sorted(a) p = [[j for j in range(i, len(a), k)] for i in range(k)] b = [sorted([a[j] for j in range(i, len(a), k)]) for i in range(k)] for i in range(len(p)): for j in range(len(p[i])): a[p[i][j]] = b[i][j] for i in range(len(a)): if a[i] != s[i]: print( i + 1 ) break else: print( -1 )
C++11(clang++ 3.9) 解法, 执行用时: 29ms, 内存消耗: 2380K, 提交时间: 2018-05-03 21:05:21
#include<iostream> #include<cstring> #include<algorithm> using namespace std; const int maxn=1e5+10; int a[maxn],b[maxn]; int main(){ int n,k; cin>>n>>k; for(int i=0;i<n;++i){ scanf("%d",&a[i]); b[i]=a[i]; } if(k==1){ printf("-1\n"); return 0; } sort(b,b+n); int ans=-1; for(int i=0;i<n;++i){ if(a[i]!=b[i]) { ans=i+1; break; } } printf("%d",ans); }