complete 2600.cpp 6906.cpp 4881.rs 24753.rs 34423.rs

This commit is contained in:
2026-01-22 23:39:54 -08:00
parent ffc4d85822
commit a52b20d5ed
5 changed files with 386 additions and 0 deletions

View File

@@ -0,0 +1,133 @@
#include <iostream>
#include <stdint.h>
#include <vector>
#define let auto
typedef int i32;
typedef size_t usize;
typedef long long i64;
typedef uint64_t u64;
using namespace std;
let fastio() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
}
enum Turn {
A,
B,
};
enum Winnable {
UNDEF,
WIN,
LOSE,
};
typedef struct GS {
u64 b1, b2, b3;
Winnable **dp;
} GameState;
let dfs(GameState *state, Turn turn, u64 k1, u64 k2) -> Winnable {
let opposite = turn == A ? B : A;
if (k2 > k1) {
let temp = k2;
k2 = k1;
k1 = temp;
}
if (state->dp[k1][k2] != UNDEF) {
return state->dp[k1][k2];
} else {
let b1 = state->b1;
let b2 = state->b2;
let b3 = state->b3;
let sigs = vector<Winnable>();
if (k1 >= b1) {
sigs.push_back(dfs(state, opposite, k1 - b1, k2));
}
if (k1 >= b2) {
sigs.push_back(dfs(state, opposite, k1 - b2, k2));
}
if (k1 >= b3) {
sigs.push_back(dfs(state, opposite, k1 - b3, k2));
}
if (k2 >= b1) {
sigs.push_back(dfs(state, opposite, k1, k2 - b1));
}
if (k2 >= b2) {
sigs.push_back(dfs(state, opposite, k1, k2 - b2));
}
if (k2 >= b3) {
sigs.push_back(dfs(state, opposite, k1, k2 - b3));
}
let flag = LOSE;
for (let sig: sigs) {
if (sig == LOSE) {
flag = WIN;
}
}
state->dp[k1][k2] = flag;
return flag;
}
}
const u64 MAX_K = 502;
let main()
-> i32 {
fastio();
let b1 = (u64) 0;
let b2 = (u64) 0;
let b3 = (u64) 0;
cin >> b1 >> b2 >> b3;
let dp = (Winnable **) calloc(MAX_K, sizeof(Winnable *));
{// init
for (u64 i = 0; i < MAX_K; i++) {
dp[i] = (Winnable *) calloc(MAX_K, sizeof(Winnable));
}
for (u64 i = 0; i < b1; i++) {
for (u64 j = 0; j < b1; j++) {
dp[i][j] = LOSE;
}
}
for (u64 i = 0; i < b1; i++) {
dp[i][b1] = WIN;
dp[b1][i] = WIN;
}
dp[b1][b1] = LOSE;
}
let gs = GameState{
.b1 = b1,
.b2 = b2,
.b3 = b3,
.dp = dp};
for (let i = (usize) 0; i < 5; i++) {
u64 k1, k2;
cin >> k1 >> k2;
let sig = dfs(&gs, A, k1, k2);
if (sig == WIN) {
cout << "A" << endl;
} else {
cout << "B" << endl;
}
}
{// delete
for (let i = (u64) 0; i < MAX_K; i++) {
free(dp[i]);
}
free(dp);
}
}

View File

@@ -0,0 +1,94 @@
#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>
#include <vector>
#define let auto
typedef int i32;
typedef long long i64;
typedef size_t usize;
using namespace std;
let fastio() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
}
let is_vowel(char c) -> bool {
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
}
let get_rhyme_part(const string &line) -> string {
if (line.empty()) return "";
let end = (i32) line.length() - 1;
while (end >= 0 && isspace(line[end])) {
end--;
}
if (end < 0) return "";
let start = end;
while (start >= 0 && !isspace(line[start])) {
start--;
}
let word = line.substr(start + 1, end - start);
for (let &c: word) {
c = tolower(c);
}
let vowel_pos = -1;
for (let i = (i32) word.length() - 1; i >= 0; --i) {
if (is_vowel(word[i])) {
vowel_pos = i;
break;
}
}
if (vowel_pos != -1) {
return word.substr(vowel_pos);
} else {
return word;
}
}
let main() -> i32 {
fastio();
let line = string();
getline(cin, line);
if (line.empty()) return 0;
let tc = (usize) stoi(line);
for (let i = (usize) 0; i < tc; i++) {
let rhymes = vector<string>(4);
for (let j = 0; j < 4; j++) {
getline(cin, line);
rhymes[j] = get_rhyme_part(line);
}
let p12 = (rhymes[0] == rhymes[1]);
let p23 = (rhymes[1] == rhymes[2]);
let p34 = (rhymes[2] == rhymes[3]);
let p13 = (rhymes[0] == rhymes[2]);
let p24 = (rhymes[1] == rhymes[3]);
let p14 = (rhymes[0] == rhymes[3]);
if (p12 && p23 && p34) {
cout << "perfect" << endl;
} else if (p12 && p34) {
cout << "even" << endl;
} else if (p13 && p24) {
cout << "cross" << endl;
} else if (p14 && p23) {
cout << "shell" << endl;
} else {
cout << "free" << endl;
}
}
return 0;
}

View File

@@ -0,0 +1,33 @@
use std::io::{read_to_string, stdin};
fn main() {
let temp = read_to_string(stdin()).unwrap();
let mut iter = temp
.split_ascii_whitespace()
.map(|x| x.parse::<usize>().unwrap());
let s = iter.next().unwrap();
let n = iter.next().unwrap();
let arr = (0..n).map(|_| iter.next().unwrap());
let mut seat_stat = vec![false; s];
for i in arr {
seat_stat[i - 1] = true;
}
let mut cnt = 0;
for i in 0..s {
if !seat_stat[i] {
let left = if i == 0 { s - 1 } else { i - 1 };
let right = (i + 1) % s;
if !seat_stat[left] && !seat_stat[right] {
seat_stat[i] = true;
cnt += 1;
}
}
}
println!("{}", cnt);
}

View File

@@ -0,0 +1,67 @@
use std::cmp::max;
use std::io::{read_to_string, stdin};
fn main() {
let temp = read_to_string(stdin()).unwrap();
let mut iter = temp
.split_ascii_whitespace()
.map(|x| x.parse::<i64>().unwrap());
let (t0, t1, t2) = (
iter.next().unwrap(),
iter.next().unwrap(),
iter.next().unwrap(),
);
let ts = [[0, t0, t1], [t0, 0, t2], [t1, t2, 0]];
let n = iter.next().unwrap() as usize;
let pp = {
let mut pp = vec![vec![0; 3]; n];
for i in 0..3 {
for j in 0..n {
pp[j][i] = iter.next().unwrap();
}
}
pp
};
let mut dp = vec![vec![0; 3]; n];
dp[0][0] = pp[0][0];
dp[0][1] = pp[0][1];
dp[0][2] = pp[0][2];
for i in 1..n {
dp[i][0] = max(
dp[i - 1][0] + pp[i][0],
max(
dp[i - 1][1] + pp[i][0] - ts[1][0],
dp[i - 1][2] + pp[i][0] - ts[2][0],
),
);
dp[i][1] = max(
dp[i - 1][1] + pp[i][1],
max(
dp[i - 1][0] + pp[i][1] - ts[0][1],
dp[i - 1][2] + pp[i][1] - ts[2][1],
),
);
dp[i][2] = max(
dp[i - 1][2] + pp[i][2],
max(
dp[i - 1][0] + pp[i][2] - ts[0][2],
dp[i - 1][1] + pp[i][2] - ts[1][2],
),
);
}
let max_cost = dp[n - 1].iter().max().unwrap();
println!("{}", max_cost);
}

View File

@@ -0,0 +1,59 @@
use std::{
collections::HashMap,
io::{read_to_string, stdin},
u64,
};
fn transform(mut x: u64) -> u64 {
let mut s = 0;
while x > 0 {
let t = x % 10;
s += t * t;
x = x / 10;
}
s
}
fn main() {
let temp = read_to_string(stdin()).unwrap();
let mut iter = temp
.split_ascii_whitespace()
.map(|x| x.parse::<u64>().unwrap());
loop {
let mut dist_from_a: HashMap<u64, u64> = HashMap::new();
let mut dist_from_b: HashMap<u64, u64> = HashMap::new();
let (a, b) = (iter.next().unwrap(), iter.next().unwrap());
if a == 0 && b == 0 {
break;
}
let mut curr = a;
let mut step = 1;
while !dist_from_a.contains_key(&curr) {
dist_from_a.insert(curr, step);
curr = transform(curr);
step += 1;
}
curr = b;
step = 1;
while !dist_from_b.contains_key(&curr) {
dist_from_b.insert(curr, step);
curr = transform(curr);
step += 1;
}
let mut res = u64::MAX;
for (&n, &dist_a) in dist_from_a.iter() {
if let Some(dist_b) = dist_from_b.get(&n) {
res = res.min(dist_a + dist_b);
}
}
if res == u64::MAX {
res = 0;
}
println!("{} {} {}", a, b, res);
}
}