-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4_permuted_binary_strings.rs
More file actions
152 lines (142 loc) · 4.2 KB
/
Copy path4_permuted_binary_strings.rs
File metadata and controls
152 lines (142 loc) · 4.2 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// CSES Interactive-Problems Q-4 :: Permuted Binary Strings
// DateSolved: 14 Aug 2025
// SolvedBy: taneshqGupta
// use std::io::{stderr, Write};
#[derive(Default, Debug)]
struct Scanner(Vec<String>);
impl Scanner {
fn next<T: std::str::FromStr>(&mut self) -> T {
loop {
if let Some(c) = self.0.pop() {
return c.parse().ok().unwrap();
}
let mut s = String::new();
std::io::stdin().read_line(&mut s).unwrap();
self.0 = s.split_whitespace().rev().map(String::from).collect();
}
}
}
fn main() {
let mut cin = Scanner::default();
let n: usize = cin.next();
#[derive(Debug)]
struct Node {
low_bound: usize,
high_bound: usize,
}
let mut a: Vec<Node> = (0..n)
.map(|_| Node {
low_bound: 1,
high_bound: n,
})
.collect();
let mut divs = [false; 1001];
divs[n] = true;
loop {
let mut ys = true;
for i in 1..=n {
if !divs[i] {
ys = false;
}
}
if ys {
break;
}
let mut last = 0;
for i in 1..=n {
if divs[i] {
divs[last + (i - last) / 2] = true;
last = i;
}
}
let mut queries = Vec::new();
let mut zero = true;
for i in 1..=n {
queries.push(!zero);
if divs[i] {
zero = !zero;
}
}
print!("? ");
for &i in &queries {
print!("{}", if i { 1 } else { 0 });
}
println!();
let s: String = cin.next();
// for i in 1..=n {
// let _ = write!(stderr(), "{} ", if divs[i] { 1 } else { 0 });
// }
// let _ = writeln!(stderr());
let mut boolx = true;
for i in 1..=n {
if !divs[i] {
boolx = false;
}
}
for (i, c) in s.chars().enumerate() {
if a[i].low_bound == a[i].high_bound {
continue;
}
if !boolx {
if c == '0' {
let mut z = a[i].low_bound;
loop {
if divs[z] {
a[i].high_bound = z;
break;
}
z += 1;
}
} else {
let mut z = a[i].high_bound - 1;
loop {
if divs[z] {
a[i].low_bound = z + 1;
break;
}
z -= 1;
}
}
} else {
if c == '0' {
match (a[i].low_bound % 2 == 1, a[i].high_bound % 2 == 1) {
(true, true) => {
a[i].high_bound = a[i].low_bound;
}
(true, false) => {
a[i].high_bound = a[i].low_bound;
}
(false, true) => {
a[i].low_bound = a[i].high_bound;
}
(false, false) => {
a[i].high_bound = a[i].low_bound;
}
}
} else {
match (a[i].low_bound % 2 == 0, a[i].high_bound % 2 == 0) {
(true, true) => {
a[i].high_bound = a[i].low_bound;
}
(true, false) => {
a[i].high_bound = a[i].low_bound;
}
(false, true) => {
a[i].low_bound = a[i].high_bound;
}
(false, false) => {
a[i].high_bound = a[i].low_bound;
}
}
}
}
}
drop(s);
// dbg!(&a);
}
print!("! ");
for node in a {
print!("{} ", node.low_bound);
}
println!();
}