QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#360900#7944. Max Minus MinCDastrupTL 797ms2132kbRust3.2kb2024-03-22 14:29:002024-03-22 14:29:01

Judging History

你现在查看的是最新测评结果

  • [2024-03-22 14:29:01]
  • 评测
  • 测评结果:TL
  • 用时:797ms
  • 内存:2132kb
  • [2024-03-22 14:29:00]
  • 提交

answer

use std::{collections::HashMap, io::stdin};

const U:u32 = u32::MAX/4+1;
struct sparse_st<T:Ord+Copy> {
    h: HashMap<(u32,u32),T>
}
impl<T: Ord+Copy> sparse_st<T> {
    fn new() -> Self{
        Self {
            h:HashMap::new()
        }
    }
    fn insert(&mut self, i:u32, v: T) {
        let mut b = i;
        let mut l=1;
        while l<=U {
            let m = self.h.entry((b*l,(b+1)*l)).or_insert(v);
            *m=v.max(*m);
            l*=2;
            b/=2;
        }
    }
    fn query(&self, (mut a,mut b):(u32,u32))->Option<T> {
        let mut ans:Option<T> = None;
        let mut l = 1;
        while l<=U && a!=b{
            if a%2==1 {
                ans=ans.max(self.h.get(&(l*a,l*(a+1))).copied());
            }
            if b%2==1 {
                ans=ans.max(self.h.get(&(l*(b-1),l*b)).copied());
            }
            a=(a+1)/2;
            b=b/2;
            l*=2;
        }
        ans
    }
}
#[derive(PartialEq, Eq, Clone, Copy)]
struct reversed(u32);
impl PartialOrd for reversed {
    fn partial_cmp(&self, other: &Self) -> std::option::Option<std::cmp::Ordering> {
        let reversed(s)=self;
        let reversed(o)=other;
        o.partial_cmp(s)
    }
}
impl Ord for reversed {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        let reversed(s)=self;
        let reversed(o)=other;
        o.cmp(s)
    }
}
fn increase(A:&Vec<u32>) -> u32{
    let m=*A.iter().min().unwrap();
        let M = *A.iter().max().unwrap();
        let mut lo=0;
        let mut hi =M-m+1;
        while hi>lo+1 {
            let x = (hi+lo)/2;
            let mut st1=sparse_st::new();
            let mut st2=sparse_st::new();
            let mut st3=sparse_st::new();
            for (i,v) in A.iter().enumerate() {
                st1.insert(i as u32, v);
                st2.insert(*v, i as u32);
                st3.insert(*v, reversed(i as u32));
            }
            let reversed(a)=st3.query((m,m+x)).unwrap();
            let b=st2.query((m,m+x)).unwrap();
            //println!("{:?}", (x,a,b));
            let bv=st1.query((a,b+1)).unwrap();
            //println!("{:?}", (x,a,b,bv));
            if bv+x>M {
                hi=x;
            } else {
                lo=x;
            }
        }
        lo
}
pub fn main() {
    /*let mut st:sparse_st<u32> = sparse_st::new();
    st.insert(0,5);
    st.insert(1,4);
    st.insert(3,7);
    st.insert(2,0);
    for i in 0..4 {
        for j in i..5 {
            println!("{:?}", (i,j, st.query((i,j))));
        }
    }*/
    let mut s = String::new();
    stdin().read_line(&mut s);
    let t = s.trim().parse::<usize>().unwrap();
    for _ in 0..t {
        let mut s = String::new();
        stdin().read_line(&mut s);
        let mut s = String::new();
        stdin().read_line(&mut s);
        let v = s.trim().split(' ').map(|s| s.parse::<u32>().unwrap()).collect::<Vec<_>>();
        let m=*v.iter().min().unwrap();
        let M = *v.iter().max().unwrap();
        let x=increase(&v);
        let w=v.into_iter().map(|x| M-x+m).collect();
        let y= increase(&w);
        let z=x.max(y);
        println!("{}",M-m-z);
    }
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 2132kb

input:

4
3
42 42 42
4
1 2 2 1
5
1 100 1 100 1
6
1 2 3 4 5 6

output:

0
0
99
2

result:

ok 4 number(s): "0 0 99 2"

Test #2:

score: 0
Accepted
time: 797ms
memory: 2124kb

input:

19530
6
2 3 3 3 4 3
6
2 4 4 2 5 5
6
2 5 2 5 1 3
6
4 4 1 2 4 1
5
5 4 5 3 3
5
2 2 1 5 1
6
3 1 2 4 2 3
5
5 5 4 4 5
6
5 3 3 1 4 2
6
3 3 2 4 2 4
6
1 2 4 5 2 5
5
3 4 5 5 3
6
4 3 3 3 4 3
6
1 5 1 2 3 1
5
5 5 2 1 4
6
1 2 5 3 5 2
6
4 5 2 4 5 2
5
2 4 2 4 1
4
2 3 3 3
6
1 2 2 1 4 5
6
3 2 1 3 3 2
6
2 1 1 2 5 3
6
...

output:

1
2
3
3
1
1
2
0
3
2
3
1
1
2
1
2
3
2
0
1
1
2
0
3
2
2
3
2
2
2
3
3
2
2
1
2
2
2
2
2
2
1
2
1
2
1
2
2
2
1
1
2
2
1
2
2
1
1
1
2
1
2
2
1
2
2
3
2
2
1
1
2
1
2
3
2
0
2
1
1
2
1
1
2
2
2
1
3
2
1
2
3
2
1
1
2
2
3
1
1
1
2
2
1
1
1
1
2
2
2
2
2
3
2
1
2
0
1
1
1
1
1
1
2
2
2
2
2
2
1
2
1
2
4
1
1
1
3
2
2
2
1
2
1
2
1
2
2
1
3
...

result:

ok 19530 numbers

Test #3:

score: -100
Time Limit Exceeded

input:

1
199996
95228303 201285494 63848235 748936712 940169142 379639162 189291770 224201078 335564466 792345215 948056869 35198826 312793561 194588099 297198853 665606109 163797196 584404459 996890415 458867609 331820116 713293915 858136035 520976262 519894660 918315819 660535535 639896052 141007070 1151...

output:


result: