QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#309899 | #8130. Yet Another Balanced Coloring Problem | ucup-team296# | AC ✓ | 53ms | 29384kb | Rust | 26.3kb | 2024-01-20 22:18:42 | 2024-01-20 22:18:43 |
Judging History
answer
//
pub mod solution {
//{"name":"ucup19_c","group":"Manual","url":"","interactive":false,"timeLimit":2000,"tests":[{"input":"","output":""}],"testType":"single","input":{"type":"stdin","fileName":null,"pattern":null},"output":{"type":"stdout","fileName":null,"pattern":null},"languages":{"java":{"taskClass":"ucup19_c"}}}
use crate::algo_lib::collections::vec_ext::inc_dec::IncDec;
use crate::algo_lib::io::input::Input;
use crate::algo_lib::io::output::Output;
use crate::algo_lib::string::str::Str;
use std::collections::HashSet;
use std::iter::once;
type PreCalc = ();
fn solve(input: &mut Input, out: &mut Output, _test_case: usize, _data: &PreCalc) {
let n = input.read_size();
let m = input.read_size();
let p = input.read_size_vec(n - 1).dec();
let q = input.read_size_vec(m - 1).dec();
let k = *p.iter().min().unwrap();
let mut left = (0..n)
.map(|i| {
if i < k {
once(i).collect::<HashSet<_>>()
} else {
HashSet::new()
}
})
.collect::<Vec<_>>();
for i in 0..n - 1 {
if left[i].len() % 2 == 1 {
let x = *left[i].iter().next().unwrap();
left[i].remove(&x);
left[p[i]].insert(x);
}
}
let mut right = (0..m)
.map(|i| {
if i < k {
once(i).collect::<HashSet<_>>()
} else {
HashSet::new()
}
})
.collect::<Vec<_>>();
for i in 0..m - 1 {
if right[i].len() % 2 == 1 {
let x = *right[i].iter().next().unwrap();
right[i].remove(&x);
right[q[i]].insert(x);
}
}
let mut left_at = vec![0; k];
let mut right_at = vec![0; k];
for i in 0..n {
for &j in &left[i] {
left_at[j] = i;
}
}
for i in 0..m {
for &j in &right[i] {
right_at[j] = i;
}
}
let mut ans = Str::from(vec![b' '; k]);
if left[n - 1].len() % 2 == 1 {
let mut x = *left[n - 1].iter().next().unwrap();
left[n - 1].remove(&x);
loop {
ans[x] = b'R';
let r = right_at[x];
right[r].remove(&x);
if right[r].len() % 2 == 0 {
break;
}
let y = *right[r].iter().next().unwrap();
right[r].remove(&y);
ans[y] = b'B';
let l = left_at[y];
left[l].remove(&y);
x = *left[l].iter().next().unwrap();
left[l].remove(&x);
}
}
for i in 0..n {
while !left[i].is_empty() {
let mut x = *left[i].iter().next().unwrap();
left[i].remove(&x);
loop {
ans[x] = b'R';
let r = right_at[x];
right[r].remove(&x);
let y = *right[r].iter().next().unwrap();
right[r].remove(&y);
ans[y] = b'B';
let l = left_at[y];
left[l].remove(&y);
if left[l].len() % 2 == 0 {
break;
}
x = *left[l].iter().next().unwrap();
left[l].remove(&x);
}
}
}
out.print_line(ans);
}
pub(crate) fn run(mut input: Input, mut output: Output) -> bool {
let pre_calc = ();
#[allow(dead_code)]
enum TestType {
Single,
MultiNumber,
MultiEof,
}
let test_type = TestType::MultiNumber;
match test_type {
TestType::Single => solve(&mut input, &mut output, 1, &pre_calc),
TestType::MultiNumber => {
let t = input.read();
for i in 1..=t {
solve(&mut input, &mut output, i, &pre_calc);
}
}
TestType::MultiEof => {
let mut i = 1;
while input.peek().is_some() {
solve(&mut input, &mut output, i, &pre_calc);
i += 1;
}
}
}
output.flush();
input.skip_whitespace();
input.peek().is_none()
}
}
pub mod algo_lib {
pub mod collections {
pub mod iter_ext {
pub mod collect {
pub trait IterCollect<T>: Iterator<Item = T> + Sized {
fn collect_vec(self) -> Vec<T> {
self.collect()
}
}
impl<T, I: Iterator<Item = T> + Sized> IterCollect<T> for I {}
}
}
pub mod vec_ext {
pub mod default {
pub fn default_vec<T: Default>(len: usize) -> Vec<T> {
let mut v = Vec::with_capacity(len);
for _ in 0..len {
v.push(T::default());
}
v
}
}
pub mod inc_dec {
use crate::algo_lib::numbers::num_traits::algebra::AdditionMonoidWithSub;
use crate::algo_lib::numbers::num_traits::algebra::One;
pub trait IncDec {
#[must_use]
fn inc(self) -> Self;
#[must_use]
fn dec(self) -> Self;
}
impl<T: AdditionMonoidWithSub + One> IncDec for Vec<T> {
fn inc(mut self) -> Self {
self.iter_mut().for_each(|i| *i += T::one());
self
}
fn dec(mut self) -> Self {
self.iter_mut().for_each(|i| *i -= T::one());
self
}
}
impl<T: AdditionMonoidWithSub + One, U: AdditionMonoidWithSub + One> IncDec for Vec<(T, U)> {
fn inc(mut self) -> Self {
self.iter_mut().for_each(|(i, j)| {
*i += T::one();
*j += U::one();
});
self
}
fn dec(mut self) -> Self {
self.iter_mut().for_each(|(i, j)| {
*i -= T::one();
*j -= U::one();
});
self
}
}
impl<T: AdditionMonoidWithSub + One, U: AdditionMonoidWithSub + One, V> IncDec for Vec<(T, U, V)> {
fn inc(mut self) -> Self {
self.iter_mut().for_each(|(i, j, _)| {
*i += T::one();
*j += U::one();
});
self
}
fn dec(mut self) -> Self {
self.iter_mut().for_each(|(i, j, _)| {
*i -= T::one();
*j -= U::one();
});
self
}
}
impl<T: AdditionMonoidWithSub + One, U: AdditionMonoidWithSub + One, V, W> IncDec
for Vec<(T, U, V, W)>
{
fn inc(mut self) -> Self {
self.iter_mut().for_each(|(i, j, ..)| {
*i += T::one();
*j += U::one();
});
self
}
fn dec(mut self) -> Self {
self.iter_mut().for_each(|(i, j, ..)| {
*i -= T::one();
*j -= U::one();
});
self
}
}
impl<T: AdditionMonoidWithSub + One, U: AdditionMonoidWithSub + One, V, W, X> IncDec
for Vec<(T, U, V, W, X)>
{
fn inc(mut self) -> Self {
self.iter_mut().for_each(|(i, j, ..)| {
*i += T::one();
*j += U::one();
});
self
}
fn dec(mut self) -> Self {
self.iter_mut().for_each(|(i, j, ..)| {
*i -= T::one();
*j -= U::one();
});
self
}
}
impl<T: AdditionMonoidWithSub + One, U: AdditionMonoidWithSub + One> IncDec for (T, U) {
fn inc(mut self) -> Self {
self.0 += T::one();
self.1 += U::one();
self
}
fn dec(mut self) -> Self {
self.0 -= T::one();
self.1 -= U::one();
self
}
}
}
}
}
pub mod io {
pub mod input {
use crate::algo_lib::collections::vec_ext::default::default_vec;
use std::io::Read;
pub struct Input<'s> {
input: &'s mut dyn Read,
buf: Vec<u8>,
at: usize,
buf_read: usize,
}
macro_rules! read_impl {
($t: ty, $read_name: ident, $read_vec_name: ident) => {
pub fn $read_name(&mut self) -> $t {
self.read()
}
pub fn $read_vec_name(&mut self, len: usize) -> Vec<$t> {
self.read_vec(len)
}
};
($t: ty, $read_name: ident, $read_vec_name: ident, $read_pair_vec_name: ident) => {
read_impl!($t, $read_name, $read_vec_name);
pub fn $read_pair_vec_name(&mut self, len: usize) -> Vec<($t, $t)> {
self.read_vec(len)
}
};
}
impl<'s> Input<'s> {
const DEFAULT_BUF_SIZE: usize = 4096;
pub fn new(input: &'s mut dyn Read) -> Self {
Self {
input,
buf: default_vec(Self::DEFAULT_BUF_SIZE),
at: 0,
buf_read: 0,
}
}
pub fn new_with_size(input: &'s mut dyn Read, buf_size: usize) -> Self {
Self {
input,
buf: default_vec(buf_size),
at: 0,
buf_read: 0,
}
}
pub fn get(&mut self) -> Option<u8> {
if self.refill_buffer() {
let res = self.buf[self.at];
self.at += 1;
if res == b'\r' {
if self.refill_buffer() && self.buf[self.at] == b'\n' {
self.at += 1;
}
return Some(b'\n');
}
Some(res)
} else {
None
}
}
pub fn peek(&mut self) -> Option<u8> {
if self.refill_buffer() {
let res = self.buf[self.at];
Some(if res == b'\r' { b'\n' } else { res })
} else {
None
}
}
pub fn skip_whitespace(&mut self) {
while let Some(b) = self.peek() {
if !char::from(b).is_whitespace() {
return;
}
self.get();
}
}
pub fn next_token(&mut self) -> Option<Vec<u8>> {
self.skip_whitespace();
let mut res = Vec::new();
while let Some(c) = self.get() {
if char::from(c).is_whitespace() {
break;
}
res.push(c);
}
if res.is_empty() {
None
} else {
Some(res)
}
}
//noinspection RsSelfConvention
pub fn is_exhausted(&mut self) -> bool {
self.peek().is_none()
}
//noinspection RsSelfConvention
pub fn is_empty(&mut self) -> bool {
self.skip_whitespace();
self.is_exhausted()
}
pub fn read<T: Readable>(&mut self) -> T {
T::read(self)
}
pub fn read_vec<T: Readable>(&mut self, size: usize) -> Vec<T> {
let mut res = Vec::with_capacity(size);
for _ in 0..size {
res.push(self.read());
}
res
}
pub fn read_char(&mut self) -> char {
self.skip_whitespace();
self.get().unwrap().into()
}
read_impl!(u32, read_unsigned, read_unsigned_vec);
read_impl!(u64, read_u64, read_u64_vec);
read_impl!(usize, read_size, read_size_vec, read_size_pair_vec);
read_impl!(i32, read_int, read_int_vec, read_int_pair_vec);
read_impl!(i64, read_long, read_long_vec, read_long_pair_vec);
read_impl!(i128, read_i128, read_i128_vec);
fn refill_buffer(&mut self) -> bool {
if self.at == self.buf_read {
self.at = 0;
self.buf_read = self.input.read(&mut self.buf).unwrap();
self.buf_read != 0
} else {
true
}
}
}
pub trait Readable {
fn read(input: &mut Input) -> Self;
}
impl Readable for char {
fn read(input: &mut Input) -> Self {
input.read_char()
}
}
impl<T: Readable> Readable for Vec<T> {
fn read(input: &mut Input) -> Self {
let size = input.read();
input.read_vec(size)
}
}
macro_rules! read_integer {
($($t:ident)+) => {$(
impl Readable for $t {
fn read(input: &mut Input) -> Self {
input.skip_whitespace();
let mut c = input.get().unwrap();
let sgn = match c {
b'-' => {
c = input.get().unwrap();
true
}
b'+' => {
c = input.get().unwrap();
false
}
_ => false,
};
let mut res = 0;
loop {
assert!(c.is_ascii_digit());
res *= 10;
let d = (c - b'0') as $t;
if sgn {
res -= d;
} else {
res += d;
}
match input.get() {
None => break,
Some(ch) => {
if ch.is_ascii_whitespace() {
break;
} else {
c = ch;
}
}
}
}
res
}
}
)+};
}
read_integer!(i8 i16 i32 i64 i128 isize u8 u16 u32 u64 u128 usize);
macro_rules! tuple_readable {
($($name:ident)+) => {
impl<$($name: Readable), +> Readable for ($($name,)+) {
fn read(input: &mut Input) -> Self {
($($name::read(input),)+)
}
}
}
}
tuple_readable! {T}
tuple_readable! {T U}
tuple_readable! {T U V}
tuple_readable! {T U V X}
tuple_readable! {T U V X Y}
tuple_readable! {T U V X Y Z}
tuple_readable! {T U V X Y Z A}
tuple_readable! {T U V X Y Z A B}
tuple_readable! {T U V X Y Z A B C}
tuple_readable! {T U V X Y Z A B C D}
tuple_readable! {T U V X Y Z A B C D E}
tuple_readable! {T U V X Y Z A B C D E F}
impl Read for Input<'_> {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
if self.at == self.buf_read {
self.input.read(buf)
} else {
let mut i = 0;
while i < buf.len() && self.at < self.buf_read {
buf[i] = self.buf[self.at];
i += 1;
self.at += 1;
}
Ok(i)
}
}
}
}
pub mod output {
use crate::algo_lib::collections::vec_ext::default::default_vec;
use std::io::stderr;
use std::io::Stderr;
use std::io::Write;
#[derive(Copy, Clone)]
pub enum BoolOutput {
YesNo,
YesNoCaps,
PossibleImpossible,
Custom(&'static str, &'static str),
}
impl BoolOutput {
pub fn output(&self, output: &mut Output, val: bool) {
(if val { self.yes() } else { self.no() }).write(output);
}
fn yes(&self) -> &str {
match self {
BoolOutput::YesNo => "Yes",
BoolOutput::YesNoCaps => "YES",
BoolOutput::PossibleImpossible => "Possible",
BoolOutput::Custom(yes, _) => yes,
}
}
fn no(&self) -> &str {
match self {
BoolOutput::YesNo => "No",
BoolOutput::YesNoCaps => "NO",
BoolOutput::PossibleImpossible => "Impossible",
BoolOutput::Custom(_, no) => no,
}
}
}
pub struct Output<'s> {
output: &'s mut dyn Write,
buf: Vec<u8>,
at: usize,
auto_flush: bool,
bool_output: BoolOutput,
}
impl<'s> Output<'s> {
const DEFAULT_BUF_SIZE: usize = 4096;
pub fn new(output: &'s mut dyn Write) -> Self {
Self {
output,
buf: default_vec(Self::DEFAULT_BUF_SIZE),
at: 0,
auto_flush: false,
bool_output: BoolOutput::YesNoCaps,
}
}
pub fn new_with_auto_flush(output: &'s mut dyn Write) -> Self {
Self {
output,
buf: default_vec(Self::DEFAULT_BUF_SIZE),
at: 0,
auto_flush: true,
bool_output: BoolOutput::YesNoCaps,
}
}
pub fn flush(&mut self) {
if self.at != 0 {
self.output.write_all(&self.buf[..self.at]).unwrap();
self.output.flush().unwrap();
self.at = 0;
}
}
pub fn print<T: Writable>(&mut self, s: T) {
s.write(self);
self.maybe_flush();
}
pub fn print_line<T: Writable>(&mut self, s: T) {
self.print(s);
self.put(b'\n');
self.maybe_flush();
}
pub fn put(&mut self, b: u8) {
self.buf[self.at] = b;
self.at += 1;
if self.at == self.buf.len() {
self.flush();
}
}
pub fn maybe_flush(&mut self) {
if self.auto_flush {
self.flush();
}
}
pub fn print_per_line<T: Writable>(&mut self, arg: &[T]) {
for i in arg {
i.write(self);
self.put(b'\n');
}
}
pub fn print_iter<T: Writable, I: Iterator<Item = T>>(&mut self, iter: I) {
let mut first = true;
for e in iter {
if first {
first = false;
} else {
self.put(b' ');
}
e.write(self);
}
}
pub fn print_iter_ref<'a, T: 'a + Writable, I: Iterator<Item = &'a T>>(&mut self, iter: I) {
let mut first = true;
for e in iter {
if first {
first = false;
} else {
self.put(b' ');
}
e.write(self);
}
}
pub fn set_bool_output(&mut self, bool_output: BoolOutput) {
self.bool_output = bool_output;
}
}
impl Write for Output<'_> {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
let mut start = 0usize;
let mut rem = buf.len();
while rem > 0 {
let len = (self.buf.len() - self.at).min(rem);
self.buf[self.at..self.at + len].copy_from_slice(&buf[start..start + len]);
self.at += len;
if self.at == self.buf.len() {
self.flush();
}
start += len;
rem -= len;
}
self.maybe_flush();
Ok(buf.len())
}
fn flush(&mut self) -> std::io::Result<()> {
self.flush();
Ok(())
}
}
pub trait Writable {
fn write(&self, output: &mut Output);
}
impl Writable for &str {
fn write(&self, output: &mut Output) {
output.write_all(self.as_bytes()).unwrap();
}
}
impl Writable for String {
fn write(&self, output: &mut Output) {
output.write_all(self.as_bytes()).unwrap();
}
}
impl Writable for char {
fn write(&self, output: &mut Output) {
output.put(*self as u8);
}
}
impl<T: Writable> Writable for [T] {
fn write(&self, output: &mut Output) {
output.print_iter_ref(self.iter());
}
}
impl<T: Writable, const N: usize> Writable for [T; N] {
fn write(&self, output: &mut Output) {
output.print_iter_ref(self.iter());
}
}
impl<T: Writable> Writable for &T {
fn write(&self, output: &mut Output) {
T::write(self, output)
}
}
impl<T: Writable> Writable for Vec<T> {
fn write(&self, output: &mut Output) {
self.as_slice().write(output);
}
}
impl Writable for () {
fn write(&self, _output: &mut Output) {}
}
macro_rules! write_to_string {
($($t:ident)+) => {$(
impl Writable for $t {
fn write(&self, output: &mut Output) {
self.to_string().write(output);
}
}
)+};
}
write_to_string!(u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize);
macro_rules! tuple_writable {
($name0:ident $($name:ident: $id:tt )*) => {
impl<$name0: Writable, $($name: Writable,)*> Writable for ($name0, $($name,)*) {
fn write(&self, out: &mut Output) {
self.0.write(out);
$(
out.put(b' ');
self.$id.write(out);
)*
}
}
}
}
tuple_writable! {T}
tuple_writable! {T U:1}
tuple_writable! {T U:1 V:2}
tuple_writable! {T U:1 V:2 X:3}
tuple_writable! {T U:1 V:2 X:3 Y:4}
tuple_writable! {T U:1 V:2 X:3 Y:4 Z:5}
tuple_writable! {T U:1 V:2 X:3 Y:4 Z:5 A:6}
tuple_writable! {T U:1 V:2 X:3 Y:4 Z:5 A:6 B:7}
impl<T: Writable> Writable for Option<T> {
fn write(&self, output: &mut Output) {
match self {
None => (-1).write(output),
Some(t) => t.write(output),
}
}
}
impl Writable for bool {
fn write(&self, output: &mut Output) {
let bool_output = output.bool_output;
bool_output.output(output, *self)
}
}
static mut ERR: Option<Stderr> = None;
pub fn err() -> Output<'static> {
unsafe {
if ERR.is_none() {
ERR = Some(stderr());
}
Output::new_with_auto_flush(ERR.as_mut().unwrap())
}
}
}
}
pub mod numbers {
pub mod num_traits {
pub mod algebra {
use crate::algo_lib::numbers::num_traits::invertible::Invertible;
use std::ops::Add;
use std::ops::AddAssign;
use std::ops::Div;
use std::ops::DivAssign;
use std::ops::Mul;
use std::ops::MulAssign;
use std::ops::Neg;
use std::ops::Rem;
use std::ops::RemAssign;
use std::ops::Sub;
use std::ops::SubAssign;
pub trait Zero {
fn zero() -> Self;
}
pub trait One {
fn one() -> Self;
}
pub trait AdditionMonoid: Add<Output = Self> + AddAssign + Zero + Eq + Sized {}
impl<T: Add<Output = Self> + AddAssign + Zero + Eq> AdditionMonoid for T {}
pub trait AdditionMonoidWithSub: AdditionMonoid + Sub<Output = Self> + SubAssign {}
impl<T: AdditionMonoid + Sub<Output = Self> + SubAssign> AdditionMonoidWithSub for T {}
pub trait AdditionGroup: AdditionMonoidWithSub + Neg<Output = Self> {}
impl<T: AdditionMonoidWithSub + Neg<Output = Self>> AdditionGroup for T {}
pub trait MultiplicationMonoid: Mul<Output = Self> + MulAssign + One + Eq + Sized {}
impl<T: Mul<Output = Self> + MulAssign + One + Eq> MultiplicationMonoid for T {}
pub trait IntegerMultiplicationMonoid:
MultiplicationMonoid + Div<Output = Self> + Rem<Output = Self> + DivAssign + RemAssign
{
}
impl<T: MultiplicationMonoid + Div<Output = Self> + Rem<Output = Self> + DivAssign + RemAssign>
IntegerMultiplicationMonoid for T
{
}
pub trait MultiplicationGroup:
MultiplicationMonoid + Div<Output = Self> + DivAssign + Invertible<Output = Self>
{
}
impl<T: MultiplicationMonoid + Div<Output = Self> + DivAssign + Invertible<Output = Self>>
MultiplicationGroup for T
{
}
pub trait SemiRing: AdditionMonoid + MultiplicationMonoid {}
impl<T: AdditionMonoid + MultiplicationMonoid> SemiRing for T {}
pub trait SemiRingWithSub: AdditionMonoidWithSub + SemiRing {}
impl<T: AdditionMonoidWithSub + SemiRing> SemiRingWithSub for T {}
pub trait Ring: SemiRing + AdditionGroup {}
impl<T: SemiRing + AdditionGroup> Ring for T {}
pub trait IntegerSemiRing: SemiRing + IntegerMultiplicationMonoid {}
impl<T: SemiRing + IntegerMultiplicationMonoid> IntegerSemiRing for T {}
pub trait IntegerSemiRingWithSub: SemiRingWithSub + IntegerSemiRing {}
impl<T: SemiRingWithSub + IntegerSemiRing> IntegerSemiRingWithSub for T {}
pub trait IntegerRing: IntegerSemiRing + Ring {}
impl<T: IntegerSemiRing + Ring> IntegerRing for T {}
pub trait Field: Ring + MultiplicationGroup {}
impl<T: Ring + MultiplicationGroup> Field for T {}
macro_rules! zero_one_integer_impl {
($($t: ident)+) => {$(
impl Zero for $t {
fn zero() -> Self {
0
}
}
impl One for $t {
fn one() -> Self {
1
}
}
)+};
}
zero_one_integer_impl!(i128 i64 i32 i16 i8 isize u128 u64 u32 u16 u8 usize);
}
pub mod invertible {
pub trait Invertible {
type Output;
fn inv(&self) -> Option<Self::Output>;
}
}
}
}
pub mod string {
pub mod str {
use crate::algo_lib::collections::iter_ext::collect::IterCollect;
use crate::algo_lib::io::input::Input;
use crate::algo_lib::io::input::Readable;
use crate::algo_lib::io::output::Output;
use crate::algo_lib::io::output::Writable;
use std::cmp::Ordering;
use std::fmt::Debug;
use std::fmt::Display;
use std::fmt::Formatter;
use std::hash::Hash;
use std::hash::Hasher;
use std::iter::Copied;
use std::iter::FromIterator;
use std::marker::PhantomData;
use std::ops::Add;
use std::ops::AddAssign;
use std::ops::Deref;
use std::ops::DerefMut;
use std::ops::Index;
use std::ops::IndexMut;
use std::slice::Iter;
use std::slice::IterMut;
use std::slice::SliceIndex;
use std::str::FromStr;
use std::vec::IntoIter;
pub enum Str<'s> {
Extendable(Vec<u8>, PhantomData<&'s [u8]>),
Owned(Box<[u8]>, PhantomData<&'s [u8]>),
Ref(&'s [u8]),
}
impl Default for Str<'static> {
fn default() -> Self {
Self::new()
}
}
impl Str<'static> {
pub fn new() -> Self {
Str::Extendable(Vec::new(), PhantomData)
}
pub fn with_capacity(cap: usize) -> Self {
Str::Extendable(Vec::with_capacity(cap), PhantomData)
}
}
impl<'s> Str<'s> {
pub fn push(&mut self, c: u8) {
self.transform_to_extendable();
self.as_extendable().push(c)
}
pub fn pop(&mut self) -> Option<u8> {
self.transform_to_extendable();
self.as_extendable().pop()
}
pub fn as_slice(&self) -> &[u8] {
match self {
Str::Extendable(s, _) => s.as_ref(),
Str::Owned(s, _) => s.as_ref(),
Str::Ref(s) => s,
}
}
pub fn len(&self) -> usize {
self.as_slice().len()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn iter(&self) -> Copied<Iter<u8>> {
match self {
Str::Extendable(v, _) => v.iter(),
Str::Owned(v, _) => v.iter(),
Str::Ref(v) => v.iter(),
}
.copied()
}
pub fn iter_mut(&mut self) -> IterMut<u8> {
self.transform_to_owned();
self.as_mut_slice().iter_mut()
}
pub fn sort(&mut self) {
self.transform_to_owned();
self.as_mut_slice().sort_unstable();
}
pub fn into_owned(mut self) -> Str<'static> {
self.transform_to_owned();
match self {
Str::Extendable(v, _) => Str::Extendable(v, PhantomData),
Str::Owned(v, _) => Str::Owned(v, PhantomData),
_ => unreachable!(),
}
}
fn transform_to_extendable(&mut self) {
match self {
Str::Extendable(_, _) => {}
Str::Owned(_, _) => {
let mut fake = Str::new();
std::mem::swap(self, &mut fake);
if let Str::Owned(s, _) = fake {
*self = Str::Extendable(s.to_vec(), PhantomData)
}
}
Str::Ref(s) => *self = Str::Extendable(s.to_vec(), PhantomData),
}
}
fn as_extendable(&mut self) -> &mut Vec<u8> {
match self {
Str::Extendable(s, _) => s,
_ => panic!("unreachable"),
}
}
fn transform_to_owned(&mut self) {
if let Str::Ref(s) = self {
*self = Str::Owned(s.to_vec().into_boxed_slice(), PhantomData)
}
}
pub fn as_mut_slice(&mut self) -> &mut [u8] {
self.transform_to_owned();
match self {
Str::Extendable(s, _) => s.as_mut_slice(),
Str::Owned(s, _) => s.as_mut(),
_ => panic!("unreachable"),
}
}
pub fn into_string(self) -> String {
match self {
Str::Extendable(v, _) => unsafe { String::from_utf8_unchecked(v) },
Str::Owned(v, _) => unsafe { String::from_utf8_unchecked(v.into_vec()) },
Str::Ref(v) => String::from_utf8_lossy(v).into_owned(),
}
}
pub fn reverse(&mut self) {
self.as_mut_slice().reverse();
}
pub fn trim(&self) -> Str<'_> {
let mut start = 0;
let mut end = self.len();
while start < end && (self[start] as char).is_whitespace() {
start += 1;
}
while start < end && (self[end - 1] as char).is_whitespace() {
end -= 1;
}
self[start..end].into()
}
pub fn split<'a, 'b>(&'a self, sep: impl Into<Str<'b>>) -> Vec<Str<'a>>
where
's: 'a,
{
let sep = sep.into();
let mut res = Vec::new();
let mut start = 0;
for i in 0..self.len() {
if self[i..].starts_with(sep.as_slice()) {
res.push(self[start..i].into());
start = i + sep.len();
}
}
res.push(self[start..].into());
res
}
pub fn parse<F: FromStr>(self) -> F
where
F::Err: Debug,
{
self.into_string().parse().unwrap()
}
pub fn parse_vec<T: Readable>(&self) -> Vec<T> {
let mut bytes = self.as_slice();
let mut input = Input::new(&mut bytes);
let mut res = Vec::new();
while !input.is_exhausted() {
res.push(input.read());
}
res
}
}
impl<'s> IntoIterator for Str<'s> {
type Item = u8;
type IntoIter = IntoIter<u8>;
#[allow(clippy::unnecessary_to_owned)]
fn into_iter(self) -> Self::IntoIter {
match self {
Str::Extendable(v, _) => v.into_iter(),
Str::Owned(v, _) => v.into_vec().into_iter(),
Str::Ref(v) => v.to_vec().into_iter(),
}
}
}
impl From<String> for Str<'static> {
fn from(s: String) -> Self {
Str::Extendable(s.into(), PhantomData)
}
}
impl<'s> From<&'s str> for Str<'s> {
fn from(s: &'s str) -> Self {
Str::Ref(s.as_bytes())
}
}
impl From<Vec<u8>> for Str<'static> {
fn from(s: Vec<u8>) -> Self {
Str::Extendable(s, PhantomData)
}
}
impl<'s> From<&'s [u8]> for Str<'s> {
fn from(s: &'s [u8]) -> Self {
Str::Ref(s)
}
}
impl<'s, const N: usize> From<&'s [u8; N]> for Str<'s> {
fn from(s: &'s [u8; N]) -> Self {
Str::Ref(s)
}
}
impl<'s> From<&'s String> for Str<'s> {
fn from(s: &'s String) -> Self {
Str::Ref(s.as_bytes())
}
}
impl<'s> From<&'s Vec<u8>> for Str<'s> {
fn from(s: &'s Vec<u8>) -> Self {
Str::Ref(s.as_slice())
}
}
impl From<u8> for Str<'static> {
fn from(c: u8) -> Self {
Str::Owned(Box::new([c]), PhantomData)
}
}
impl From<char> for Str<'static> {
fn from(c: char) -> Self {
Str::from(c as u8)
}
}
impl<'s, 't: 's> From<&'s Str<'t>> for Str<'s> {
fn from(value: &'s Str<'t>) -> Self {
Str::Ref(value.as_slice())
}
}
impl<R: SliceIndex<[u8]>> Index<R> for Str<'_> {
type Output = R::Output;
fn index(&self, index: R) -> &Self::Output {
self.as_slice().index(index)
}
}
impl<R: SliceIndex<[u8]>> IndexMut<R> for Str<'_> {
fn index_mut(&mut self, index: R) -> &mut Self::Output {
self.transform_to_owned();
self.as_mut_slice().index_mut(index)
}
}
impl Clone for Str<'_> {
fn clone(&self) -> Self {
match self {
Str::Extendable(s, _) => s.clone().into(),
Str::Owned(s, _) => s.to_vec().into(),
Str::Ref(s) => Str::Ref(s),
}
}
}
impl<'r, 's, S: Into<Str<'r>>> AddAssign<S> for Str<'s> {
fn add_assign(&mut self, rhs: S) {
self.transform_to_extendable();
self.as_extendable()
.extend_from_slice(rhs.into().as_slice());
}
}
impl<'r, 's, S: Into<Str<'r>>> Add<S> for Str<'s> {
type Output = Str<'s>;
fn add(mut self, rhs: S) -> Self::Output {
self += rhs;
self
}
}
impl Readable for Str<'static> {
fn read(input: &mut Input) -> Self {
input.next_token().unwrap().into()
}
}
impl Writable for Str<'_> {
fn write(&self, output: &mut Output) {
for c in self.as_slice() {
output.put(*c);
}
output.maybe_flush();
}
}
impl Display for Str<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
<String as Display>::fmt(&String::from_utf8(self.as_slice().to_vec()).unwrap(), f)
}
}
impl Hash for Str<'_> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.as_slice().hash(state);
}
}
impl<'r> PartialEq<Str<'r>> for Str<'_> {
fn eq(&self, other: &Str<'r>) -> bool {
self.as_slice().eq(other.as_slice())
}
}
impl Eq for Str<'_> {}
impl<'r> PartialOrd<Str<'r>> for Str<'_> {
fn partial_cmp(&self, other: &Str<'r>) -> Option<Ordering> {
self.as_slice().partial_cmp(other.as_slice())
}
}
impl Ord for Str<'_> {
fn cmp(&self, other: &Self) -> Ordering {
self.as_slice().cmp(other.as_slice())
}
}
impl FromIterator<u8> for Str<'static> {
fn from_iter<T: IntoIterator<Item = u8>>(iter: T) -> Self {
Self::Extendable(iter.into_iter().collect_vec(), Default::default())
}
}
impl<'r> FromIterator<&'r u8> for Str<'static> {
fn from_iter<T: IntoIterator<Item = &'r u8>>(iter: T) -> Self {
Self::Extendable(iter.into_iter().cloned().collect_vec(), Default::default())
}
}
impl Deref for Str<'_> {
type Target = [u8];
fn deref(&self) -> &Self::Target {
self.as_slice()
}
}
impl DerefMut for Str<'_> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.as_mut_slice()
}
}
pub trait StrReader {
fn read_str(&mut self) -> Str<'static>;
fn read_str_vec(&mut self, n: usize) -> Vec<Str<'static>>;
fn read_line(&mut self) -> Str<'static>;
fn read_line_vec(&mut self, n: usize) -> Vec<Str<'static>>;
fn read_lines(&mut self) -> Vec<Str<'static>>;
}
impl StrReader for Input<'_> {
fn read_str(&mut self) -> Str<'static> {
self.read()
}
fn read_str_vec(&mut self, n: usize) -> Vec<Str<'static>> {
self.read_vec(n)
}
fn read_line(&mut self) -> Str<'static> {
let mut res = Str::new();
while let Some(c) = self.get() {
if c == b'\n' {
break;
}
res.push(c);
}
res
}
fn read_line_vec(&mut self, n: usize) -> Vec<Str<'static>> {
let mut res = Vec::with_capacity(n);
for _ in 0..n {
res.push(self.read_line());
}
res
}
fn read_lines(&mut self) -> Vec<Str<'static>> {
let mut res = Vec::new();
while !self.is_exhausted() {
res.push(self.read_line());
}
if let Some(s) = res.last() {
if s.is_empty() {
res.pop();
}
}
res
}
}
}
}
}
fn main() {
let mut sin = std::io::stdin();
let input = if false {
algo_lib::io::input::Input::new_with_size(&mut sin, 1)
} else {
algo_lib::io::input::Input::new(&mut sin)
};
let mut stdout = std::io::stdout();
let output = if false {
algo_lib::io::output::Output::new_with_auto_flush(&mut stdout)
} else {
algo_lib::io::output::Output::new(&mut stdout)
};
solution::run(input, output);
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 2048kb
input:
2 7 7 5 5 6 6 7 7 5 6 5 6 7 7 5 4 4 4 5 5 4 4 4
output:
BRRB RBR
result:
ok ok (2 test cases)
Test #2:
score: 0
Accepted
time: 19ms
memory: 2256kb
input:
10000 6 6 5 5 5 5 6 5 6 6 6 6 9 6 7 9 7 7 6 8 9 9 6 6 6 6 6 9 8 9 9 8 7 7 8 8 9 7 7 8 7 7 7 8 6 10 4 5 5 6 6 4 6 5 7 8 9 8 9 10 6 9 6 6 6 6 6 6 9 7 8 8 9 9 9 8 9 8 6 6 7 6 7 8 7 9 7 6 7 8 9 9 9 8 6 7 7 5 8 8 8 9 7 5 6 5 8 7 8 8 7 6 6 5 5 6 7 8 5 7 6 6 7 7 9 9 8 9 8 8 8 8 9 9 9 9 8 8 9 9 8 9 9 8 8 8 ...
output:
BRRB RRRBB BRBRBR RRB RRBBR RRBRB BBRR BRBR BBRBRRR BRRRRBB RBR RBRRBB RRB RRRBBRB RRBBRB BRRB RBR BRRRB RRB BRRBR RRBRB BRRB RBBRR RRB RRBBR BBRR BRRBBR RRBRBB RBBR RRBBRB RBBBRR RBRRB RRRBBB BBBRRR RBRBRB BRR RRBB BRR RRB RRRBB RBBR BRBBRR RBR RBBRR BRR RRRBBB BBRRBR RRB BRRBBR RBR BRR BBRR RBRBR ...
result:
ok ok (10000 test cases)
Test #3:
score: 0
Accepted
time: 24ms
memory: 2388kb
input:
1000 98 96 41 39 52 47 34 37 45 33 68 54 74 35 65 58 49 46 53 42 87 30 43 48 38 36 56 40 88 66 32 31 72 44 91 96 51 85 83 61 60 59 80 63 70 80 75 61 51 83 50 69 86 55 79 62 67 57 73 93 96 64 69 91 78 73 80 83 81 91 91 71 76 81 75 90 92 77 82 89 82 86 98 84 96 89 97 96 91 97 94 93 95 97 97 95 96 97 9...
output:
RRRBRBBRRRRRBBBBRBBBBRRRBBRBR BRBRRBBBRBRBRBRRBRRBRBRB BRRRRBRBBBBBRRBBRRBBRRBRBRBRRRRBBRRBBBBRRBBRBBRRRRBRRRRBBRBBRRRBBRRBRRRRBBBBBBB BRBRBBBRBRRBRRRBBBBRRRBRR RBRBBRBBBBRRRBRBBBBBBRBRBRRBBBBBRRBRRBRRRRRRRBBRRR RRRBRRRRBRRRRRBRRBBBBBBBRRBRBBBRBBBB BRRRRRRRRBRBRRBRRBRBBBBRBBRBBBBRRRBBBB RRRBBRB RRBB...
result:
ok ok (1000 test cases)
Test #4:
score: 0
Accepted
time: 23ms
memory: 4564kb
input:
10 9442 9473 6729 7355 6467 7301 7964 7025 7066 7206 8711 8044 7401 6634 6594 9405 7767 7253 7611 6730 6630 8250 6872 6720 8868 8644 9280 7272 6808 8887 7965 7384 6376 9115 8340 7618 8377 9351 8690 8842 9014 6913 7207 7552 8087 9013 9340 6509 8152 6963 8666 8716 7681 6447 8097 7014 6854 8576 6915 92...
output:
BRBBBBBBBBRBBRRRBRBBRRRRRRBRBRBRRRBBBRBBBBBBRBBRRRBBRRBRRRRBRRBBRRBRRBRRBBRRRBBBRRBBRBRBBBBRRBRRBBBBBBBRRRRBBRRBBRRRBBBRRRBBRRBBRRBRRBRBBRBRBRRRBBRRRRRBRRRBBBBBRRBRBRBRRBRRRBRBBBRBBRRRRBBRRBRRBBRBBBBBRRBBBRBRBRRRBBBRBBBRBRRBBBBRRRRRBRBBBRBBBRBRBRBRBBBBBRBBRBRBRBRRRBRBBBBRRBBRRRRRRRBBBRRRRBBBRBBRRBRB...
result:
ok ok (10 test cases)
Test #5:
score: 0
Accepted
time: 18ms
memory: 25444kb
input:
1 100000 100000 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 9...
output:
R
result:
ok ok (1 test case)
Test #6:
score: 0
Accepted
time: 26ms
memory: 25604kb
input:
1 100000 100000 27 17 44 12 22 19 14 21 15 11 48 13 16 20 34 18 24 26 25 28 43 23 33 29 31 30 46 45 41 36 32 38 90 35 40 37 39 55 47 42 59 52 65 72 49 50 54 53 51 64 56 57 66 58 63 82 62 61 60 69 86 95 85 71 68 67 83 70 74 73 77 75 81 76 78 88 89 79 80 84 94 96 123 106 110 87 92 91 99 102 93 98 101 ...
output:
RRRRBBBBBR
result:
ok ok (1 test case)
Test #7:
score: 0
Accepted
time: 24ms
memory: 25640kb
input:
1 100000 100000 218 381 317 660 186 296 679 224 357 361 193 231 232 288 310 183 209 206 182 300 400 344 250 440 519 563 203 333 346 543 427 447 245 289 202 386 221 249 256 359 257 370 200 393 263 270 340 191 240 644 489 522 380 453 241 207 345 719 261 336 364 260 211 541 178 215 383 220 886 403 441 ...
output:
RBRRBRBRBBBRRRRBRRBBRBRRRBRBRRRRBRRRBRBRRBRRRRBRRRRRBRRBBBRBRBBBBBBBRBRBRRBRRRBRRBRBRRBRRBBBBBRRRRRRBBRBBBBBBBRBRRBRRBBBBBBRRRBBBBRBRBRBRBBRBRBRBBBBBRBBRRRRRRBBBBBRBBBBBRRRBBRR
result:
ok ok (1 test case)
Test #8:
score: 0
Accepted
time: 27ms
memory: 25644kb
input:
1 100000 100000 1421 2788 1423 1160 1112 1488 1236 5948 1163 1757 1410 2312 1361 2923 1593 1342 1999 1293 1081 2501 1942 2294 1893 1059 1610 1844 1256 1287 3866 2443 1668 2351 2721 1191 1618 1058 2556 1182 4821 1303 1528 2149 1564 1294 2339 1490 2179 3047 1645 1178 1536 3950 1275 8455 1068 2870 2234...
output:
RRBBRRBBRBBBBRBRBRBBBRRRBRBBBBRBBBBBBRBRBBRBBRRRRRRBBBBRRRRRBBBRRRBRRRRBBBBRRRRRBBRRBRBRBRBBBRBBRRRBRBRBRBBRBBRRBBRBBBBRRRBRRBRBRBBBRRBBBRRBBBRBBBBBRBBRRBRRRBBBRRRBBBBBBBBBBBBBRBRRBRRRRBBBRRBRRBRBBBBRBBRBBBBRRRBBBBRRBRRRBRRRBRBBRRBRRRBBRBRBRBBBBBRBBRRBRRRRRRRBRBBRRBRBBBRRRBRRRRBBBRRRRRBRRRBRRBBBBBRR...
result:
ok ok (1 test case)
Test #9:
score: 0
Accepted
time: 52ms
memory: 25596kb
input:
1 100000 100000 13732 23027 27029 18787 13613 22155 13102 24134 16692 14811 14984 38995 53389 15109 14007 13571 39616 23329 57926 78696 15054 18000 18665 15849 48065 15896 12897 14354 18446 27792 19390 19744 32923 16980 18489 27104 17926 43496 25322 21020 16210 38766 16458 32680 31819 51489 16147 16...
output:
BRBBRBBRRBBRRBBRBBRBBRRRBRBBRRBRBBBRRRRRRRRBRRRBBRRRBRBBRRRBRBBBRBRBRBBBBBBRRBRBRRBBBRBRBBBBBBBBRRBRBBBRBBRRRBRRRBBBRBBBRRRBRBBBRRRRBRRBRBRBRRBBRBBBBRRRRBRBBRBRBBBRBBBRBBBRBBBRRBRBBRBRBBRBBRBBRRBRBBRBRBBBRBBBRBRRBRBBBRRBRBBRRRRRRRRRBBBRBRBBBRRRRRRRRRBRBBBBRBRBRRBRBRRRRBBRRRRBBRRBRRBBRBBBBBBRBBBRBRRR...
result:
ok ok (1 test case)
Test #10:
score: 0
Accepted
time: 53ms
memory: 29384kb
input:
1 100000 100000 96980 98039 98580 93297 93757 97307 99757 94452 92927 98214 98011 93924 93860 95890 92565 93639 93376 92841 93009 93010 98649 93905 99583 97162 96430 99638 99287 94535 94294 98896 92712 98796 96197 94388 99111 96453 94989 97747 95737 96731 97796 98914 95663 93712 93423 94795 96869 98...
output:
BBBRRRRBBBRRBBBRBRBRBBBBRRRRBBRRRBBBRBBBRRBRBRRBBBRBBRBBBRRBBBRRRBRRBBRBRBRBBBBBBBRBRBBBRBBRRBBBBRBBBBRBRRBBRBRRBRBRBRBBBBBRRRBBRRRRRBBRBRRBRBRBBBRBBBRRRBBRRRBRBRRRRRBBBRRRRRBRRRRBRBBBBRRBRBRRBBRRBRRRBBRBRRBBBBRBBRRBRRRBRRRBBBRRBRBBBRRBBRBRRRRRBRBRRBRRRBRRBRBBBBBRRBBBBRBRRBRBRRRBBBRRRRRBRRRBBRRRBRBB...
result:
ok ok (1 test case)
Test #11:
score: 0
Accepted
time: 15ms
memory: 13680kb
input:
1 100000 30 51 28 32 13 22 18 19 20 12 25 21 14 15 16 23 17 29 33 27 24 39 35 26 34 37 46 42 31 30 36 41 40 38 50 48 71 44 79 45 58 43 68 47 93 54 49 53 60 66 57 52 55 63 56 62 67 76 59 64 61 78 65 69 74 70 89 75 118 77 72 73 81 80 84 111 95 87 82 98 85 83 86 90 96 121 91 88 94 92 99 97 100 101 102 ...
output:
RBBBRRRBBRR
result:
ok ok (1 test case)
Test #12:
score: 0
Accepted
time: 0ms
memory: 2084kb
input:
1 3 3 3 3 3 3
output:
RB
result:
ok ok (1 test case)
Extra Test:
score: 0
Extra Test Passed