C. Mocha and Hiking
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
The city where Mocha lives in is called Zhijiang. There are n+1 villages and 2n−1 directed roads in this city.
There are two kinds of roads:
- n−1 roads are from village i to village i+1, for all 1≤i≤n−1.
- n roads can be described by a sequence a1,…,an. If ai=0, the i-th of these roads goes from village ii to village n+1, otherwise it goes from village n+1 to village i, for all 1≤i≤n.
Mocha plans to go hiking with Taki this weekend. To avoid the trip being boring, they plan to go through every village exactly once. They can start and finish at any villages. Can you help them to draw up a plan?
Input
Each test contains multiple test cases.
The first line contains a single integer tt (1≤t≤20) — the number of test cases. Each test case consists of two lines.
The first line of each test case contains a single integer n (1≤n≤1e4) — indicates that the number of villages is n+1.
The second line of each test case contains n integers a1,a2,…,an (0≤ai≤1). If ai=0, it means that there is a road from village i to village n+1. If ai=1, it means that there is a road from village n+1 to village i.
It is guaranteed that the sum of n over all test cases does not exceed 1e4.
Output
For each test case, print a line with n+1 integers, where the i-th number is the i-th village they will go through. If the answer doesn't exist, print −1.
If there are multiple correct answers, you can print any one of them.
Example
input
2 3 0 1 0 3 1 1 0output
1 4 2 3 4 1 2 3题意理解:如果输入0,则这条路从该镇连通到n+1小镇,如果输入1,则这条路从n+1小镇连通到该小镇,1到n小镇按次序连接单向边,所以这题分多种情况,如果小镇1输入的是1,那么直接从n+1小镇到1小镇一直可以走完,如果最后一个小镇输入的是0,那么直接从1小镇到n+1小镇走下来即可,如果不是以上两种,我们可以找一个小镇i能到达小镇n+1的并且小镇n+1能到达小镇i+1的,记得记录一下小镇i和i+1的坐标,输出的时候把n+1添加在他们中间即可,如果都不是这三种输出-1(除了小镇n其他都能到达小镇n+1,这样必然不能走过所有小镇)。
#include <bits/stdc++.h> using namespace std; int main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin >> t; while(t--){ int n; cin >> n; vector<int> a(n + 2, 0); int x; for(int i = 1;i <= n;i++){ cin >> x; if(!x){ a[i] = 1; } else{ a[i] = 0; } } if(a[1] == 0){ cout << n+1 << ' '; for(int i = 1;i <= n;i++){ cout << i << ' '; } cout << endl; continue; } if(a[n]){ for(int i = 1;i <= n+1;i++){ cout << i << ' '; } cout << endl; continue; } int y,z,f=0; for(int i = 1;i <= n-1;i++){ if(a[i] == 1 && a[i+1] == 0){ y = i; z = i+1; f = 1; break; } } if(f){ for(int i = 1;i <= y;i++){ cout << i << ' '; } cout << n+1 << ' '; for(int i = z;i <= n;i++){ cout << i << ' '; } cout << endl; continue; } cout << "-1" << endl; } return 0; }
Codeforces Round #738 (Div. 2) C. Mocha and Hiking
于 2021-11-20 20:35:12 首次发布