首页 > 其他分享 >题解:Codeforces Round 960 (Div. 2) B

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

时间:2024-07-21 11:17:51浏览次数:10  
标签:10 le 960 int 题解 leq max Div ldots

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\) of size \(m\), we define:

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

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

  • \(a_i\) is either \(1\) or \(-1\) for all \(1 \le i \le n\);
  • the maximum prefix position of \(a\) is \(x\);
  • the maximum suffix position of \(a\) is \(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\) 的数组 \(b\) ,我们可以这样定义:

  • \(b\) 的最大前缀位置是满足 \(b_1+\ldots+b_i=\max_{j=1}^{m}(b_1+\ldots+b_j)\) 的最小索引 \(i\) ;
  • \(b\) 的最大后缀位置是满足 \(b_i+\ldots+b_m=\max_{j=1}^{m}(b_j+\ldots+b_m)\) 的索引 \(i\) 。

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

  • 对于所有 \(1 \le i \le n\) , \(a_i\) 要么是 \(1\) 要么是 \(-1\) ;
  • \(a\) 的个最大前缀位置是 \(x\) ;
  • \(a\) 的最大后缀位置是 \(y\) 。

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

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

Input

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

For each test case:

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

It is guaranteed that the sum of \(n\) over all test cases will not exceed \(10^5\).

输入

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

对于每个测试用例

  • 唯一一行包含三个整数 \(n\) 、 \(x\) 和 \(y\) ( \(2 \leq n \leq 10^5, 1 \le y \lt x \le n)\) .

保证所有测试用例中 \(n\) 的总和不超过 \(10^5\) 。

Output

For each test case, output \(n\) space-separated integers \(a_1, a_2, \ldots, a_n\) in a new line.

输出

对于每个测试用例,在新行中输出 \(n\) 个空格分隔的整数 \(a_1, a_2, \ldots, a_n\) 。

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\) is the smallest index that satisfies \(a_1+\ldots +a_i=\max_{j=1}^{n}(a_1+\ldots+a_j)=2\);
  • \(i=y=3\) is the greatest index that satisfies \(a_i+\ldots +a_n=\max_{j=1}^{n}(a_j+\ldots+a_n)=2\).

Thus, the array \(a=[1,-1,1,1]\) is considered correct.

在第二个测试案例中

  • \(i=x=4\) 是满足 \(a_1+\ldots +a_i=\max_{j=1}^{n}(a_1+\ldots+a_j)=2\) 的索引;
  • \(i=y=3\) 是满足 \(a_i+\ldots +a_n=\max_{j=1}^{n}(a_j+\ldots+a_n)=2\) 的索引。

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

我的题解

分类讨论一下

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

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

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

但是!
我忽然想到,假如真的出现 \(x + 1 = y\) 的情况的话,那么 \(x\) 就不应该是在 \(x\) 这个位置上,应该比 \(x\) 大, \(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就可以了

标签:10,le,960,int,题解,leq,max,Div,ldots
From: https://www.cnblogs.com/jiejiejiang2004/p/18314260

相关文章

  • Codeforces Round 960 (Div.2) 补题
    A非常容易观察到性质,注意Alice为先手,发现当\(a_{\max}\)的个数为奇数时显然能win,但如果\(a_{\max}\)的个数为偶数且有一个数具有奇数个可以作为跳板,那么也能win,否则就lose。B结论:\(presum_x\geq2+presum_{y-1}\geq2+\min{presum_i}\geq1+\max{presum_i}......
  • 洛谷 P1162 填涂颜色题解
    题目链接填涂颜色题目描述由数字\(0\)组成的方阵中,有一任意形状的由数字\(1\)构成的闭合圈。现要求把闭合圈内的所有空间都填写成\(2\)。例如:\(6\times6\)的方阵(\(n=6\)),涂色前和涂色后的方阵如下:如果从某个\(0\)出发,只向上下左右\(4\)个方向移动且仅经过其他\(0\)......
  • 题解:Codeforces Round 960 (Div. 2) A
    A.SubmissionBaittimelimitpertest:1secondmemorylimitpertest:256megabytesinput:standardinputoutput:standardoutputAliceandBobareplayingagameinanarray\(a\)ofsize\(n\).Theytaketurnstodooperations,withAlicestarting......
  • Codeforces Round 960 (Div. 2) 补题记录(A~D)
    打的稀烂,但是还是上分了(A考虑对值域做一个后缀和。若某一个后缀和的值是奇数那么先手就可以获胜。否则就不可以获胜。(我才不会告诉你我这题吃了一次罚时的)#pragmaGCCoptimize(3)#include<bits/stdc++.h>#defineintlonglongusingnamespacestd;intmysqrt(intx){......
  • 使用牛顿法近似整数的 sqrt,ZeroDivisionError
    Sqrt(x)给定一个非负整数x,返回x的平方根,向下舍入到最接近的整数。返回的整数也应该是非负数。不得使用任何内置指数函数或运算符。例如,不要在c++中使用pow(x,0.5)或在c++中使用x**0.5python.示例1:输入:x=4输出:2解释:4的平方根是2,所......
  • [题解]P4166 [SCOI2007] 最大土地面积
    P4166[SCOI2007]最大土地面积解法\(1\)-\(O(n^2)\)我们运用调整法,可以证明这个四边形的\(4\)个顶点一定都在凸包的顶点上,具体来说:\(\textbf{Proof:}\)首先我们知道,凸包内,到某条直线距离最大的点一定包括\(1\)个顶点。接下来我们考虑一个凸包内的四边形:我们过它的对角......
  • ABC 363 E - Sinking Land 题解
    用一个优先队列维护和海相邻的位置,每次海面上升就判断一下队列中海拔最低的那个位置会不会被淹没,如果会,就删除,同时它上下左右的位置也是和海相邻的(或者就在海里),把它们加进优先队列里,记得判断一下加入的格子曾经有没有被加入过队列,不要加重复了。点击开DconstintN=1099;int......
  • ABC 363 F - Palindromic Expression 题解
    下文中提到的数字都不包含0,注意把含0的数字特判掉。反转指各个数位倒过来,比如114514反转过后就是415411。注意到,答案一定是这样:数列\(a\)的各个数字相乘,乘以一个回文,再把数列\(a\)倒过来,每个数反转,再相乘。比如:2*57*184481*75*2,其中的数列\(a\)就是:257,中间的回文......
  • Python学习笔记39:进阶篇(二十八)pygame的使用之按键映射及按键失效问题解决
    前言基础模块的知识通过这么长时间的学习已经有所了解,更加深入的话需要通过完成各种项目,在这个过程中逐渐学习,成长。我们的下一步目标是完成pythoncrashcourse中的外星人入侵项目,这是一个2D游戏项目。在这之前,我们先简单学习一下pygame模块。私信我发送消息python资料,......
  • CF819B Mister B and PR Shifts 题解
    题目传送门前置知识权值树状数组及应用解法由[ABC351F]DoubleSum的套路,尝试展开绝对值及\(\min,\max\)。将式子拆开有\(\begin{aligned}&\min\limits_{k=0}^{n-1}\{\sum\limits_{i=1}^{n-k}|a_{i}-(i+k)|+\sum\limits_{i=n-k+1}^{n}|a_{i}-(i-(n-k))|\}\\&=\min......