传送门

思路:对于一般情况,我们有三个袋子,容易想到把袋子里物品的价值排序。然后贪心,我们想让最后的价值最大,则三个袋子最后都可以剩余一个物品,这三个物品总和需要最大,最好的情况就是三个物品的符号“+”,“-”,“-”,这样总价值直接可以算是每个袋子中物品绝对值的累加和。为了让三个物品价值最大,我们可以容易想到,价值大的物品减去价值小的物品,让可用价值尽可能大,而且最后剩余每个袋子的最后物品符号分别是“+”“-”“-”。这样,我们之前每个袋子的物品都排好了序,容易想到,对于三个袋子中,每个袋子价值最小的物品是关键。因为我们需要让可用价值尽可能大,所以我们可以让三个袋子中,最小值最大的那个物品为“+”,然后让其他两个袋子中的最小物品收集其他价值,这样就满足了三个袋子都只剩下一个物品且满足“+”“-”“-”。我们可以让最小值最大和最小值中间大的袋子中其他物品累加减去最小值最小的物品让其为“-”,然后让最小值最小的其他物品减去最小值中间大的那个物品让其为“-”就可以了。但最小值最小的其他所有物品和最小值中间大的物品的差值会有特殊情况,例如:最小值的其他物品 1,1;中间值的最小值 4.这样(4-1-1 = 2),这里需要特判一下,我们发现两者的差值只需要取一个abs就可以了,上面的例子可以转化为:1 - 中间值放入最小值的袋中,变成了abs(-3+1)。

然后就是特殊情况,即三个袋子中任意一个袋子的物品只有一个的情况,需要特殊判断。

 1 #include <iostream>
2 #include <cstdio>
3 #include <algorithm>
4 #include <cstring>
5 #include <cmath>
6 #include <queue>
7 #include <vector>
8 #include <cstring>
9 #include <functional>
10 #include <map>
11 #define LL long long
12 #define lson(rt) (rt << 1)
13 #define rson(rt) (rt << 1 | 1)
14 using namespace std;
15
16 const int N = 1e3 + 10;
17 struct node
18 {
19 int v, id;
20
21 bool friend operator<(const node& a, const node& b)
22 {
23 return a.v < b.v;
24 }
25 };
26 int a[N];
27
28 void solve ()
29 {
30 int n, m, k, x;
31 scanf("%d%d%d", &n, &m, &k);
32 vector<int > a[3];
33 for(int i = 0; i < n; ++i) {
34
35 scanf("%d", &x);
36 a[0].push_back(x);
37 }
38 for(int i = 0; i < m; ++i) {
39 scanf("%d", &x);
40 a[1].push_back(x);
41 }
42 for(int i = 0; i < k; ++i) {
43 scanf("%d", &x);
44 a[2].push_back(x);
45 }
46 for(int i = 0; i < 3; ++i) sort(a[i].begin(), a[i].end());
47
48 vector<node > vn;
49 vn.push_back({a[0][0], 0});
50 vn.push_back({a[1][0], 1});
51 vn.push_back({a[2][0], 2});
52
53 sort(vn.begin(), vn.end());
54
55
56 int maxx = vn[2].id;
57 int midd = vn[1].id;
58 int minn = vn[0].id;
59
60 long long maxv, midv, minv;
61 maxv = midv = minv = 0;
62 for(auto x : a[maxx]) maxv += x;
63 for(auto x : a[midd]) midv += x;
64 for(auto x : a[minn]) minv += x;
65 // printf("(%lld %lld %lld)\n", maxv, midv,minv);
66 midv -= a[midd][0];
67 minv -= a[minn][0];
68 maxv -= a[maxx][0];
69 // printf("(%lld %lld %lld)\n", maxv, midv,minv);
70 // printf("(%d %d %d)\n", a[maxx].size(), a[midd].size(), a[minn].size());
71 long long ans = 0;
72 if(a[minn].size() == 1) {
73 ans += maxv + midv + a[maxx][0] + a[midd][0] - a[minn][0];
74 } else if(a[midd].size() == 1) {
75 // cout << "s" << endl;
76 ans += abs(minv + a[minn][0] - a[midd][0]);
77 ans += a[maxx][0] + maxv;
78 } else if(a[maxx].size() == 1 && a[maxx][0] < a[midd][0] + a[minn][0]) {
79 ans += midv + minv + a[midd][0] + a[minn][0] - a[maxx][0];
80 } else {
81 // cout << "s" << endl;
82 ans += a[maxx][0] + maxv + midv - a[minn][0];
83 ans += abs(minv - a[midd][0]);
84 }
85
86 printf("%lld\n", ans);
87 }
88
89 int main ()
90 {
91
92 solve();
93
94 return 0;
95 }

C. Three Bags【CF 1467】的更多相关文章

  1. 【CF#338D】GCD Table

    [题目描述] 有一张N,M<=10^12的表格,i行j列的元素是gcd(i,j) 读入一个长度不超过10^4,元素不超过10^12的序列a[1..k],问是否在某一行中出现过 [题解] 要保证g ...

  2. 【CF#303D】Rotatable Number

    [题目描述] Bike是一位机智的少年,非常喜欢数学.他受到142857的启发,发明了一种叫做“循环数”的数. 如你所见,142857是一个神奇的数字,因为它的所有循环排列能由它乘以1,2,...,6 ...

  3. 【CF 463F】Escape Through Leaf

    题意 给你一棵 \(n\) 个点的树,每个节点有两个权值 \(a_i,b_i\). 从一个点 \(u\) 可以跳到以其为根的子树内的任意一点 \(v\)(不能跳到 \(u\) 自己),代价是 \(a_ ...

  4. 【CF 453A】 A. Little Pony and Expected Maximum(期望、快速幂)

    A. Little Pony and Expected Maximum time limit per test 1 second memory limit per test 256 megabytes ...

  5. 【CF 585E】 E. Present for Vitalik the Philatelist

    E. Present for Vitalik the Philatelist time limit per test 5 seconds memory limit per test 256 megab ...

  6. 【35.20%】【CF 706D】Vasiliy's Multiset

    time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standa ...

  7. 【26.8%】【CF 46D】Parking Lot

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  8. 【31.42%】【CF 714A】Meeting of Old Friends

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  9. 【31.95%】【CF 714B】Filya and Homework

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

随机推荐

  1. Python 常用方法和模块的使用(time & datetime & os &random &sys &shutil)-(六)

    1 比较常用的一些方法 1.eval()方法:执行字符串表达式,并返回到字符串. 2.序列化:变量从内存中变成可存储或传输到文件或变量的过程,可以保存当时对象的状态,实现其生命周期的延长,并且需要时可 ...

  2. Linux文件权限问题

    更改权限命令   chmod 常用参数 -R, --recursive change files and directories recursively (以递归的方式对目前目录下的所有档案与子目录进 ...

  3. 搭建yum仓库服务器

    环境:服务端centos6.9 客户端要求 能上网(可以ping通baidu.com) 1.yum的配置文件信息在/etc/yum.repos.d/下,我们配置的是自己的网络yum源,所以这些文件我们 ...

  4. Sense Sense (Beta)安装及解决部分Chrome插件安装时程序包无效:"CRX_HEADER_INVALID"

    下载地址  http://down.cnplugins.com/down/down.aspx?fn=1412/www.cnplugins.com_lhjgkmllcaadmopgmanpapmpjgm ...

  5. Linux下weblogic启动慢

    修改Linux上Weblogic使用的jdk $JAVA_HOME/jre/lib/security/java.security (/bea/weblogic/jdk150_12/jre/lib/se ...

  6. Asp.net core验证类ModelStateDictionary的bug

    在使用.net core 3.1 时发现明明没有验证请求类属性,甚至已经加了默认值 但是验证类时依然会报错 经过网上百度等搜索,尝试使用可空类型赋值默认值 果然验证类没有报错 不清楚是微软的bug还是 ...

  7. Flink读写Redis(三)-读取redis数据

    自定义flink的RedisSource,实现从redis中读取数据,这里借鉴了flink-connector-redis_2.11的实现逻辑,实现对redis读取的逻辑封装,flink-connec ...

  8. Shiro实现Basic认证

    前言 今天跟小伙伴们分享一个实战内容,使用Spring Boot+Shiro实现一个简单的Http认证. 场景是这样的,我们平时的工作中可能会对外提供一些接口,如果这些接口不做一些安全认证,什么人都可 ...

  9. 面试 23-面试技巧 by smyhvae

    23-面试技巧 by smyhvae #写简历的注意事项 最多可以写"深入了解",但不要写"精通". #遇到不知道的问题,该怎么回答 这块儿我没了解过,准备回去 ...

  10. 一个 Spark 应用程序的完整执行流程

    一个 Spark 应用程序的完整执行流程 1.编写 Spark Application 应用程序 2.打 jar 包,通过 spark-submit 提交执行 3.SparkSubmit 提交执行 4 ...