当前位置: 首页 > 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();}
}

相关文章:

  • 【笔记】为 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:基于神经网络的摊销贝叶斯推断框架
  • python可以做网站吗/怎么做链接推广产品
  • 可以做装修效果图的网站有哪些/有免费做网站的吗
  • 淘客网站怎么建立/免费自助建站平台
  • 做消费金融网站价格/百度有专做优化的没
  • 怎样做单页微信网站连接/邢台市seo服务
  • 花都网站设计/官方网站怎么查询