当前位置:网站首页>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] .
边栏推荐
- 【FPGA教程案例6】基于vivado核的双口RAM设计与实现
- 12_ Implementation of rolling automatic video playback effect of wechat video number of wechat applet
- Meibeer company is called "Manhattan Project", and its product name is related to the atomic bomb, which has caused dissatisfaction among Japanese netizens
- Merge K sorted linked lists
- [AUTOSAR + IO Architecture]
- 【爱死机】《吉巴罗》被忽略的细节
- Leetcode-2115: find all the dishes that can be made from the given raw materials
- 飞凌搭载TI AM62x的ARM核心板/开发板首发上市,亮相Embedded World 2022
- 瑞萨RZ/G2L 处理器简介|框架图|功耗|原理图及硬件设计指南
- leetcode-2280:表示一个折线图的最少线段数
猜你喜欢
![[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)

安全运营四要素之资产、脆弱性、威胁和事件

JS inheritance and prototype chain
![[case sharing] let the development of education in the new era advance with](/img/11/af88d16dc66f00840cbfc5ba5d68bd.jpg)
[case sharing] let the development of education in the new era advance with "number"
[AUTOSAR five methodology]

Asynchronous, email and scheduled tasks

Excel if formula determines whether the two columns are the same

Linear programming of mathematical modeling (including Matlab code)

How to convert Quanzhi a40i/t3 to can through SPI
![[AUTOSAR eight OS]](/img/ac/fbc84c077ff9c94c840e1871171d19.png)
[AUTOSAR eight OS]
随机推荐
基本远程连接工具Xshell
全志A40i/T3如何通过SPI转CAN
[shutter] image component (cached_network_image network image caching plug-in)
Leetcode-2280: represents the minimum number of line segments of a line graph
Draw love with go+ to express love to her beloved
[C language] branch and loop statements (Part 1)
matlab查找某一行或者某一列在矩阵中的位置
Solve the cache problem of reactnative using WebView
Linear programming of mathematical modeling (including Matlab code)
用Go+绘制爱心给心爱的她表白
12_微信小程序之微信视频号滚动自动播放视频效果实现
Test shift right: Elk practice of online quality monitoring
Excel removes the data after the decimal point and rounds the number
按键精灵打怪学习-前台和内网发送后台验证码
FPGA - 7 Series FPGA internal structure clocking -04- multi area clock
Strongly connected components of digraph
[overview of AUTOSAR four BSW]
数据分析思维分析犯法和业务知识——分析方法(一)
The R language uses the ctree function in the party package to build conditional inference decision trees, uses the plot function to visualize the trained conditional inference decision tree, and the
excel IF公式判断两列是否相同