Codeforces Round #714 (Div. 2)_A.Array and Peaks

Array and Peaks

题目链接在此!

题面:

在这里插入图片描述
在这里插入图片描述

中文题意:

给你样例数T,再给你T个n和m。
n代表数组元素为序列(1~n),m代表数组中有m个山峰。
输出排列和元素符合条件的数组。

思路:

先正常1,2,3…这样排,然后有几个山峰,就从后往前制造几个。
很简单的,交换a[n-1]/a[n]。下一个在a[n-2]/a[n-3]交换。

代码:

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <iomanip>

using namespace std;
int a[105];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int T;
    scanf("%d", &T);
    while (T--) {
        int n;
        scanf("%d", &n);
        int k;
        scanf("%d", &k);
        if((n%2==0&&(n/2-1<k))||(n%2!=0&&(n/2<k)))
        {
            printf("-1\n");
            continue;
        }
        for (int i = 1; i <= n; i++)
            a[i] = i;
        int cnt = 0;
        for (int i = 0;n-i-1>=1; i += 2) {
            if (cnt == k) break;
            else {
                swap(a[n - i], a[n - 1 - i]);
                cnt++;
            }
        }
        for (int i = 1; i <= n; i++) {
            if (i != n) printf("%d ", a[i]);
            else printf("%d\n", a[i]);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值