当前位置:网站首页>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] .
边栏推荐
- 1038 Recover the Smallest Number
- leetcode-871:最低加油次数
- 瑞萨RZ/G2L ARM开发板存储读写速度与网络实测
- 用Go+绘制爱心给心爱的她表白
- leetcode:701. Insertion in binary search tree [BST insertion]
- Embrace the safety concept of platform delivery
- Rk3568 development board evaluation (II): development environment construction
- Telephone network problems
- Excel if formula determines whether the two columns are the same
- mysql 多表联合删除
猜你喜欢
![[shutter] image component (configure local GIF image resources | load placeholder with local resources)](/img/73/19e2e0fc5ea6f05e34584ba40a452d.jpg)
[shutter] image component (configure local GIF image resources | load placeholder with local resources)

Linear programming of mathematical modeling (including Matlab code)

数据分析思维分析犯法和业务知识——分析方法(一)

matlab将数字矩阵保存为地理空间数据出错,显示下标索引必须为正整数类型或逻辑类型,解决

What is needed to develop a domestic arm intelligent edge computing gateway

RK3568开发板评测篇(二):开发环境搭建

Infrared thermography temperature detection system based on arm rk3568

MySQL基础用法02

Draw love with go+ to express love to her beloved
![[flutter] icons component (load the built-in icon of flutter | display the material design icon completely)](/img/f5/3ec22f1480227f33a1c8ac457155ed.jpg)
[flutter] icons component (load the built-in icon of flutter | display the material design icon completely)
随机推荐
On Fibonacci sequence
[shutter] animation animation (shutter animation type | the core class of shutter animation)
Initial order of pointer (basic)
465. 最优账单平衡 DFS 回溯
Reading and writing speed of Reza rz/g2l arm development board storage and network measurement
The difference between tail -f, tail -f and tail
递归处理组织的几种情况
The difference between relational database and non relational database
Meibeer company is called "Manhattan Project", and its product name is related to the atomic bomb, which has caused dissatisfaction among Japanese netizens
[AUTOSAR eight OS]
MySQL multi table joint deletion
ROS2之ESP32简单速度消息测试(极限频率)
指针进阶(一)
1696C. Fishingprince plays with array [thinking questions + intermediate state + optimized storage]
[applet project development -- JD mall] user defined search component of uni app (middle) -- search suggestions
链表内指定区间反转
研发一款国产ARM智能边缘计算网关需要什么
Array and collection performance comparison
12_微信小程序之微信视频号滚动自动播放视频效果实现
Several cases of recursive processing organization