This commit is contained in:
arielherself 2023-12-20 00:32:15 +08:00
parent 3cba6f22d7
commit 1aeecc6ba7
19 changed files with 576 additions and 2 deletions

8
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

11
.idea/RustIsBestLang.iml Normal file
View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="EMPTY_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/RustIsBestLang.iml" filepath="$PROJECT_DIR$/.idea/RustIsBestLang.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

7
Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "RustIsBestLang"
version = "0.1.0"

81
src/bin/cf-1886c.rs Normal file
View File

@ -0,0 +1,81 @@
use std::io::stdin;
use std::collections::VecDeque;
fn calc_sum(n:i32,k:i32) -> i64 {
n as i64 * k as i64 - (k as i64 - 1)* k as i64 / 2
}
fn lower_bound(n:i32,x:i64) -> (i32,i64) {
let mut l = 0;
let mut r = n;
while l != r {
let mid = l + r + 1 >> 1;
if calc_sum(n,mid) >= x {
r = mid - 1;
} else {
l = mid;
}
}
(l, x-calc_sum(n,l))
}
fn solve() {
let mut buf = String::new();
stdin().read_line(&mut buf).ok();
let at = buf.to_owned();
let a = at.trim().as_bytes();
let n = a.len();
let mut ena = vec![true;n];
let mut cnt = 0;
let mut st:VecDeque<usize> = VecDeque::new();
buf.clear();
stdin().read_line(&mut buf).ok();
let pos:i64 = buf.trim().parse().unwrap();
let (prev, diff) = lower_bound(n as i32,pos);
// println!("{prev} {diff}");
for (i,&c) in a.iter().enumerate() {
if cnt == prev{
break;
}
while let Some(&j) = st.back() {
if a[j] <= c {
break;
}
cnt += 1;
ena[j] = false;
if cnt == prev {
break;
}
st.pop_back();
}
st.push_back(i);
}
for i in (0..n).rev() {
if cnt == prev {
break;
}
if ena[i] {
ena[i] = false;
cnt+=1;
}
}
let mut p = 0;
for (i,&c) in a.iter().enumerate() {
if ena[i] {
p += 1;
if p == diff {
print!("{}",c as char);
return;
}
}
}
}
fn main() {
let mut buf = String::new();
stdin().read_line(&mut buf).ok();
let t:i32 = buf.trim().parse().unwrap();
for _ in 0..t {
solve();
}
}

31
src/bin/cf-620c.rs Normal file
View File

@ -0,0 +1,31 @@
fn main() {
use std::io::stdin;
use std::collections::HashSet;
let mut buf:String = String::new();
stdin().read_line(&mut buf).ok();
let n:usize = buf.trim().parse().unwrap();
buf.clear();
let mut a:Vec<i32> = Vec::with_capacity(n);
stdin().read_line(&mut buf).ok();
buf.trim().split(' ').for_each(|val| a.push(val.parse().unwrap()));
let mut mp:HashSet<i32> = HashSet::new();
let mut res = vec![];
let mut l = 0usize;
for i in 0..n {
if mp.contains(&a[i]) {
res.push([l,i]);
l = i + 1;
mp.clear();
} else {
mp.insert(a[i]);
}
}
if res.is_empty() {
println!("-1");
} else {
let m = res.len();
println!("{}",res.len());
res[m-1] = [res[m-1][0],n-1];
res.into_iter().for_each(|[l,r]| println!("{} {}",l+1,r+1));
}
}

43
src/bin/lc-15.rs Normal file
View File

@ -0,0 +1,43 @@
struct Solution;
impl Solution {
pub fn three_sum(nums: Vec<i32>) -> Vec<Vec<i32>> {
let mut a = nums.to_owned();
a.sort();
let n = a.len();
let mut res:Vec<Vec<i32>> = vec![];
for i in 0..n-2 {
if i>0 && a[i]==a[i-1] { continue; }
let mut l = i + 1;
let mut r = n - 1;
while l < r {
if a[l] + a[r] == -a[i] {
res.push(vec![a[i],a[l],a[r]]);
}
if a[l] + a[r] < -a[i] {
while l < r {
l += 1;
if a[l] != a[l-1] { break; }
}
} else {
while l < r {
r -= 1;
if a[r] != a[r+1] { break; }
}
}
}
}
res
}
}
fn main() {
let nums = vec![-1,0,1,2,-1,-4];
let res = Solution::three_sum(nums);
for x in res {
for y in x {
println!("{y} ");
}
println!();
}
}

35
src/bin/lc-179.rs Normal file
View File

@ -0,0 +1,35 @@
struct Solution;
impl Solution {
pub fn largest_number(nums: Vec<i32>) -> String {
use std::cmp::{Ordering, max};
let n = nums.len();
let mut a = Vec::with_capacity(n);
for x in nums {
a.push(x.to_string().into_bytes());
}
a.sort_by(|s,t| {
let sl = s.len();
let tl = t.len();
let L = max(sl,tl);
for i in 0..L {
let i = L % sl;
let j = L % tl;
if s[i]!=t[j] {
return t[j].cmp(&s[i]);
}
}
Ordering::Equal
});
let mut s = String::new();
for x in a {
s.push_str(String::from_utf8(x).unwrap().as_str());
}
s
}
}
fn main() {
let nums = vec![10,2];
println!("{}",Solution::largest_number(nums));
}

53
src/bin/lc-22.rs Normal file
View File

@ -0,0 +1,53 @@
use std::collections::VecDeque;
struct Solution;
struct St {
n:i32,
stack:VecDeque<i32>,
res:Vec<String>
}
impl St {
fn solve(&mut self, i: i32, open: i32, close: i32){
if i == 2 * self.n {
let mut s = String::with_capacity(self.n as usize);
for x in &self.stack {
match x {
1 => s.push('('),
0 => s.push(')'),
_ => ()
}
}
self.res.push(s);
return;
}
if open < self.n {
self.stack.push_back(1);
self.solve(i+1,open+1,close);
self.stack.pop_back();
}
if close < open {
self.stack.push_back(0);
self.solve(i+1,open,close+1);
self.stack.pop_back();
}
}
}
impl Solution {
pub fn generate_parenthesis(n: i32) -> Vec<String> {
let mut st = St{n, stack: Default::default(), res: vec![] };
st.solve(0,0,0);
st.res
}
}
fn main() {
let n = 3;
let res = Solution::generate_parenthesis(n);
for s in &res {
println!("{s}");
}
}

33
src/bin/lc-2448.rs Normal file
View File

@ -0,0 +1,33 @@
struct Solution;
impl Solution {
pub fn min_cost(nums: Vec<i32>, cost: Vec<i32>) -> i64 {
let n = nums.len();
let mut idx = (0..n).collect::<Vec<usize>>();
idx.sort_by(|&i,&j| nums[i].cmp(&nums[j]));
let mut l = 0;
let mut r = 0;
cost.iter().for_each(|&x|r+=x as i64);
let mut min_diff = (r - l).abs();
let mut med = 0;
for (t,&i) in idx.iter().enumerate() {
r -= cost[i] as i64;
l += cost[i] as i64;
if (r - l).abs() < min_diff {
min_diff = (r-l).abs();
med = if r > l { nums[idx[t + 1]] } else { nums[i] };
}
}
let mut res = 0;
for i in 0..n {
res += (nums[idx[i]] - med).abs() as i64 * cost[idx[i]] as i64;
}
res
}
}
fn main() {
let nums = vec![1,3,5,2];
let cost = vec![2,3,1,14];
println!("{}",Solution::min_cost(nums,cost));
}

9
src/bin/lc-2454.rs Normal file
View File

@ -0,0 +1,9 @@
struct Solution {}
impl Solution {
pub fn second_greater_element(nums: Vec<i32>) -> Vec<i32> {
let n = nums.len();
}
}
fn main() {}

39
src/bin/lc-2589.rs Normal file
View File

@ -0,0 +1,39 @@
struct Solution;
impl Solution {
pub fn find_minimum_time(tasks: Vec<Vec<i32>>) -> i32 {
use std::collections::BinaryHeap;
use std::cmp::{min, max};
let mut tasks = tasks.to_owned();
tasks.sort();
let mut cnt = 0;
let mut res = 0;
let mut pq:BinaryHeap<(i32,i32)> = BinaryHeap::new();
for it in &tasks {
let s = it[0];
let e = it[1];
let req = it[2];
while let Some(&(pe,pc)) = pq.peek() {
if -pe >= s {
pq.pop();
let new_pc = min(pc, - pe - s + 1);
pq.push((pe, new_pc));
cnt -= pc - new_pc;
break;
}
pq.pop();
cnt -= pc;
}
let c = max(req - cnt, 0);
cnt += c;
res += c;
pq.push((-e,c));
}
res
}
}
fn main() {
let tasks = vec![vec![2,3,1],vec![4,5,1],vec![1,5,2]];
println!("{}",Solution::find_minimum_time(tasks));
}

22
src/bin/lc-2697.rs Normal file
View File

@ -0,0 +1,22 @@
struct Solution {}
impl Solution {
pub fn make_smallest_palindrome(s: String) -> String {
let n = s.len();
let r = n >> 1;
let mut ss = s.into_bytes();
for i in 0..r {
if ss[i] > ss[n - 1 - i] {
ss[i] = ss[n-1-i];
} else {
ss[n-1-i] = ss[i];
}
}
String::from_utf8(ss).unwrap()
}
}
fn main() {
let s = "atie".to_string();
println!("{}",Solution::make_smallest_palindrome(s));
}

19
src/bin/lc-2828.rs Normal file
View File

@ -0,0 +1,19 @@
struct Solution;
impl Solution {
pub fn is_acronym(words: Vec<String>, s: String) -> bool {
let n = words.len();
let mut s = s.as_bytes();
if s.len()!=n {return false;}
for i in 0..n {
if words[i].as_bytes()[0] != s[i] {
return false;
}
}
true
}
}
fn main() {
}

80
src/bin/lc-321.rs Normal file
View File

@ -0,0 +1,80 @@
use std::collections::VecDeque;
struct Solution;
impl Solution {
pub fn max_number(a: Vec<i32>, b: Vec<i32>, k: i32) -> Vec<i32> {
let k = k as usize;
let mut i = 0; let n = a.len();
let mut j = 0; let m = b.len();
let mut st1 = Vec::new();
let mut st2 = Vec::new();
let mut cnt = 0;
while i<n || j<m {
if j==m || i<n && (a[i]>b[j] || j+1==m || i+1!=n && a[i+1]>b[j+1]) {
while let Some(t) = st1.last() {
if cnt-1+n+m-i-j<k || &a[i]<=t {break;}
cnt -= 1;
st1.pop();
}
cnt+=1;
st1.push(a[i]);
i+=1;
} else {
while let Some(t) = st2.last() {
if cnt-1+n+m-i-j<k || &b[j]<=t {break;}
cnt -= 1;
st2.pop();
}
cnt+=1;
st2.push(b[j]);
j+=1;
}
}
let mut res = Vec::with_capacity(k);
i = 0;
j = 0;
while res.len()<k {
if j == st2.len() {
res.push(st1[i]);
i += 1;
} else if i == st1.len() {
res.push(st2[j]);
j += 1;
} else {
if st1[i] > st2[j] {
res.push(st1[i]);
i += 1;
} else if st1[i] < st2[j] {
res.push(st2[j]);
j += 1;
} else {
if i + 1 == st1.len() {
res.push(st2[j]);
j += 1;
} else if j + 1 == st2.len() {
res.push(st1[i]);
i += 1;
} else if st1[i+1]>st2[j+1] {
res.push(st1[i]);
i += 1;
} else {
res.push(st2[j]);
j += 1;
}
}
}
}
res
}
}
fn main() {
let nums1 = vec![3,9];
let nums2 = vec![8,9];
let k = 3;
let res = Solution::max_number(nums1,nums2, k);
for x in res {
print!("{x} ");
}
}

41
src/bin/lc-376.rs Normal file
View File

@ -0,0 +1,41 @@
struct Solution;
impl Solution {
pub fn wiggle_max_length(nums: Vec<i32>) -> i32 {
use std::collections::VecDeque;
type pii = (i32,i32);
let mut up:VecDeque<pii> = VecDeque::new();
let mut down:VecDeque<pii> = VecDeque::new();
let mut up_max = 0;
let mut down_max = 0;
for x in nums {
while let Some(&(y,_)) = up.back() {
if y < x { break; }
up.pop_back();
}
while let Some(&(y,_)) = down.back() {
if y > x { break; }
down.pop_back();
}
let mut u = 1;
let mut d = 1;
if let Some(&(_,l)) = up.back() {
u = u.max(l+1);
}
if let Some(&(_,l)) = down.back() {
d = d.max(l+1);
}
up_max = up_max.max(u);
down_max = down_max.max(d);
down.push_back((x,u));
up.push_back((x,d));
}
up_max.max(down_max)
}
}
fn main() {
let nums = vec![1,7,4,9,2,5];
let b = nums[1..2].iter().collect::<Vec<&i32>>();
println!("{}",Solution::wiggle_max_length(nums));
}

36
src/bin/lc-5.rs Normal file
View File

@ -0,0 +1,36 @@
struct Solution;
impl Solution {
pub fn longest_palindrome(s: String) -> String {
use std::cmp::max;
let n = s.len();
let mut res = (0,0);
let mut mx = 0;
let mut dp=vec![vec![false;n];n];
let ss = s.into_bytes();
for i in (0..n).rev() {
for j in i..n {
let &c = &ss[i];
let &d = &ss[j];
if j == i {
dp[i][j] = true;
} else if j == i + 1 {
dp[i][j] = c==d;
} else {
dp[i][j] = dp[i+1][j-1] && c==d;
}
if dp[i][j] && (j-i+1 > mx) {
mx = max(mx,j-i+1);
res = (i,j);
}
}
}
let (i,j) = res;
String::from_utf8(ss[i..=j].to_owned()).unwrap()
}
}
fn main() {
let s = String::from("babad");
println!("{}",Solution::longest_palindrome(s));
}

View File

@ -1,3 +1,15 @@
fn main() { trait Printable<T> {
println!("Hello, world!"); fn print(a:T);
}
struct Test;
impl<T> Printable<T> for Test where T: std::fmt::Display {
fn print(a:T) {
println!("{a}");
}
}
fn main() {
<Test>::print(1.5);
} }