GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — rustforces/src/pairs_1463D.rs at master · schiemon/rustforces

Expand file tree

/

pairs_1463D.rs

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
138 lines (108 loc) · 3.15 KB
/

pairs_1463D.rs

Copy path

File metadata and controls

138 lines (108 loc) · 3.15 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#![allow(dead_code)]
#![allow(unused_variables)]
#![allow(unused_mut)]
use std::mem::swap;
fn is_pairable(A: &[u32], B: &[u32]) -> bool {
assert_eq!(A.len(), B.len());
for i in 0..A.len() {
if !(A[i] < B[i]) {
return false;
}
}
return true;
}
const MULTI_TEST: bool = true;
fn solve<B: std::io::BufRead, W: std::io::Write>(
read: &mut Reader<B>,
write: &mut std::io::BufWriter<W>,
) {
let n = read.next::<usize>();
let mut B: Vec<u32> = Vec::with_capacity(n);
for i in 0..n {
B.push(read.next::<u32>());
}
let mut A = Vec::with_capacity(n);
let mut last = 0;
let mut cs = Vec::with_capacity(1 + n + 1);
// Get complement.
cs.push(0);
cs.extend(B.iter().cloned());
cs.push(2 * n as u32 + 1);
for i in 0..n + 1 {
A.extend(cs[i] + 1..cs[i + 1])
}
let mut limits = Vec::new();
for _ in 0..2 {
// Get max value for x.
let mut lo = 0;
let mut hi = n;
while lo < hi {
let x = lo + (hi - lo + 2 - 1) / 2;
if is_pairable(&B[..x], &A[n - x..]) {
lo = x;
} else {
hi = x - 1;
}
}
limits.push(lo); // Yeah, push them!
swap(&mut B, &mut A);
}
let hi = limits[0];
let lo = n - limits[1];
assert!(hi >= lo);
println!("{}", hi - lo + 1);
}
pub fn main() {
let (stdin, stdout) = (std::io::stdin(), std::io::stdout());
let mut read = Reader::new(stdin.lock());
let mut write = std::io::BufWriter::new(stdout.lock());
let t = if MULTI_TEST { read.next::<u32>() } else { 1 };
for _ in 0..t {
solve(&mut read, &mut write);
}
}
pub struct Reader<B> {
reader: B,
buf_str: Vec<u8>,
buf_iter: std::str::SplitWhitespace<'static>,
}
impl<B: std::io::BufRead> Reader<B> {
pub fn new(reader: B) -> Self {
Self {
reader,
buf_str: Vec::new(),
buf_iter: "".split_whitespace(),
}
}
pub fn next<T: std::str::FromStr>(&mut self) -> T {
loop {
if let Some(token) = self.buf_iter.next() {
return token.parse().ok().expect("Failed parse");
}
// First, clear the buffer. We want to read new data now.
self.buf_str.clear();
// Read the next line.
self.reader
.read_until(b'\n', &mut self.buf_str)
.expect("Failed to read the next line.");
// Split the line into tokens and buffer them.
self.buf_iter = unsafe {
let slice = std::str::from_utf8_unchecked(&self.buf_str);
let split_slice = slice.split_whitespace();
std::mem::transmute(split_slice)
}
}
}
pub fn next_vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
let mut v = Vec::with_capacity(n);
for _ in 0..n {
v.push(self.next());
}
v
}
pub fn next_pair<T: std::str::FromStr>(&mut self) -> (T, T) {
let first = self.next();
let second = self.next();
(first, second)
}
}
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.