博客
关于我
AtCoder Beginner Contest 100 D - Patisserie ABC[思维]
阅读量:535 次
发布时间:2019-03-08

本文共 1155 字,大约阅读时间需要 3 分钟。

题意:n个物品,每个物品有三个属性a, b, c(可能为正,可能为负)。现在取m个,使得|Σa| + |Σb| + |Σc|最大化。

思路:绝对值中的符号可以趋向极端值(正或负)。因此,每个绝对值符号有两种可能情况,总共有8种符号组合。通过对物品排序的方法,选择最优的m个物品,计算对应的绝对值和,找出最大值。

代码实现

#include 
using namespace std;typedef long long ll;const int N = 1e5 + 5;ll a[N], sum[N];struct Node { ll a, b, c;};int n, m;Node p[N];bool cmp(Node x, Node y) { return x.a * i + x.b * j + x.c * k > y.a * i + y.b * j + y.c * k;}int main() { ios::sync_with_stdio(false); cin.tie(0); for (int i = 1; i <= n; ++i) { cin >> p[i].a >> p[i].b >> p[i].c; } ll ans = 0; for (int i = -1; i <= 1; i += 2) { for (int j = -1; j <= 1; j += 2) { for (int k = -1; k <= 1; k += 2) { sort(p + 1, p + n, cmp); ll t[4] = {0}; for (int te = 1; te <= m; ++te) { t[1] += p[te].a; t[2] += p[te].b; t[3] += p[te].c; } ans = max(ans, t[1]*i + t[2]*j + t[3]*k); } } } cout << ans << endl; return 0;}

说明:以上代码实现了通过对物品属性的不同符号组合进行排序,选择最优m个物品,使得三个绝对值和的最大值得到最大化。通过对每个绝对值符号的方向进行分析(8种组合),确保找到最优解。

转载地址:http://xykiz.baihongyu.com/

你可能感兴趣的文章
poj 1258 Agri-Net
查看>>
quagga 和 zebos
查看>>
poj 1286 Necklace of Beads
查看>>
POJ 1321 棋盘问题
查看>>
poj 1321(回溯)
查看>>
Qt高级——Qt元对象系统源码解析
查看>>
qt调用vs2008编写的dll动态库(隐式调用)
查看>>
Qt读取注册表默认值
查看>>
poj 1679 判断MST是不是唯一的 (次小生成树)
查看>>
POJ 1703 Find them, Catch them
查看>>
POJ 1703 Find them, Catch them 并查集
查看>>
POJ 1738 An old Stone Game(石子合并)
查看>>
POJ 1740 A New Stone Game(博弈)题解
查看>>
Qt网络编程之实例二POST方式
查看>>
POJ 1765 November Rain
查看>>
poj 1860 Currency Exchange
查看>>
POJ 1961 Period
查看>>
POJ 2019 Cornfields (二维RMQ)
查看>>
poj 2057 The Lost House 贪心思想在动态规划上的应用
查看>>
poj 2057 树形DP,数学期望
查看>>