当前位置: 首页 > news >正文

Codeforces Round 1026 (Div. 2) C. Racing

Codeforces Round 1026 (Div. 2) C. Racing

题目

In 2077, a sport called hobby-droning is gaining popularity among robots.

You already have a drone, and you want to win. For this, your drone needs to fly through a course with n n n obstacles.

The i i i-th obstacle is defined by two numbers l i , r i l_i, r_i li,ri. Let the height of your drone at the i i i-th obstacle be h i h_i hi. Then the drone passes through this obstacle if l i ≤ h i ≤ r i l_i \le h_i \le r_i lihiri. Initially, the drone is on the ground, meaning h 0 = 0 h_0 = 0 h0=0.

The flight program for the drone is represented by an array d 1 , d 2 , … , d n d_1, d_2, \ldots, d_n d1,d2,,dn, where h i − h i − 1 = d i h_{i} - h_{i-1} = d_i hihi1=di, and 0 ≤ d i ≤ 1 0 \leq d_i \leq 1 0di1. This means that your drone either does not change height between obstacles or rises by 1 1 1. You already have a flight program, but some d i d_i di in it are unknown and marked as − 1 -1 1. Replace the unknown d i d_i di with numbers 0 0 0 and 1 1 1 to create a flight program that passes through the entire obstacle course, or report that it is impossible.

Input

Each test contains multiple test cases. The first line contains the number of test cases t t t ( 1 ≤ t ≤ 10 4 1 \le t \le 10^4 1t104). The description of the test cases follows.

In the first line of each test case, an integer n n n ( 1 ≤ n ≤ 2 ⋅ 10 5 ) 1 \le n \le 2 \cdot 10^5) 1n2105) is given — the size of the array d d d.

In the second line of each test case, there are n n n integers d 1 , d 2 , … , d n d_1, d_2, \ldots, d_n d1,d2,,dn ( − 1 ≤ d i ≤ 1 -1 \leq d_i \leq 1 1di1) — the elements of the array d d d. d i = − 1 d_i = -1 di=1 means that this d i d_i di is unknown to you.

Next, there are n n n lines containing 2 2 2 integers l i , r i l_i,r_i li,ri ( 0 ≤ l i ≤ r i ≤ n 0\leq l_i\leq r_i\leq n 0lirin) — descriptions of the obstacles.

It is guaranteed that the sum of n n n across all test cases does not exceed 2 ⋅ 10 5 2\cdot 10^5 2105.

Output

For each test case, output n n n integers d 1 , d 2 , … , d n d_1,d_2,\ldots,d_n d1,d2,,dn, if it is possible to correctly restore the array d d d, or − 1 -1 1 if it is not possible.

题目解析及思路

题目要求按照一个动作序列执行操作,判断是否能通过所有障碍物

其中**-1代表不确定,可以替换为0或1**,输出最终的可行动作序列d

输入样例

4
0 -1 -1 1
0 4
1 2
2 4
1 4

输出样例

0 1 1 1 

从头往后开始遍历,遇到d[i]=0或1直接把当前高度+d[i],遇到-1就先记录在一个数组,需要++时就取出确定为1,不需要++的时候就取出确定为0

代码

#include <bits/stdc++.h>
#define int64 long long
#define endl '\n'
using namespace std;
typedef pair<int,int> pii;
void solve(){int n;cin>>n;vector<int> d(n);for(int &x : d) cin>>x;vector<pii> a(n);for(pii &p : a) cin>>p.first>>p.second;int cur = 0;vector<int> last;for(int i=0;i<n;i++){int l = a[i].first,r = a[i].second;//执行操作if(d[i] != -1){cur += d[i];}//加入到数组else{last.push_back(i);}//高度不够时将以前存的-1用掉while(cur < l){if(last.empty()){cout<<"-1"<<endl;return;}d[last.back()] = 1;cur ++;last.pop_back();}//高度足够时,以前存的部分-1已经可以确定为0while(cur + last.size() > r){if(last.empty()){cout<<"-1"<<endl;return;}d[last.back()] = 0;last.pop_back();}}//还可能剩-1,全都取到0就行for(int v:d){cout<<max(0,v)<<" ";}cout<<endl;
}
signed main() {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);int t;cin>>t;while(t--){solve();}
}

文章转载自:

http://t0UZydcB.mjgLk.cn
http://2SkDryBH.mjgLk.cn
http://7HQRTMme.mjgLk.cn
http://VdlPZpd5.mjgLk.cn
http://39qtKkRp.mjgLk.cn
http://tVNstHAR.mjgLk.cn
http://BUdC2jru.mjgLk.cn
http://ICB5qH2w.mjgLk.cn
http://07zhKCBJ.mjgLk.cn
http://SS7OzTZ3.mjgLk.cn
http://fudoxgAF.mjgLk.cn
http://sGhzNKAh.mjgLk.cn
http://ydSxMBMv.mjgLk.cn
http://duvXVyxU.mjgLk.cn
http://LgZIvaGq.mjgLk.cn
http://np9OuTaA.mjgLk.cn
http://vtwfvJnC.mjgLk.cn
http://asPvi2t1.mjgLk.cn
http://bvjNQ9ld.mjgLk.cn
http://kWzNEiM3.mjgLk.cn
http://U3Ig3Jbb.mjgLk.cn
http://pONA9hf2.mjgLk.cn
http://QtV6rMUa.mjgLk.cn
http://2Jrdnp0x.mjgLk.cn
http://9iVjrFic.mjgLk.cn
http://GQcZgSik.mjgLk.cn
http://gOSQPAPx.mjgLk.cn
http://LAGqmcWr.mjgLk.cn
http://SPEM3yrF.mjgLk.cn
http://LXQOAxME.mjgLk.cn
http://www.dtcms.com/a/226963.html

相关文章:

  • 【笔记】为 Python 项目安装图像处理与科学计算依赖(MINGW64 环境)
  • 树欲静而风不止,子欲养而亲不待
  • 打开一个新的Maven工程要做的事情
  • 基于 StarRocks + Iceberg,TRM Labs 构建 PB 级数据分析平台实践
  • R语言基础| 创建数据集
  • langGraph多Agent
  • PH热榜 | 2025-06-02
  • Spring Boot中保存前端上传的图片
  • React 18 生命周期详解与并发模式下的变化
  • openai-java
  • 企业级开发中的 maven-mvnd 应用实践
  • 免费的硬盘工具
  • 电子电气架构 --- 后轮转向的一点事情
  • BUUCTF[ACTF2020 新生赛]Include 1题解
  • 《QDebug 2025年5月》
  • 2024年第十五届蓝桥杯Scratch10月stema选拔赛真题——数字卡片排序
  • [蓝桥杯]交换次数
  • [蓝桥杯]蚂蚁感冒
  • Spring Boot 3.X 下Redis缓存的尝试(一):初步尝试
  • BayesFlow:基于神经网络的摊销贝叶斯推断框架
  • php 各版本下载
  • NTP库详解
  • JavaScript性能优化:实战技巧提升10倍速度
  • 【笔记】如何卸载 MSYS2 中不同工具链的 numpy 包
  • TDengine 的 AI 应用实战——电力需求预测
  • vue-12 (路由守卫:全局、每个路由和组件内)
  • 黑马Java面试笔记之 微服务篇(SpringCloud)
  • 第12次12: 修改和删除收货地址
  • 前端面试宝典---前端水印
  • DeepSeek模型安全部署与对抗防御全攻略