题解:Codeforces Round 960 (Div. 2) B

B. Array Craft

time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output

For an array b b b of size m m m, we define:

  • the maximum prefix position of b b b is the smallest index i i i that satisfies b 1 + … + b i = max ⁡ j = 1 m ( b 1 + … + b j ) b_1+\ldots+b_i=\max_{j=1}^{m}(b_1+\ldots+b_j) b1++bi=maxj=1m(b1++bj);
  • the maximum suffix position of b b b is the largest index i i i that satisfies b i + … + b m = max ⁡ j = 1 m ( b j + … + b m ) b_i+\ldots+b_m=\max_{j=1}^{m}(b_j+\ldots+b_m) bi++bm=maxj=1m(bj++bm).

You are given three integers n n n, x x x, and y y y ( x > y x \gt y x>y). Construct an array a a a of size n n n satisfying:

  • a i a_i ai is either 1 1 1 or − 1 -1 1 for all 1 ≤ i ≤ n 1 \le i \le n 1in;
  • the maximum prefix position of a a a is x x x;
  • the maximum suffix position of a a a is y y y.

If there are multiple arrays that meet the conditions, print any. It can be proven that such an array always exists under the given conditions.

对于大小为 m m m 的数组 b b b ,我们可以这样定义:

  • b b b最大前缀位置是满足 b 1 + … + b i = max ⁡ j = 1 m ( b 1 + … + b j ) b_1+\ldots+b_i=\max_{j=1}^{m}(b_1+\ldots+b_j) b1++bi=maxj=1m(b1++bj)最小索引 i i i
  • b b b最大后缀位置是满足 b i + … + b m = max ⁡ j = 1 m ( b j + … + b m ) b_i+\ldots+b_m=\max_{j=1}^{m}(b_j+\ldots+b_m) bi++bm=maxj=1m(bj++bm)索引 i i i

给你三个整数 n n n x x x y y y ( x > ; y x \gt; y x>;y )。请构造一个大小为 n n n 的数组 a a a 满足以下条件:

  • 对于所有 1 ≤ i ≤ n 1 \le i \le n 1in a i a_i ai 要么是 1 1 1 要么是 − 1 -1 1
  • a a a个最大前缀位置 x x x
  • a a a最大后缀位置 y y y

如果有多个数组满足条件,则打印任意一个。可以证明,在给定的条件下,这样的数组总是存在的。

对于大小为 m m m 的数组 b b b ,我们可以定义: b b b 的最大前缀位置是满足 b 1 + … + b i = m a x j = 1 m ( b 1 + … + b j ) b_1+\ldots+b_i=\\max_{j=1}^{m}(b_1+\ldots+b_j) b1++bi=maxj=1m(b1++bj) 的最小索引 i i i b b b 的最大后缀位置是满足 b i + … + b m = m a x j = 1 m ( b j + … + b _ m ) b_i+\ldots+b_m=\\max_{j=1}^{m}(b_j+\ldots+b\_m) bi++bm=maxj=1m(bj++b_m) 的最大索引 i i i 。给你三个整数 n n n x x x y y y ( x > y x > y x>y )。构造一个大小为 n n n 的数组 a a a 满足: - 对于所有的 1 ≤ i ≤ n 1 \le i \le n 1in 来说, a i a_i ai 要么是 1 1 1 要么是 − 1 -1 1 a a a 的最大前缀位置是 x x x ; - a a a 的最大后缀位置是 y y y 。如果有多个数组满足条件,请打印任意一个。可以证明,在给定的条件下,这样的数组总是存在的。

Input

The first line contains an integer t t t ( 1 ≤ t ≤ 1 0 4 1 \leq t \leq 10^4 1t104) — the number of test cases.

For each test case:

  • The only line contains three integers n n n, x x x, and y y y ( 2 ≤ n ≤ 1 0 5 , 1 ≤ y < x ≤ n ) 2 \leq n \leq 10^5, 1 \le y \lt x \le n) 2n105,1y<xn).

It is guaranteed that the sum of n n n over all test cases will not exceed 1 0 5 10^5 105.

输入

第一行包含一个整数 t t t ( 1 ≤ t ≤ 1 0 4 1 \leq t \leq 10^4 1t104 ) - 测试用例的数量。

对于每个测试用例

  • 唯一一行包含三个整数 n n n x x x y y y 2 ≤ n ≤ 1 0 5 , 1 ≤ y < x ≤ n ) 2 \leq n \leq 10^5, 1 \le y \lt x \le n) 2n105,1y<xn) .

保证所有测试用例中 n n n 的总和不超过 1 0 5 10^5 105

Output

For each test case, output n n n space-separated integers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an in a new line.

输出

对于每个测试用例,在新行中输出 n n n 个空格分隔的整数 a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an

Example

input

3
2 2 1
4 4 3
6 5 1

output

1 1
1 -1 1 1
1 1 -1 1 1 -1

Note

In the second test case,

  • i = x = 4 i=x=4 i=x=4 is the smallest index that satisfies a 1 + … + a i = max ⁡ j = 1 n ( a 1 + … + a j ) = 2 a_1+\ldots +a_i=\max_{j=1}^{n}(a_1+\ldots+a_j)=2 a1++ai=maxj=1n(a1++aj)=2;
  • i = y = 3 i=y=3 i=y=3 is the greatest index that satisfies a i + … + a n = max ⁡ j = 1 n ( a j + … + a n ) = 2 a_i+\ldots +a_n=\max_{j=1}^{n}(a_j+\ldots+a_n)=2 ai++an=maxj=1n(aj++an)=2.

Thus, the array a = [ 1 , − 1 , 1 , 1 ] a=[1,-1,1,1] a=[1,1,1,1] is considered correct.

在第二个测试案例中

  • i = x = 4 i=x=4 i=x=4 是满足 a 1 + … + a i = max ⁡ j = 1 n ( a 1 + … + a j ) = 2 a_1+\ldots +a_i=\max_{j=1}^{n}(a_1+\ldots+a_j)=2 a1++ai=maxj=1n(a1++aj)=2索引;
  • i = y = 3 i=y=3 i=y=3 是满足 a i + … + a n = max ⁡ j = 1 n ( a j + … + a n ) = 2 a_i+\ldots +a_n=\max_{j=1}^{n}(a_j+\ldots+a_n)=2 ai++an=maxj=1n(aj++an)=2索引。

因此,数组 a = [ 1 , − 1 , 1 , 1 ] a=[1,-1,1,1] a=[1,1,1,1] 被认为是正确的。

我的题解

分类讨论一下

    • 假如 x < y x \lt y x<y ,那就是两个区间之间没有交集了,要使左边到x最大,就是要到 x x x 的右边会变小;同理,要使右边到 y y y 最大,就是要到y的左边会变小。
    • 因此,在我只要让 x x x y y y 的中间部分全部为 − 1 -1 1,然后从 x − 1 x-1 x1 开始一直到 0 0 0 交替为 1 , − 1 , 1 , − 1 , . . . 1,-1,1,-1,... 1,1,1,1,... ,同样的,从 y + 1 y+1 y+1 开始一直到 n n n 交替为 1 , − 1 , 1 , − 1 , . . . 1,-1,1,-1,... 1,1,1,1,...,这样让两边的区间和趋于0,同时又不会说边界被选到除了 x x x y y y 的其他位置。
    • 假如 x > y x \gt y x>y ,那就是两个区间之间有交集
    • 因此,在我只要让 x x x y y y 的中间部分全部为 1 1 1,然后从 x − 1 x-1 x1 开始一直到 0 0 0 交替为 − 1 , 1 , − 1 , . . . -1,1,-1,... 1,1,1,... ,同样的,从 y + 1 y+1 y+1 开始一直到 n n n 交替为 − 1 , 1 , − 1 , . . . -1,1,-1,... 1,1,1,...,这样让两边的区间和趋于0,同时又不会说边界被选到除了 y y y x x x 的其他位置。

但是!
虽然但是总感觉这样有点漏洞,直到我看到了题目数据范围, 1 ≤ y < x ≤ n 1 \le y \lt x \le n 1y<xn,我才知道我想多了,只用算第二种情况,那就是绝对无误的

别急!
假设我们包含第一种情况吧,我为什么会觉得 x < y x \lt y x<y 会有点问题呢,因为我想到了加入 x + 1 = y x + 1 = y x+1=y 的话怎么办,这样的话我的程序出来就是错的了。

但是!
我忽然想到,假如真的出现 x + 1 = y x + 1 = y x+1=y 的情况的话,那么 x x x 就不应该是在 x x x 这个位置上,应该比 x x x 大, y y y 也不应该是在 y y y 这个位置上,应该比 y y y 小!

结论:
我的思路是正确的!


我的代码

#include <bits/stdc++.h>
#define int long long

const int N = 1e7 + 10;
int t;
int a[N];

void solve() {
    int n,x,y;
    std::cin >> n >> x >> y;
    int jud = -1;

    if(x > y) {
        std::swap(x,y);
        jud *= (-1);
    }

    if(jud == -1) {
        for(int i = x ; i <= y ; i ++) a[i] = 1;
        x--;
        while(x-1 >= 0) a[x] = -1, x--;
        y++;
        while(n-y >= 0) a[y] =-1,y++;
    }
    else {
        for(int i = x ; i <= y ; i ++) a[i] = 1;
        for(int i = x-1 ; i > 0 ; i --) a[i] = ((x-i)&1 ? -1 : 1);
        for(int i = y+1 ; i <= n ; i ++) a[i] = ((i-y)&1 ? -1 : 1);
    }

    for(int i = 1 ; i <= n ;i ++) std::cout << a[i] << " ";
    std::cout << "\n";
}

signed main() {
    std::cin >> t;
    while(t--) {
        solve();
    }
    return 0;
}

简化版

把 if-else 那部分只保留else就可以了

搬运自https://www.cnblogs.com/jiejiejiang2004/p/18314260
博主已同意,我就是博主

  • 8
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
抱歉,根据提供的引用内容,我无法理解你具体想要问什么问题。请提供更清晰明确的问题,我将竭诚为你解答。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Codeforces Round 860 (Div. 2)题解](https://blog.csdn.net/qq_60653991/article/details/129802687)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [【CodeforcesCodeforces Round 865 (Div. 2) (补赛)](https://blog.csdn.net/t_mod/article/details/130104033)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [Codeforces Round 872 (Div. 2)(前三道](https://blog.csdn.net/qq_68286180/article/details/130570952)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值