結果

問題 No.5002 stick xor
ユーザー 最新の錆最新の錆
提出日時 2018-05-31 00:16:26
言語 Rust
(1.77.0)
結果
AC  
実行時間 8 ms / 1,000 ms
コード長 3,873 bytes
コンパイル時間 4,765 ms
実行使用メモリ 5,108 KB
スコア 18,777
最終ジャッジ日時 2018-05-31 00:16:33
ジャッジサーバーID
(参考情報)
judge8 /
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
5,100 KB
testcase_01 AC 7 ms
5,104 KB
testcase_02 AC 6 ms
5,108 KB
testcase_03 AC 6 ms
5,104 KB
testcase_04 AC 6 ms
5,104 KB
testcase_05 AC 6 ms
5,100 KB
testcase_06 AC 6 ms
5,104 KB
testcase_07 AC 6 ms
5,104 KB
testcase_08 AC 5 ms
5,100 KB
testcase_09 AC 6 ms
5,100 KB
testcase_10 AC 6 ms
5,104 KB
testcase_11 AC 5 ms
5,104 KB
testcase_12 AC 6 ms
5,104 KB
testcase_13 AC 5 ms
5,104 KB
testcase_14 AC 6 ms
5,104 KB
testcase_15 AC 6 ms
5,104 KB
testcase_16 AC 6 ms
5,104 KB
testcase_17 AC 7 ms
5,100 KB
testcase_18 AC 6 ms
5,100 KB
testcase_19 AC 6 ms
5,104 KB
testcase_20 AC 7 ms
5,104 KB
testcase_21 AC 8 ms
5,100 KB
testcase_22 AC 6 ms
5,104 KB
testcase_23 AC 6 ms
5,104 KB
testcase_24 AC 6 ms
5,100 KB
testcase_25 AC 6 ms
5,104 KB
testcase_26 AC 6 ms
5,104 KB
testcase_27 AC 6 ms
5,104 KB
testcase_28 AC 6 ms
5,100 KB
testcase_29 AC 6 ms
5,100 KB
testcase_30 AC 6 ms
5,100 KB
testcase_31 AC 6 ms
5,104 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io::{Read, stdin};
use std::cmp::max;

fn main() {
    let mut buf = String::new();
    stdin().read_to_string(&mut buf).unwrap();
    let mut tok = buf.split_whitespace();
    let mut get = || tok.next().unwrap();
    macro_rules! get {
        ($t:ty) => (get().parse::<$t>().unwrap());
        () => (get!(usize));
    }
    
    let n = get!();
    let k = get!();
    let mut ls = vec![0; k];
    let mut lmax = 0;
    for i in 0..ls.len() {
        ls[i] = get!();
        lmax = max(lmax, ls[i]);
    }
    let mut lcount = vec![0; lmax+1];
    for &l in ls.iter() {
        lcount[l] += 1;
    }
    let mut field = vec![vec![]; n];
    for i in 0..field.len() {
        let a = get().as_bytes();
        for j in 0..a.len() {
            field[i].push((a[j] - b'0') as i32);
        }
    }
    
    let mut ans = vec![vec![]; lmax+1];
    
    for y in 0..n {
        for x in 0..n {
            if field[y][x] == 0 {
                continue;
            }
            let mut gs = vec![];
            let mut bs = vec![];
            let mut cnt_h = 1;
            let mut cnt_v = 1;
            for p in 1..lmax {
                if lcount[p+1] == 0 {
                    continue;
                }
                let l = 1+p as i32;
                if x+p < n {
                    cnt_h += field[y][x+p];
                    let value = -cnt_h * 100000 / l;
                    if cnt_h+cnt_h > l {
                        gs.push((value, p, true));
                    } else {
                        let value = lcount[p+1] as i32;
                        bs.push((-value, p, false));
                    }
                }
                if y+p < n {
                    cnt_v += field[y+p][x];
                    let value = -cnt_v * 100000 / l;
                    if cnt_v+cnt_v > l {
                        gs.push((value, p, false));
                    } else {
                        let value = lcount[p+1] as i32;
                        bs.push((-value, p, false));
                    }
                }
            }
            if !gs.is_empty() {
                gs.sort();
                if let Some((_, p, h)) = gs.pop() {
                    lcount[p+1] -= 1;
                    if h {
                        ans[p+1].push((y, x, y, x+p));
                        for x in x..x+p+1 {
                            field[y][x] = 1 - field[y][x];
                        }
                    } else {
                        ans[p+1].push((y, x, y+p, x));
                        for y in y..y+p+1 {
                            field[y][x] = 1 - field[y][x];
                        }
                    }
                }
            } else if !bs.is_empty() {
                bs.sort();
                if let Some((_, p, h)) = bs.pop() {
                    lcount[p+1] -= 1;
                    if h {
                        ans[p+1].push((y, x, y, x+p));
                        for x in x..x+p+1 {
                            field[y][x] = 1 - field[y][x];
                        }
                    } else {
                        ans[p+1].push((y, x, y+p, x));
                        for y in y..y+p+1 {
                            field[y][x] = 1 - field[y][x];
                        }
                    }
                }
            } else if lcount[1] > 0 {
                lcount[1] -= 1;
                ans[1].push((y, x, y, x));
                field[y][x] = 1 - field[y][x];
            }
        }
    }
    
    let mut emp = 0;
    for &l in ls.iter() {
        if let Some((y1, x1, y2, x2)) = ans[l].pop() {
            println!("{} {} {} {}", y1+1, x1+1, y2+1, x2+1);
        } else {
            println!("{} {} {} {}",
                n,
                n-l+1,
                n,
                n,
            );
            emp += 1;
        }
    }
    eprintln!("emp {}", emp);
}
0