fn push(x: i32) { let mut heap = [1, 2, 3, 4, 5, 6, 7, 8]; let sz = 0; let mut i = sz + 1; while(i > 0) { let p = (i -1) / 2; if heap[p] <= x { break; } heap[i] = heap[p]; i = p; } heap[i] = x; } fn pop() { let mut heap = [1, 2, 3, 4, 5, 6, 7, 8]; let sz = 0; let ret = heap[0]; let x = heap[heap.len() -1]; let mut i = 0; while(i * 2 + 1 < sz) { let mut a = i * 2 + 1; let mut b = i * 2 + 2; if (b < sz && heap[b] < heap[a]) { a = b; } if (heap[a] >= x) { break; } heap[i] = heap[a]; i = a; } heap[i] = x; }
dynamic programing
use std::collections::HashMap; fn rec (i: i32, j: i32) -> i32{ let n = 4; let w = [ [2,3], [1,2], [3,4], [2,2] ]; let res; if(i == n) { res = 0; } else if (j < w[i as usize][0]) { res = rec(i + 1, j); } else { res = std::cmp::max(rec(i + 1, j), rec(i + 1, j - w[i as usize][0]) + w[i as usize][1]); } return res; } fn main() { let W = 5; let res = rec(0, W); println!("{}", res); }
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.52s
Running `target/debug/basic`
7
hashmapではなく、多次元配列にする必要あり
硬貨の問題
fn main() { let C: [i32; 6] = [3, 2, 1, 3, 0, 2]; let mut A = 620; let V: [i32; 6] = [1, 5, 10, 50, 100, 500]; let mut ans: i32 = 0; for i in (0..6).rev() { let t = std::cmp::min(A / V[i], C[i]); A -= t * V[i]; ans += t; } println!("{}", ans); }
Lake Counting 2386
const N: i32 = 10; const M: i32 = 12; fn dfs(x: i32, y: i32) { field[x][y] = ".".to_string(); for (let dx in -1..1) { for (let dy in -1..1) { let nx = x + dx; let ny = y + dy; if(0 <= nx && nx < N && 0 <= ny && ny < M && field[nx][ny] == 'W') { dfs(nx, ny); } } } } fn main() { let field[N, M]; let mut res = 0; for i in 0..N { for j in 0..M { if field[i][j] == 'W' { dfs(i, j); res =+ 1; } } } println!("{}", res); }
全探索
fn dfs(i: i32, sum: i32) -> bool { let k = 10; let n = 10; if (i == n) { return sum == k; } if (dfs(i + 1, sum)) { return true; } if (dfs(i + 1, sum + i)) { return true; } return false; } fn main() { if (dfs (0, 0)) { println!("yes"); } else { println!("no"); } }
Ants POJ 1852
fn main() { let n = 10; let L = 100; let mut minT: i32 = 0; for i in 0..n { minT = std::cmp::max(minT, i.min(L - i)); } let mut maxT: i32 = 0; for i in 0..n { maxT = std::cmp::max(minT, std::cmp::max(i, L - i)); } println!("max:{}, min: {}", minT, maxT); }
max:9, min: 91
三角形の周長
fn main() { let mut ans: i32 = 0; let n: i32 = 10; for i in 0..n { let j = i + 1; for j in j..n { let k = j + 1; for k in k..n { let len = i + j + k; let ma = std::cmp::max(i, std::cmp::max(j, k)); let rest = len - ma; if (ma < rest) { ans = std::cmp::max(ans, len); } } } } println!("{}", ans); }
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.14s
Running `target/debug/basic`
24
【Blockchain】Avalancheのノードの稼働
$ wget -nd -m https://raw.githubusercontent.com/ava-labs/avalanche-docs/master/scripts/avalanchego-installer.sh;\
chmod 755 avalanchego-installer.sh;\
./avalanchego-installer.sh
$ ./avalanchego –network-id=local –staking-enabled=false –snow-sample-size=1 –snow-quorum-size=1 –http-host=0.0.0.0
【Blockchain】Cardanoのノードの稼働
### 依存パッケージのインストール
$ sudo apt update
$ sudo apt install -y libssl-dev build-essential m4 jq curl
MithrilクライアントはRustで開発されている
### Rustツールチェーンのインストール
$ curl –proto ‘=https’ –tlsv1.2 -sSf https://sh.rustup.rs | sh
$ source $HOME/.cargo/env
$ rustup install stable
$ rustup default stable
$ rustup update
$ rustup component add clippy rustfmt
$ rustup target add x86_64-unknown-linux-musl
Mithrilクライアントは、Cardanoノードのデータベースを迅速に同期するためのツール
$ git clone https://github.com/input-output-hk/mithril.git
$ cd mithril
$ ls
CHANGELOG.md NOTICE internal mithril-install.sh
CODE-OF-CONDUCT.md README.md mithril-aggregator mithril-relay
CONTRIBUTING.md SECURITY.md mithril-client mithril-signer
Cargo.lock demo mithril-client-cli mithril-stm
Cargo.toml docs mithril-client-wasm mithril-test-lab
DEV-ADR.md examples mithril-common networks.json
LICENSE flake.lock mithril-explorer openapi.yaml
Makefile flake.nix mithril-infra
$ git fetch –all –prune
$ git checkout tags/2403.1
$ cd mithril-client-cli
$ make build
$ ./mithril-client -V
$ sudo mv mithril-client /usr/local/bin/mithril-client
$ mithril-client cardano-db snapshot list
$ mithril-client cardano-db snapshot download –snapshot-id
【Blockchain】ethereumのlight nodeをサーバで動かしたい
### ubuntuにaptよりインストール
$ sudo add-apt-repository -y ppa:ethereum/ethereum
$ sudo apt update
$ sudo apt install -y geth
### light modeについて
$ geth –syncmode “light”
INFO [06-02|12:21:33.754] Starting Geth on Ethereum mainnet…
INFO [06-02|12:21:33.756] Bumping default cache on mainnet provided=1024 updated=4096
INFO [06-02|12:21:33.762] Maximum peer count ETH=50 total=50
INFO [06-02|12:21:33.767] Smartcard socket not found, disabling err=”stat /run/pcscd/pcscd.comm: no such file or directory”
WARN [06-02|12:21:33.773] Sanitizing cache to Go’s GC limits provided=4096 updated=654
Fatal: –syncmode: unknown sync mode “light”, want “full” or “snap”
lightクライアント機能は削除されている模様
### snap (軽量なフルノード)
数時間で同期可能
400GB以上が必要
$ geth –syncmode=snap