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 li≤hi≤ri. 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 hi−hi−1=di, and 0 ≤ d i ≤ 1 0 \leq d_i \leq 1 0≤di≤1. 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 1≤t≤104). 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) 1≤n≤2⋅105) 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 −1≤di≤1) — 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 0≤li≤ri≤n) — 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 2⋅105.
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已经可以确定为0
while(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();
}
}