結果
| 問題 |
No.1604 Swap Sort:ONE
|
| コンテスト | |
| ユーザー |
fukafukatani
|
| 提出日時 | 2021-07-17 23:39:13 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
AC
|
| 実行時間 | 1 ms / 2,000 ms |
| コード長 | 1,915 bytes |
| コンパイル時間 | 22,999 ms |
| コンパイル使用メモリ | 376,792 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-07-07 14:27:31 |
| 合計ジャッジ時間 | 12,283 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 24 |
コンパイルメッセージ
warning: unused variable: `n`
--> src/main.rs:19:9
|
19 | let n = read::<usize>();
| ^ help: if this is intentional, prefix it with an underscore: `_n`
|
= note: `#[warn(unused_variables)]` on by default
warning: function `argsort` is never used
--> src/main.rs:34:4
|
34 | fn argsort<T: std::cmp::Ord + std::marker::Copy>(v: &Vec<T>) -> Vec<usize> {
| ^^^^^^^
|
= note: `#[warn(dead_code)]` on by default
ソースコード
#![allow(unused_imports)]
use std::cmp::*;
use std::collections::*;
use std::io::Write;
use std::ops::Bound::*;
#[allow(unused_macros)]
macro_rules! debug {
($($e:expr),*) => {
#[cfg(debug_assertions)]
$({
let (e, mut err) = (stringify!($e), std::io::stderr());
writeln!(err, "{} = {:?}", e, $e).unwrap()
})*
};
}
fn main() {
let n = read::<usize>();
let p = read_vec::<usize>();
println!("{}", count_inversion(&p));
}
fn count_inversion(v: &Vec<usize>) -> i64 {
let mut ans = 0;
let mut bt = BinaryIndexTree::new(v.len());
for i in 0..v.len() {
ans += i as i64 - bt.sum(v[i]);
bt.add(v[i], 1);
}
ans
}
fn argsort<T: std::cmp::Ord + std::marker::Copy>(v: &Vec<T>) -> Vec<usize> {
let mut sort_v = Vec::new();
for i in 0..v.len() {
sort_v.push((v[i], i));
}
sort_v.sort();
sort_v.iter().map(|x| x.1).collect::<Vec<usize>>()
}
struct BinaryIndexTree {
bit: Vec<i64>,
n: usize,
}
impl BinaryIndexTree {
fn new(n: usize) -> BinaryIndexTree {
BinaryIndexTree {
bit: vec![0; n + 1],
n: n,
}
}
fn sum(&self, i: usize) -> i64 {
let mut i = i;
let mut s = 0i64;
while i > 0 {
s += self.bit[i];
i -= (i as i64 & -(i as i64)) as usize;
}
s
}
fn add(&mut self, i: usize, x: i64) {
let mut i = i;
while i <= self.n {
self.bit[i as usize] += x;
i += (i as i64 & -(i as i64)) as usize;
}
}
}
fn read<T: std::str::FromStr>() -> T {
let mut s = String::new();
std::io::stdin().read_line(&mut s).ok();
s.trim().parse().ok().unwrap()
}
fn read_vec<T: std::str::FromStr>() -> Vec<T> {
read::<String>()
.split_whitespace()
.map(|e| e.parse().ok().unwrap())
.collect()
}
fukafukatani