#[derive(Debug)]
struct Edge {
to: i32,
cost: i32,
}
fn main() {
let V = 3;
let E = 3;
let matrix = [[0,1], [0,2], [1,2]];
let mut edges: Vec<Edge> = Vec::new();
for i in 0..E {
let ed = Edge { to: matrix[i][0], cost: matrix[i][1]};
edges.push(ed);
}
println!("{:?}", edges);
}
Expedition 2431
fn main() {
let mut N = 4; // gas station num
let P = 10; // initial gas
let L = 25; // distance
let mut A: Vec<i32> = vec![10, 14, 20, 21]; // gas station distance
let mut B: Vec<i32> = vec![10, 5, 2, 4]; // gas volume supply
A.push(L);
B.push(0);
N += 1;
let que; // manage gas station
let mut ans = 0; // num of gas supply
let mut pos = 0; // position
let mut tank = P; // gas volume
for i in 0..N {
let d = A[i] - pos; // distance
while (tank - d < 0) {
if que.empty() {
que.push(-1);
return;
}
tank += que.top();
que.pop();
ans += 1;
}
tank -= d;
pos = A[i];
que.push(B[i]);
}
println!("{}", ans);
}
priority queはstackで大きい順に値を入れていく。
heapのpushとpop
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