当前位置:网站首页>(greedy) B. avoid local maximums
(greedy) B. avoid local maximums
2022-06-21 07:55:00 【Honestbutter-】
B. Avoid Local Maximums
lianjie
The question :
If I found a [ i ] > a [ i − 1 ] a[i]>a[i-1] a[i]>a[i−1]&& a [ i ] > a [ i + 1 ] a[i]>a[i+1] a[i]>a[i+1] We need to make some changes when , So that the entire sequence does not exist a [ i ] > a [ i − 1 ] a[i]>a[i-1] a[i]>a[i−1]&& a [ i ] > a [ i + 1 ] a[i]>a[i+1] a[i]>a[i+1], Find the minimum number of operations .
Ideas :
The problem of greed must be that the more impact the current operation has, the better
- The idea is to traverse from left to right , If I found a [ i ] > a [ i − 1 ] a[i]>a[i-1] a[i]>a[i−1]&& a [ i ] > a [ i + 1 ] a[i]>a[i+1] a[i]>a[i+1] When , modify a [ i ] = a [ i − 1 ] a[i]=a[i-1] a[i]=a[i−1], But this is obviously not optimal , Because if you modify it like this , The impact result is only : a [ i ] , a [ i − 1 ] , a [ i + 1 ] a[i],a[i-1],a[i+1] a[i],a[i−1],a[i+1] The three numbers are in harmony , But it has no effect on other numbers . So we don't consider modifying a [ i ] a[i] a[i].
- Do not modify a [ i − 1 ] a[i-1] a[i−1], Because it will affect the previous numbers
- At last we decided to revise a [ i + 1 ] a[i+1] a[i+1], because a [ i + 1 ] a[i+1] a[i+1] Would be right a [ i ] a[i] a[i] and a [ i + 2 ] a[i+2] a[i+2] All have an impact , If you just let a [ i + 1 ] = a [ i ] a[i+1]=a[i] a[i+1]=a[i], There may be a[i+2] Still more than a [ i + 1 ] and a [ i + 3 ] a[i+1] and a[i+3] a[i+1] and a[i+3] The big picture , So let a [ i + 1 ] = m a x ( a [ i ] , a [ i + 2 ] ) a[i+1]=max(a[i],a[i+2]) a[i+1]=max(a[i],a[i+2]) It can also affect a [ i + 2 ] a[i+2] a[i+2].
such as :
1 2 1 3 2
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 2e5+100;
int b[N],a[N];
int n;
int main()
{
int t;
cin>>t;
while(t--)
{
cin>>n;
for(int i=0;i<n;i++) cin>>a[i];
int cnt=0;
for(int i=1;i<n-1;i++)
{
if(a[i]>a[i-1]&&a[i]>a[i+1])
a[i+1]=max(a[i],a[i+2]),cnt++;
}
cout<<cnt<<endl;
for(int i=0;i<n;i++) cout<<a[i]<<" ";
cout<<endl;
}
return 0;
}
边栏推荐
- Three declaration methods of structure type
- mysql的安装路径如何查看
- Leetcode topic [array] -40- combined sum II
- [visualization - source code reading] antvis / g-base interpretation - 1
- 卧槽,一行代码就可将网页直接转pdf保存下来(pdfkit)
- [untitled]
- Market trend report, technical innovation and market forecast of inorganic water treatment chemicals in China
- 2021-06-16 STM32F103 EXTI 中断识别 使用固件库
- 2021-06-16 STM32F103 EXTI 中斷識別 使用固件庫
- mysql不是内部命令如何解决
猜你喜欢

22 parameter estimation - maximum likelihood estimation method

WordPress website security in 2022

Practical application cases of digital Twins - coal mine

2021-06-16 STM32F103 exti interrupt identification using firmware library

动态规划解决打家劫舍问题

為什呢代碼沒報錯但是數據庫裏邊的數據顯示不出來

How MySQL closes a transaction

mysql的安装路径如何查看
![[untitled]](/img/25/4725977134d41b5d6f5d86b60ec731.jpg)
[untitled]

Flutter returns to the black screen of the previous page
随机推荐
Leetcode topic [array] -40- combined sum II
线上GO服务出现GC故障,我当时就急了
图解 Google V8 # 14:字节码(二):解释器是如何解释执行字节码的?
A table to easily understand the prefix and suffix of increment and decrement operators
(for line breaks) differences between gets and fgets, and between puts and fputs
Research Report on market supply and demand and strategy of oil-free scroll compressor industry in China
Flutter returns to the black screen of the previous page
1004 counting leaves (30 points)
JVM memory model concepts
unity里现实摄像头运镜并LookAt到物体前方 基于Dotween
2021-07-28 STM32F103 configuration information
华三IPsec
2021-06-17 STM32F103 USART串口代码 使用固件库
2021-06-16 STM32F103 EXTI 中斷識別 使用固件庫
One year experience interview byte Tiktok e-commerce, share the following experience!
Complex four operations (23 lines of concise code)
mysql存储过程中的循环语句怎么写
群晖DSM7添加套件源
1006 Sign In and Sign Out (25 分)
Mingming has just changed his profession and won five offers as soon as he graduated