当前位置:网站首页>2022-07-03: there are 0 and 1 in the array. Be sure to flip an interval. Flip: 0 becomes 1, 1 becomes 0. What is the maximum number of 1 after turning? From little red book. 3.13 written examination.
2022-07-03: there are 0 and 1 in the array. Be sure to flip an interval. Flip: 0 becomes 1, 1 becomes 0. What is the maximum number of 1 after turning? From little red book. 3.13 written examination.
2022-07-04 03:37:00 【Fuda frame constructor daily question】
2022-07-03: In the array are 0 and 1, Be sure to flip an interval , Flip :0 change 1,1 change 0.
Can you turn it over to make 1 What is the maximum number of ?
From little red book .3.13 written examination .
answer 2022-07-03:
Some interval ,0 Number -1 Number = Maximum .
0 become 1,1 become -1, Find the maximum cumulative sum of subarrays .
The code to use rust To write . The code is as follows :
use rand::Rng;
fn main() {
let nn: i32 = 100;
let test_time: i32 = 20000;
println!(" Beginning of the test ");
for i in 0..test_time {
let n = rand::thread_rng().gen_range(0, nn) + 1;
let mut arr = random_array(n);
let mut arr2 = arr.clone();
let ans1 = max_one_numbers1(&mut arr);
let ans2 = max_one_numbers2(&mut arr2);
if ans1 != ans2 {
println!(" Something went wrong !{}", i);
println!("ans1 = {}", ans1);
println!("ans2 = {}", ans2);
break;
}
}
println!(" End of test ");
}
fn max_one_numbers1(arr: &mut Vec<i32>) -> i32 {
let mut ans = 0;
for l in 0..arr.len() as i32 {
for r in l..arr.len() as i32 {
reverse(arr, l, r);
ans = get_max(ans, one_numbers(arr));
reverse(arr, l, r);
}
}
return ans;
}
fn reverse(arr: &mut Vec<i32>, l: i32, r: i32) {
for i in l..=r {
arr[i as usize] ^= 1;
}
}
fn one_numbers(arr: &mut Vec<i32>) -> i32 {
let mut ans = 0;
for num in arr.iter() {
ans += *num;
}
return ans;
}
fn get_max<T: Clone + Copy + std::cmp::PartialOrd>(a: T, b: T) -> T {
if a > b {
a
} else {
b
}
}
fn max_one_numbers2(arr: &mut Vec<i32>) -> i32 {
let mut ans = 0;
for num in arr.iter() {
ans += *num;
}
for i in 0..arr.len() as i32 {
arr[i as usize] = if arr[i as usize] == 0 {
1 } else {
-1 };
}
let mut max = -2147483648;
let mut cur = 0;
for i in 0..arr.len() as i32 {
cur += arr[i as usize];
max = get_max(max, cur);
cur = if cur < 0 {
0 } else {
cur };
}
return ans + max;
}
// In order to test
fn random_array(n: i32) -> Vec<i32> {
let mut arr: Vec<i32> = vec![];
for _i in 0..n {
arr.push(rand::thread_rng().gen_range(0, 2));
}
return arr;
}
The results are as follows :

边栏推荐
- Contest3145 - the 37th game of 2021 freshman individual training match_ E: Eat watermelon
- [Valentine's Day confession code] - Valentine's Day is approaching, and more than 10 romantic love effects are given to the one you love
- Nbear introduction and use diagram
- Calculate the odd sum of 1~n (1~100 as an example)
- Audio and video technology development weekly | 232
- 渗透实战-SQLServer提权
- 1289_FreeRTOS中vTaskSuspend()接口实现分析
- Which product is better if you want to go abroad to insure Xinguan?
- Es network layer
- Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
猜你喜欢

Monitoring - Prometheus introduction

SQL statement strengthening exercise (MySQL 8.0 as an example)

What is the difference between enterprise wechat applet and wechat applet

150 ppt! The most complete "fair perception machine learning and data mining" tutorial, Dr. AIST Toshihiro kamishima, Japan

Dare to climb here, you're not far from prison, reptile reverse actual combat case

Fudan released its first review paper on the construction and application of multimodal knowledge atlas, comprehensively describing the existing mmkg technology system and progress

1day vulnerability pushback skills practice (3)

Session learning diary 1

MySQL query

Jenkins configures IP address access
随机推荐
Value transfer communication between components (parent to child, child to parent, brother component to value)
[latex] production of complex tables: excel2latex and detail adjustment
Which product is better for 2022 annual gold insurance?
[.NET + mqtt]. Mise en œuvre de la communication mqtt dans l'environnement net 6 et démonstration de code pour l'abonnement et la publication de messages bilatéraux du serveur et du client
PMP 考試常見工具與技術點總結
Learning video website
logistic regression
Backpropagation formula derivation [Li Hongyi deep learning version]
Objective-C member variable permissions
Exercices de renforcement des déclarations SQL (MySQL 8.0 par exemple)
Setting methods, usage methods and common usage scenarios of environment variables in postman
Redis notes (I) Linux installation process of redis
The property of judging odd or even numbers about XOR.
PID of sunflower classic
Monitoring - Prometheus introduction
Contest3145 - the 37th game of 2021 freshman individual training match_ 1: Origami
PHP database connection succeeded, but data cannot be inserted
(column 23) typical C language problem: find the minimum common multiple and maximum common divisor of two numbers. (two solutions)
Sword finger offer:55 - I. depth of binary tree
MySQL one master multiple slaves + linear replication