CF #738(div2)C. Mocha and Hiking(构造)
problem
C. Mocha and Hiking
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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 i 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 t (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≤104) — 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 104.
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
inputCopy
2
3
0 1 0
3
1 1 0
outputCopy
1 4 2 3
4 1 2 3
Note
In the first test case, the city looks like the following graph:
So all possible answers are (1→4→2→3), (1→2→3→4).
In the second test case, the city looks like the following graph:
So all possible answers are (4→1→2→3), (1→2→3→4), (3→4→1→2), (2→3→4→1).
C. 摩卡和远足
每次测试的时间限制1秒
每个测试的内存限制 256 兆字节
输入标准输入
输出标准输出
摩卡居住的城市叫之江。这个城市有n+1个村庄和2n-1条有向路。
有两种道路:
n−1 条道路是从村 i 到村 i+1,对于所有 1≤i≤n−1。
n 条道路可以用序列 a1,…,an 来描述。如果ai=0,则这些道路的第i条从村i到村n+1,否则从村n+1到村i,对于所有1≤i≤n。
摩卡计划这个周末和泷一起去远足。为免旅途无聊,他们计划每村只走一次。他们可以在任何村庄开始和结束。你能帮他们制定一个计划吗?
输入
每个测试包含多个测试用例。
第一行包含一个整数 t (1≤t≤20)——测试用例的数量。每个测试用例由两行组成。
每个测试用例的第一行包含一个整数 n (1≤n≤104) — 表示村庄数为 n+1。
每个测试用例的第二行包含 n 个整数 a1,a2,…,an (0≤ai≤1)。如果ai=0,则表示从i村到n+1村有路。如果ai=1,则表示从n+1村到i村有路。
保证所有测试用例的 n 之和不超过 104。
输出
对于每个测试用例,打印一行包含 n+1 个整数,其中第 i 个数字是他们将经过的第 i 个村庄。如果答案不存在,则打印 -1。
如果有多个正确答案,您可以打印其中任何一个。
例子
输入副本
2
3
0 1 0
3
1 1 0
输出副本
1 4 2 3
4 1 2 3
笔记
在第一个测试用例中,城市如下图所示:
所以所有可能的答案都是(1→4→2→3),(1→2→3→4)。
在第二个测试用例中,城市如下图所示:
所以所有可能的答案是(4→1→2→3)、(1→2→3→4)、(3→4→1→2)、(2→3→4→1)。
solution
如果 一种1= 1,那么路径 [ (n+1)→1→2→⋯→n ] 已验证。
如果 一种n= 0,那么路径 [ 1→2→⋯→n→(n+1) ] 已验证。
否则,由于 一种1= 0 ∧一种n= 1,必须存在一个整数 一世 (1 ≤ i < n) 在哪里 一种一世= 0 ∧一种我+ 1= 1,那么路径 [ 1→2→⋯→i→(n+1)→(i+1)→(i+2)→⋯n ] 已验证。
这是证明在锦标赛图中总是存在哈密顿路径的一步。
#include<bits/stdc++.h>
typedef long long LL;
using namespace std;
const int maxn = 1e5+10;
int a[maxn];
int main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int T; cin>>T;
while(T--){
int n; cin>>n;
for(int i = 1; i <= n; i++)cin>>a[i];
if(a[1]==1){
cout<<n+1<<" ";
for(int i = 1; i <= n; i++)
cout<<i<<" ";
cout<<"\n";
continue;
}
if(a[n]==0){
for(int i =1; i <= n+1; i++)
cout<<i<<" ";
cout<<"\n";
continue;
}
int l, r, ok = 0;
for(int i = 2; i <= n; i++){
if(a[i-1]==0 && a[i]==1){
l = i-1, r = i;
ok = 1; break;
}
}
if(ok==0)cout<<"-1\n";
else{
for(int i = 1; i <= l; i++) cout<<i<<" ";
cout<<n+1<<" ";
for(int i = r; i <= n; i++)cout<<i<<" ";
cout<<"\n";
}
}
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
文章来源: gwj1314.blog.csdn.net,作者:小哈里,版权归原作者所有,如需转载,请联系作者。
原文链接:gwj1314.blog.csdn.net/article/details/119741788
- 点赞
- 收藏
- 关注作者
评论(0)