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

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://www.lryc.cn/news/2396654.html

相关文章:

  • Python库CloudScraper详细使用(绕过 Cloudflare 的反机器人页面的 Python 模块)
  • oracle sql 语句 优化方法
  • Python数学可视化——显函数、隐函数及复杂曲线的交互式绘图技术
  • 代码随想录打卡|Day51 图论(dijkstra(堆优化版)精讲、Bellman_ford 算法精讲)
  • 【深度剖析】流处理系统性能优化:解决维表JOIN、数据倾斜与数据膨胀问题
  • PostgreSQL优化实践:从查询到架构的性能提升指南
  • AI入门——AI大模型、深度学习、机器学习总结
  • 【AI论文】论文转海报:迈向从科学论文到多模态海报的自动化生成
  • 智慧零工平台前端开发实战:从uni-app到跨平台应用
  • 【Linux】基础文件IO
  • opencv调用模型
  • 由浅入深一文详解同余原理
  • ESP-IDF 离线安装——同时存在多个版本以及进行版本切换的方法
  • android 上位机调试软件-安卓串口 com ttl 调试——仙盟创梦IDE
  • python打卡day42
  • XMOS以全新智能音频及边缘AI技术亮相广州国际专业灯光音响展
  • Playwright 测试框架 - Node.js
  • 机器学习有监督学习sklearn实战二:六种算法对鸢尾花(Iris)数据集进行分类和特征可视化
  • vr中风--数据处理模型搭建与训练2
  • 鸿蒙next系统以后会取代安卓吗?
  • PolyGen:一个用于 3D 网格的自回归生成模型 论文阅读
  • 约瑟夫问题 洛谷 - P1996
  • 系统思考:成长与投资不足
  • 快手可灵视频V1.6模型API如何接入免费AI开源项目工具
  • 数学建模期末速成 最短路径
  • 【Netty系列】实现HTTP文件服务器
  • Java开发经验——阿里巴巴编码规范实践解析7
  • 权威认证与质量保障:第三方检测在科技成果鉴定测试中的核心作用
  • 混和效应模型在医学分析中的应用
  • 架构分享|三层存储架构加速云端大模型推理