当前位置: 首页 > news >正文

贪心算法(常见贪心模型)

常见贪心模型

 简单排序模型

最小化战斗力差距

题目分析:

 

#include <bits/stdc++.h>
using namespace std;const int N = 1e5 + 10;int n;
int a[N];int main()
{// 请在此输入您的代码cin >> n;for (int i = 1;i <= n;i++) cin >> a[i];sort(a+1,a+1+n);int disparity = 1e9-1;for (int i = 1;i < n;i++) disparity = min(disparity,a[i+1] - a[i]);cout << disparity << '\n';return 0;
}

 货仓选址 

可以发现,仓库建在中间,可以使得货仓到每家商店的距离之和最小

#include <bits/stdc++.h>
using namespace std;const int N = 100010;int n;
int a[N];int main()
{cin >> n;for (int i = 1;i <= n;i++) cin >> a[i];sort(a+1,a+1+n);int store = a[n/2 + 1];int res = 0;for (int i = 1;i <= n;i++) {if (i == n/2 + 1) continue;res += abs(store - a[i]);}cout << res << '\n';return 0;
}

总操作一定的情况下的最小代价模型

谈判

如果每次选择代价最小的两个部落合并,不仅可以使得当前代价最小,还可以使得后续合并的代价也尽可能小

局部最优 ==》全局最优

 用数组模拟(O(n^2 log n))

#include <bits/stdc++.h>
using namespace std;const int N = 1010;int n;
int a[N];int main()
{// 先排序 找到两个人数最少的部落cin >> n;for (int i = 1;i <= n;i++) cin >> a[i];sort(a+1,a+1+n);int sum = 0;for (int i = 2;i <= n;i++) {a[i] = a[i] + a[i-1];sum += a[i];sort(a+i,a+1+n);}cout << sum << '\n';return 0;
}

 用优先级队列实现

#include <bits/stdc++.h>
using namespace std;
using ll = long long;priority_queue<ll,vector<ll>,greater<ll>> pq;int main()
{//用优先队列实现 每次取其中两个代价最小的部落进行合并,总花费最小int n;cin >> n;for (int i = 1;i <= n;i++){ll x;cin >> x;pq.push(x);}  ll ans = 0;while (pq.size() > 1){ll x = pq.top();pq.pop();ll y = pq.top();pq.pop();ans += x+y;pq.push(x+y);}cout << ans << '\n';return 0;
}
 糖果传递

#include <bits/stdc++.h>
using namespace std;
using ll = long long;const int N = 1000010;int n;
int a[N];
ll c[N];int main()
{cin >> n;ll sum = 0;for (int i = 1;i <= n;i++) cin >> a[i],sum += a[i];int avg = sum / n;for (int i = n;i > 0;i--) c[i] = c[i+1] + avg - a[i];c[1] = 0;sort(c+1,c+1+n);ll res = 0;for (int i = 1;i <= n;i++) res += abs(c[i] - c[(n+1)/2]);cout << res << '\n';return 0;
}

最少数量的贪心模型

纪念品分组

 

 

 

为了让组数最少,我们要尽可能将两个纪念品打包成一组

怎样的两个纪念品最有可能打包成一组呢,无疑是一个大和一个小的

所以我们先去找最大和最小的,如果不能打包,则去找次小的,直到能打包成一组

不能打包的大的纪念品则单独一组

#include <bits/stdc++.h>
using namespace std;const int N = 30010;int w,n;
int a[N];int main()
{// 请在此输入您的代码cin >> w >> n;int cnt = 0;for (int i = 1;i <= n;i++) cin >> a[i];sort(a+1,a+1+n);for (int i = 1,j = n;;){if (i > j) break;if (a[i] + a[j] <= w){cnt++;i++,j--;}else {j--;cnt++;}}cout << cnt << '\n';return 0;
}

找规律的贪心模型

分糖果

我们要去使得所有糖果组成的字符串中字典序最大的字符串尽可能小

字符串字典集大小先看首字母大小,其次看后面的字母大小

a < b < c ...

a < aabs...

先给字符串排序,然后分为三类讨论

1️⃣字符串全相等(假设全a),那就尽量使得每个人分到的字符串的最大长度尽可能小,即平分字符串

2️⃣s[x] == s[1] 说明第x个是最小的字符,让它带着后面所有的字符一起输出即可

3️⃣ s[x] != s[1] ,后面一坨字符直接丢到s[1]后面,分给 第一个同学即可

#include <bits/stdc++.h>
using namespace std;const int N = 1e6 + 10;int n,x;
char s[N];int main()
{// 请在此输入您的代码cin >> n >> x;cin >> s+1;sort(s+1,s+1+n);if (s[1] == s[n]) {for (int i = 1;i <= n / x + (n % x ? 1 : 0);i++) cout << s[i];}else if (s[1] == s[x]) {for (int i = x;i <= n;i++) cout << s[i];}else {cout << s[x];}return 0;
}
股票买卖II

容易发现规律,如果后一天的股票比当天高,那么就今天买入后一天卖出

#include <bits/stdc++.h>
using namespace std;const int N = 1e5 + 10;int n;
int a[N];int main() {cin >> n;for (int i = 1;i <= n;i++) cin >> a[i];int res = 0;for (int i = 1;i <= n;i++){if (a[i] < a[i+1]) res += a[i+1] - a[i];}cout << res << '\n';return 0;
}
乘积最大

 

#include <bits/stdc++.h>
using namespace std;
using ll = long long;const int N = 1e5 + 10,mod = 1000000009;int n,k;
int a[N];int main()
{cin >> n >> k;for (int i = 1;i <= n;i++) cin >> a[i];sort(a+1,a+1+n);int res = 1;int l = 1,r = n;int sign = 1;if (k % 2) {res = a[r--];k--;if (res < 0) sign = -1;}while (k){ll x = (ll)a[l] * a[l+1],y = (ll)a[r] * a[r-1];if (x * sign > y * sign){res = x % mod * res % mod;l += 2;}else {res = y % mod * res % mod;r -= 2;}k -= 2;}cout << res << endl;return 0;
}

http://www.lryc.cn/news/510750.html

相关文章:

  • git自动压缩提交的脚本
  • Kinova在开源家庭服务机器人TidyBot++研究里大展身手
  • 使用 Spring Boot 实现文件上传:从配置文件中动态读取上传路径
  • 《鸿蒙HarmonyOS应用开发从入门到精通(第2版)》学习笔记——HarmonyOS技术理念
  • 将多个 k8s yaml 配置文件合并为一个文件
  • Linux 文件的特殊权限—Sticky Bit(SBIT)权限
  • MIPI D-PHY/C-PHY/M-PHY 高速串行接口标准
  • USB免驱IC读写器QT小程序开发
  • OSCP靶场训练冒险之kioprix4:shell逃逸以及利用数据库提权
  • NIPS2014 | GAN: 生成对抗网络
  • Postman接口测试01|接口测试基础概念、http协议、RESTful风格、接口文档
  • Linux系统编程——详解页表
  • SpringBoot + HttpSession 自定义生成sessionId
  • 循环对称复高斯分布(Circularly Symmetric Complex Gaussian Distribution)
  • xinput1_3.dll放在哪里?当xinput1_3.dll丢失时的应对策略:详细解决方法汇总
  • 基于STM32的智能家居环境监控系统设计
  • Vscode + gdbserver远程调试开发板指南:
  • 大表:适用于结构化数据的分布式存储系统
  • 深入解析MVCC中Undo Log版本底层存储读取逻辑
  • 游戏引擎学习第64天
  • Effective C++ 条款33:避免遮掩继承而来的名称
  • UEFI Spec 学习笔记---4 - EFI System Table(1)
  • 【微信小程序】3|首页搜索框 | 我的咖啡店-综合实训
  • 独一无二,万字详谈——Linux之文件管理
  • React:前端开发领域的璀璨之星
  • C/C++ 数据结构与算法【哈夫曼树】 哈夫曼树详细解析【日常学习,考研必备】带图+详细代码
  • 基于NodeMCU的物联网窗帘控制系统设计
  • 喜报 | 擎创科技入围上海市优秀信创解决方案
  • windows10下使用沙盒多开uiautoanimation可行性验证
  • 电脑报错wsdprintproxy.dll丢失?修复wsdprintproxy.dll文件缺失的实用方法