当前位置:网站首页>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] .
边栏推荐
- 链表内指定区间反转
- [shutter] animation animation (shutter animation type | the core class of shutter animation)
- The arm core board / development board of Feiling equipped with Ti am62x made its debut in embedded world 2022
- Specified interval inversion in the linked list
- leetcode-2115:从给定原材料中找到所有可以做出的菜
- MySQL
- [AUTOSAR VI description document]
- leetcode-2280:表示一个折线图的最少线段数
- leetcode-224:基本计算器
- 递归处理组织的几种情况
猜你喜欢

Foundations of data science is free to download

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

(C language) data storage

攻克哈希的基本概念与实现
![[AUTOSAR XIII NVM]](/img/38/805ab70f199e2cfad4d9dae0e2c1ff.png)
[AUTOSAR XIII NVM]

Infrared thermography temperature detection system based on arm rk3568

excel IF公式判断两列是否相同

Key detection and sinusoidal signal output developed by Arduino

1696C. Fishingprince Plays With Array【思维题 + 中间状态 + 优化存储】

Merge K sorted linked lists
随机推荐
[shutter] image component (cached_network_image network image caching plug-in)
Excel removes the data after the decimal point and rounds the number
[flutter] icons component (load the built-in icon of flutter | display the material design icon completely)
机器学习术语
[AUTOSAR II appl overview]
12_ Implementation of rolling automatic video playback effect of wechat video number of wechat applet
leetcode:871. 最低加油次数【以前pat做过 + 最大堆 +贪心】
删除有序链表中重复的元素-II
R language ggplot2 visualization: use ggplot2 to display dataframe data that are all classified variables in the form of thermal diagram, and customize the legend color legend of factor
Meibeer company is called "Manhattan Project", and its product name is related to the atomic bomb, which has caused dissatisfaction among Japanese netizens
Assets, vulnerabilities, threats and events of the four elements of safe operation
数据分析思维分析犯法和业务知识——分析方法(一)
解决ReactNative使用webView存在缓存问题
[AUTOSAR VI description document]
Excel if formula determines whether the two columns are the same
按键精灵打怪学习-多线程后台坐标识别
Esp32 simple speed message test of ros2 (limit frequency)
1038 Recover the Smallest Number
[overview of AUTOSAR three RTE]
RISA rz/g2l processor introduction | frame diagram | power consumption | schematic diagram and hardware design guide