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

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.xdnf.cn/news/10612.html

相关文章:

  • Java内存模型与互斥锁
  • Python打卡训练营Day43
  • 《多状态DP:状态设计与状态转移方程速成指南》​
  • Leetcode 1136. 并行课程
  • MySQL语法练习 - 基础DDL/DML/DQL/DCL练习
  • 监督学习 vs 无监督学习:AI两大学习范式深度解析
  • Java内部类详细教程
  • 06.MySQL数据库操作详解
  • Retrievers检索器+RAG文档助手项目实战
  • 字符串加解密
  • 配置刷新技术
  • 【Python 进阶3】常见的 call 和 forward 区别
  • JavaSE 字符串:深入解析 String、StringBuilder与 StringBuffer
  • 第十章:Next的Seo实践
  • 力扣HOT100之多维动态规划:62. 不同路径
  • C. Basketball Exercise
  • Vue-6-前端框架Vue之基于Plotly.js绘制曲线
  • 3,信号与槽机制
  • BUUCTF[ACTF2020 新生赛]Include 1题解
  • NVM,Node.Js 管理工具
  • 【Delphi】接收windows文件夹中文件拖拽
  • (Python网络爬虫);抓取B站404页面小漫画
  • Python-matplotlib库之核心对象
  • 设计模式——备忘录设计模式(行为型)
  • Kotlin 中 companion object 扩展函数详解
  • Java连接Redis和基础操作命令
  • 【Linux】Ubuntu 20.04 英文系统显示中文字体异常
  • 什么是线程上下文切换?
  • 【SpringBoot】| 接口架构风格—RESTful
  • 概率统计:AI大模型的数学支柱