UVaLive 6694 Toy Boxes (二分+想法)
题意:给出n个数,把n个数放在三个盒子里,每个盒子里的数绑在一起,要拿出来任何一个数的时候,所承担的重量是整个盒子的总重量,求最小总重量和。
析:感觉吧,就是轻的放的多一些,拿的次数多一些,大的放的少一些,拿的少一些。分成两堆时一定是连续小的物品在一堆,连续大的在一堆,
也就是其中一堆的任意一个物品重量都要小于另一堆。,并且小的那一堆的个数一定要大于大的那一堆的个数。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <functional>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <deque>
#include <map>
#include <cctype>
#include <stack>
#include <sstream>
#include <cstdlib>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 2e4 + 5;
const int mod = 1e9 + 7;
const char *mark = "+-*";
const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int dc[] = {0, 1, 0, -1, -1, 1, 1, -1};
int n, m;
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
int a[maxn];
LL sum[maxn];
bool cmp(const int &lhs, const int &rhs){
return lhs > rhs;
} int main(){
int T; cin >> T;
while(T--){
scanf("%d", &n);
for(int i = 1; i <= n; ++i) scanf("%d", &a[i]);
sort(a+1, a+n+1, cmp);
sum[1] = a[1];
for(int i = 2; i <= n; ++i) sum[i] = a[i] + sum[i-1];
LL ans = LNF;
for(int i = 1; i <= n/3; ++i){
for(int j = i+1; j <= n*2/3; ++j){
int k = n - j;
ans = Min(ans, sum[i]*(i) + (sum[j]-sum[i])*(j-i) + (sum[n]-sum[j]) * k);
}
} printf("%lld\n", ans);
}
return 0;
}
UVaLive 6694 Toy Boxes (二分+想法)的更多相关文章
- UVALive - 3211 (2-SAT + 二分)
layout: post title: 训练指南 UVALive - 3211 (2-SAT + 二分) author: "luowentaoaa" catalog: true m ...
- Codeforces 551C GukiZ hates Boxes(二分)
Problem C. GukiZ hates Boxes Solution: 假设最后一个非零的位置为K,所有位置上的和为S 那么答案的范围在[K+1,K+S]. 二分这个答案ans,然后对每个人尽量 ...
- Codeforces Round #307 (Div. 2) C. GukiZ hates Boxes 二分
C. GukiZ hates Boxes time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- hdu 4190 Distributing Ballot Boxes 二分
Distributing Ballot Boxes Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- UVALive 3971 Assemble(模拟 + 二分)
UVALive 3971 题意:有b块钱.想要组装一台电脑,给出n个配件的种类,名字,价格,品质因子.若各种类配件各买一个,总价格<=b,求最差品质配件的最大品质因子. 思路: 求最大的最小值一 ...
- Codeforces Round #238 (Div. 2) D. Toy Sum(想法题)
传送门 Description Little Chris is very keen on his toy blocks. His teacher, however, wants Chris to s ...
- UVALive 5000 Underwater Snipers --二分
题意:一条河岸线y=k,y>k区域有n个敌人,现在要在y<=k区域布置S个狙击手,狙击手的狙击范围为距离自己半径为D的圆内,问满足能够狙死所有的敌人的情况下,离河岸线最近的那个狙击手的离河 ...
- UVALive 4949 Risk(二分网络流、SAP)
n个区域,每个区域有我方军队a[i],a[i]==0的区域表示敌方区域,输入邻接矩阵.问经过一次调兵,使得我方边界处(与敌军区域邻接的区域)士兵的最小值最大.输出该最大值.调兵从i->j仅当a[ ...
- UVALive 6168 Fat Ninjas --二分小数+搜索
题意:一个NxN的网格地板,有一些激光束从天花板垂直射向地面的某个网格,一个圆要安全地从左走到右,不碰到上边界,下边界以及激光束,问这个圆的直径最大能达到多大. 分析:可以二分直径,关键在check函 ...
随机推荐
- JS Array常用方法indexOf/filter/forEach/map/reduce详解
Array共有九个方法 Array.prototype.indexOf Array.prototype.lastIndexOf Array.prototype.every Array.protot ...
- android架构图示
Android系统架构和一些普遍的操作系统差不多,都是采用了分层的架构,从他们之间的架构图看,Android系统架构分为四个层,从高层到低层分别是应用程序层.应用程序框架层.系统运行库层和linux核 ...
- Android之Notification的多种用法(转)
我们在用手机的时候,如果来了短信,而我们没有点击查看的话,是不是在手机的最上边的状态栏里有一个短信的小图标提示啊?你是不是也想实现这种功能呢?今天的Notification就是解决这个问题的. 我们也 ...
- 常见SQLException异常
ORA-00904: invalid column name 无效列名 ORA-00942: table or view does not exist 表或者视图不存在 ORA-01400: c ...
- shell 中数学计算总结
shell中的赋值和操作默认都是字符串处理,在此记下shell中进行数学运算的几个特殊方法,以后用到的时候可以来看,呵呵. 1.错误方法举例 a) var=1+1 echo $var ...
- Brush、Color、String相互转换
using System.Windows.Media; 1.String转换成Color Color color = (Color)ColorConverter.ConvertFromString(s ...
- Modularity模块化
Modularity in this context refers to test scripts, whereas independence refers to test cases. Given ...
- 大端小端(Big- Endian和Little-Endian)[转]
原文出处: 字节序(Endian),大端(Big-Endian),小端(Little-Endian) http://www.cppblog.com/tx7do/archive/2009/01/06/ ...
- bzoj 3781 小B的询问(莫队算法)
[题意] 若干个询问sigma{ cnt[i]^2 } cnt[i]表示i在[l,r]内的出现次数. [思路] 莫队算法,裸题. 一个cnt数组即可维护插入与删除. [代码] #include< ...
- effective c++:dynamic_cast,避免返回handles指向对象内部
关于dynamic_cast 假定我们有一个基类指针bp,我们在运行时需要把它转换成他的派生类指针,这个时候需要用到dynamic_cast. Derived *dp = dynamic_cast&l ...