Codeforces Round 945 (Div. 2) C. Cat, Fox and Double Maximum 题解 贪心 构造

Cat, Fox and Double Maximum

题目描述

Fox loves permutations! She came up with the following problem and asked Cat to solve it:

You are given an even positive integer n n n and a permutation † ^\dagger p p p of length n n n.

The score of another permutation q q q of length n n n is the number of local maximums in the array a a a of length n n n, where a i = p i + q i a_i = p_i + q_i ai=pi+qi for all i i i ( 1 ≤ i ≤ n 1 \le i \le n 1in). In other words, the score of q q q is the number of i i i such that 1 < i < n 1 < i < n 1<i<n (note the strict inequalities), a i − 1 < a i a_{i-1} < a_i ai1<ai, and a i > a i + 1 a_i > a_{i+1} ai>ai+1 (once again, note the strict inequalities).

Find the permutation q q q that achieves the maximum score for given n n n and p p p. If there exist multiple such permutations, you can pick any of them.

† ^\dagger A permutation of length n n n is an array consisting of n n n distinct integers from 1 1 1 to n n n in arbitrary order. For example, [ 2 , 3 , 1 , 5 , 4 ] [2,3,1,5,4] [2,3,1,5,4] is a permutation, but [ 1 , 2 , 2 ] [1,2,2] [1,2,2] is not a permutation ( 2 2 2 appears twice in the array), and [ 1 , 3 , 4 ] [1,3,4] [1,3,4] is also not a permutation ( n = 3 n=3 n=3 but there is 4 4 4 in the array).

输入描述

The first line of input contains an integer t t t ( 1 ≤ t ≤ 1 0 4 1 \leq t \leq 10^4 1t104) — the number of test cases in the input you will have to solve.

The first line of each test case contains one even integer n n n ( 4 ≤ n ≤ 1 0 5 4 \leq n \leq 10^5 4n105, n n n is even) — the length of the permutation p p p.

The second line of each test case contains the n n n integers p 1 , p 2 , … , p n p_1, p_2, \ldots, p_n p1,p2,,pn ( 1 ≤ p i ≤ n 1 \leq p_i \leq n 1pin). It is guaranteed that p p p is a permutation of length n n n.

It is guaranteed that the sum of n n n across all test cases doesn’t exceed 1 0 5 10^5 105.

输出描述

For each test case, output one line containing any permutation of length n n n (the array q q q), such that q q q maximizes the score under the given constraints.

样例输入 #1

4
4
1 2 3 4
4
4 3 1 2
6
6 5 1 4 2 3
8
1 2 4 5 7 6 8 3

样例输出 #1

2 4 1 3
3 1 4 2
2 5 1 4 3 6
5 4 8 2 7 1 6 3

原题

CF——传送门
洛谷——传送门

代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        vector<int> p(n);
        for (int i = 0; i < n; i++)
        {
            cin >> p[i];
        }
        int n_pos = find(p.begin(), p.end(), n) - p.begin();
        vector<int> q(n);
        // 注意,由于我从下标0开始读入数据,所以在下面的描述中,0,2,4,6……是奇数位,而1,3,5,7……是偶数位
        if (n_pos & 1) // 如果值n在偶数位上,则将p排列翻转,使其变为奇数位上
        {
            reverse(p.begin(), p.end());
        }
        // n在偶数位和n在奇数位的情况最终都转化为n在奇数位的处理
        vector<pair<int, int>> even, odd; // 记录值和位置的数组
        // 先处理偶数位,偶数位需得到1~n/2,小的值交给大的
        for (int i = 1; i < n; i += 2)
        {
            even.push_back({p[i], i});
        }
        sort(even.begin(), even.end(), greater<pair<int, int>>());
        // 按小值交给大值的顺序赋值1~n/2
        for (int i = 0; i < n / 2; i++)
        {
            q[even[i].second] = i + 1;
        }
        // 再处理奇数位,奇数位的第一位得到n/2+1,剩余位得到n/2+2~n,大的值交给小的
        for (int i = 0; i < n; i += 2)
        {
            odd.push_back({p[i], i});
        }
        sort(odd.begin() + 1, odd.end(), greater<pair<int, int>>());
        // 按大值交给小值的顺序赋值n/2+1~n
        for (int i = 0; i < n / 2; i++)
        {
            q[odd[i].second] = i + n / 2 + 1;
        }
        // 若之前翻转过p,则再翻转一次结果q就能得到所需答案
        if (n_pos & 1)
        {
            reverse(q.begin(), q.end());
        }
        for (int i = 0; i < n; i++)
        {
            cout << q[i] << " \n"[i == n - 1]; // 新学到的小寄巧()
        }
    }

    return 0;
}
  • 26
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值