第十四届华中科技大学程序设计竞赛 F-Sorting Trees 【模拟】

题目描述 
It’s universally acknowledged that there’re innumerable trees in the campus of HUST.


One day the tree manager of HUST asked you to sort N trees increasing by their heights ai. Because you were a warm-hearted and professional programmer, you immediately wrote the Bubble Sort to finish the task. However, you were so tired that made a huge mistake:
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]);
Usually k=1 is the normal Bubble Sort. But you didn't type it correctly. When you now realize it, the trees have been replanted. In order to fix them you must find the first place that goes wrong, compared with the correctly sorted tree sequence.


If every trees are in their correct places then print -1. And you should know k can be equal to 1 which means you didn't make any mistake at all. Also you don't need to consider the part j+k beyond n.
输入描述:
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大写的门

#include<bits/stdc++.h>
using namespace std;
const int MAX = 1e5 + 7;
int a[MAX];
int b[MAX];
int c[MAX];
int main()
{
    int n, k;
    scanf("%d%d", &n, &k);
    for(int i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
        b[i] = a[i];
    }
    for(int i = 0; i < k; i++)
    {
        int pos = 0;
        for(int j = i; j < n; j += k)
            c[pos++] = a[j];
        sort(c, c + pos);
        pos = 0;
        for(int j = i; j < n; j += k)
            b[j] = c[pos++];
    }
    sort(a, a + n);
    for(int i = 0; i < n; i++)
    {
        if(a[i] != b[i])
        {
            cout << i + 1 << endl;
            return 0;
        }
    }
    cout << "-1" << endl;
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值