题解: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
- the maximum prefix position of
is the smallest index that satisfies ; - the maximum suffix position of
is the largest index that satisfies .
You are given three integers
is either or for all ;- the maximum prefix position of
is ; - the maximum suffix position of
is .
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.
对于大小为
的最大前缀位置是满足 的最小索引 ; 的最大后缀位置是满足 的大索引 。
给你三个整数
- 对于所有
, 要么是 要么是 ; 的个最大前缀位置是 ; 的最大后缀位置是 。
如果有多个数组满足条件,则打印任意一个。可以证明,在给定的条件下,这样的数组总是存在的。
对于大小为
Input
The first line contains an integer
For each test case:
- The only line contains three integers
, , and ( .
It is guaranteed that the sum of
输入
第一行包含一个整数
对于每个测试用例
- 唯一一行包含三个整数
、 和 ( .
保证所有测试用例中
Output
For each test case, output
输出
对于每个测试用例,在新行中输出
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,
is the smallest index that satisfies ; is the greatest index that satisfies .
Thus, the array
注
在第二个测试案例中
是满足 的小索引; 是满足 的大索引。
因此,数组
我的题解
分类讨论一下
-
- 假如
,那就是两个区间之间没有交集了,要使左边到x最大,就是要到 的右边会变小;同理,要使右边到 最大,就是要到y的左边会变小。 - 因此,在我只要让
到 的中间部分全部为 ,然后从 开始一直到 交替为 ,同样的,从 开始一直到 交替为 ,这样让两边的区间和趋于0,同时又不会说边界被选到除了 和 的其他位置。
- 假如
-
- 假如
,那就是两个区间之间有交集 - 因此,在我只要让
到 的中间部分全部为 ,然后从 开始一直到 交替为 ,同样的,从 开始一直到 交替为 ,这样让两边的区间和趋于0,同时又不会说边界被选到除了 和 的其他位置。
- 假如
但是!
虽然但是总感觉这样有点漏洞,直到我看到了题目数据范围,
别急!
假设我们包含第一种情况吧,我为什么会觉得
但是!
我忽然想到,假如真的出现
结论:
我的思路是正确的!
我的代码
#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就可以了
posted on 2024-07-21 11:16 Jiejiejiang 阅读(175) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」