当前位置:网站首页>Daily topic: movement of haystack
Daily topic: movement of haystack
2022-07-03 01:10:00 【The code family】
Classic greedy analysis :
Let's start with the topic :

At first glance, the topic is more complex , It's hard to think about , But in fact, draw pictures to find rules , In fact, it is easy to simulate .
First of all, the key is to grasp a key point :( Every haystack should become the same value ..).
So what to consider is what the same value should be , In fact, the average value is found after several simulations :: Simply draw a picture :

It can be divided into high licorice piles and low haystacks ... Greedy analysis can be used :::
If there is a haystack too high , Then he can give hay to those shorter haystacks , How much is the gift , Make your haystack the same average . Which shorter haystack will you give it to , We don't care , Give it away , Send air . Anyway, I gave it away , These haystacks are short hay anyway , It won't be lost .
You may ask : Can you guarantee that all the height is equal if you give it away like this ? The answer is yes , After a relatively high haystack is presented , The height will become an average . After all the high haystacks are presented , All heights must be equal
So things become simple ,, Just go directly to the code
// Greedy first analysis
#include<iostream>
#include<algorithm>
const int N = 1e5 +10;
int a[N];
using namespace std;
int main()
{
int n ;
cin>>n;
int sum = 0 , cnt = 0;
for(int i=0; i<n; i++)
{
cin>>a[i];
sum += a[i];
}
int hh = sum / n;
for(int i=0; i<n; i++)
{
if(a[i] < hh ) cnt += hh - a[i];
}
cout << cnt<< endl;
return 0;
}Some knowledge about greed
Baidu Encyclopedia explains :: Greedy algorithm is a simpler solution to some problems 、 Faster design technology . Greedy algorithm is characterized by step by step , It is often based on the current situation and makes the optimal choice according to an optimization measure , Regardless of the possible overall situation , It saves a lot of time that must be spent trying everything possible to find the optimal solution . The greedy algorithm adopts top-down , Make successive greedy choices in an iterative way , Every greedy choice , The problem is reduced to a smaller subproblem , Through every step of greedy choice , An optimal solution of the problem can be obtained . Although it is necessary to ensure that the local optimal solution can be obtained at each step , However, the resulting global solution is sometimes not necessarily optimal , So greedy algorithms don't go back ..
Greedy algorithm does not consider the overall optimization , What is made is only a local optimal choice in a sense . When using greedy strategy, we should pay attention to the relationship between local optimization and global optimization , Choosing the current local optimum does not necessarily deduce the global optimum of the problem . Greedy strategy needs to solve the following two problems : [5]
1、 Is this problem suitable to be solved by greedy strategy , That is, whether the problem has the nature of greedy choice [5] ;
2、 Develop greedy strategies , To achieve the optimal solution or better solution of the problem [5] .
To determine whether a problem is suitable for solving with greedy algorithm , It must be proved that the greedy choice made at each step leads to the global optimal solution of the problem . The approximate process of proof is : Firstly, a global optimal solution of the problem is investigated , It is proved that the optimal solution can be modified , Make it start with greedy choices , After making greedy choices , The original problem is simplified to a similar subproblem with smaller scale . And then use Mathematical induction Prove that you make greedy choices through every step , Finally, the overall optimal solution of the problem can be obtained [5] .
边栏推荐
- 测试右移:线上质量监控 ELK 实战
- How to convert Quanzhi a40i/t3 to can through SPI
- 465. 最优账单平衡 DFS 回溯
- 1696C. Fishingprince Plays With Array【思维题 + 中间状态 + 优化存储】
- matlab查找某一行或者某一列在矩阵中的位置
- 465. DFS backtracking of optimal bill balance
- [flutter] icons component (fluttericon Download Icon | customize SVG icon to generate TTF font file | use the downloaded TTF icon file)
- Usage of using clause in kingbases alter table
- MySQL multi table joint deletion
- [case sharing] let the development of education in the new era advance with "number"
猜你喜欢

有向图的强连通分量

Leetcode-849: maximum distance to the nearest person

Embrace the safety concept of platform delivery

tail -f 、tail -F、tailf的区别

数据分析思维分析犯法和业务知识——分析方法(一)
![[flutter] icons component (fluttericon Download Icon | customize SVG icon to generate TTF font file | use the downloaded TTF icon file)](/img/ca/1d2473ae51c59b84864352eb17de94.jpg)
[flutter] icons component (fluttericon Download Icon | customize SVG icon to generate TTF font file | use the downloaded TTF icon file)

leetcode-849:到最近的人的最大距离

指针进阶(一)

Deep analysis of data storage in memory
![[AUTOSAR + IO Architecture]](/img/cf/9ea42b50bed298c0546764b63bd957.png)
[AUTOSAR + IO Architecture]
随机推荐
MySQL
First hand evaluation of Reza electronics rz/g2l development board
[introduction to AUTOSAR seven tool chain]
基本远程连接工具Xshell
测试右移:线上质量监控 ELK 实战
Leetcode-849: maximum distance to the nearest person
异步、郵件、定時三大任務
Data analysis, thinking, law breaking and professional knowledge -- analysis method (I)
12_微信小程序之微信视频号滚动自动播放视频效果实现
RK3568开发板评测篇(二):开发环境搭建
leetcode-2115:从给定原材料中找到所有可以做出的菜
Matlab finds the position of a row or column in the matrix
【FPGA教程案例5】基于vivado核的ROM设计与实现
[AUTOSAR eight OS]
Several cases of recursive processing organization
[AUTOSAR 11 communication related mechanism]
leetcode-934:最短的桥
Linear programming of mathematical modeling (including Matlab code)
链表中的节点每k个一组翻转
1038 Recover the Smallest Number