UVALive 7274 Canvas Painting (优先队列)
Canvas Painting
题目链接:
http://acm.hust.edu.cn/vjudge/contest/127406#problem/C
Description
http://7xjob4.com1.z0.glb.clouddn.com/a4717ad58f73aa6ff84a1ab3f051c3f8
Input
The first line consists of a single integer T, the number of test cases. Each test case is composed by
two lines. The first line consists of a single integer N representing the number of canvasses. The next
line contains N space separated integers representing the sizes of the canvasses.
Constraints:
1 ≤ T ≤ 100 Number of test cases.
1 ≤ Ni ≤ 100 000 Number of canvasses in the i
th test case.
1 ≤ s ≤ 100 000 Size of each canvas.
1 ≤ ∑Ti=1 Ni ≤ 100 000 Number of canvasses over all test cases in one test file.
Output
The output contains T lines, one for each test case: the minimum amount of ink the machine needs in
order to have all canvasses with different colors.
Sample Input
2
3
7 4 7
4
5 3 7 5
Sample Output
29
40
##题意:
给出N张白布(顺序不定).
每次选出其中同一种颜色的若干张布染上某种跟之前不同的色,这种颜色剩下的布染上另一种颜色.
每次染色的花费是布的大小.
求要将N张布都染成不同的颜色的最小花费.
##题解:
一开始想的是面积大的布染尽量少的次数,先降序排列,对后缀和求和. 这个思路并不正确. (比如样例2)
这个问题反过来看就比较简单了:
最后的结果是N张颜色各异的布,反向过程是每次选出两种颜色不同的布刷成同一颜色.
这样一来,每次操作都使得集合的大小减一. 所以总次数固定是N-1.
对于每一次操作,选择最小的两张布染色一定是最小花费. 而每次的最小花费和就是总的最小花费.
维护一个优先队列,把所有大小都加进去并升序排列.
每次弹出最小的两个元素,计数并把和再push进去参与比较.
##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define eps 1e-8
#define maxn 101000
#define mod 100000007
#define inf 0x3f3f3f3f
#define mid(a,b) ((a+b)>>1)
#define IN freopen("in.txt","r",stdin);
using namespace std;
priority_queue<LL, vector, greater > pq;
int main(int argc, char const *argv[])
{
//IN;
int t; cin >> t;
while(t--)
{
int n; scanf("%d", &n);
while(!pq.empty()) pq.pop();
for(int i=1; i<=n; i++) {
LL x; scanf("%lld", &x);
pq.push(x);
}
LL ans = 0;
while(pq.size() >= 2) {
LL cur = pq.top(); pq.pop();
cur += pq.top(); pq.pop();
ans += cur;
pq.push(cur);
}
printf("%lld\n", ans);
}
return 0;
}
UVALive 7274 Canvas Painting (优先队列)的更多相关文章
- UVALive 6093 Emergency Room --优先队列实现的模拟
题意:给n个医生,这些医生有一个上班时间,然后给一些病人,病人有一个到达的时间,以及一些诊断,诊断有property(优先级)和duration(诊断时间)这两个属性,每个病人可能要诊断多次,最后问每 ...
- UVALive 3135--Argus+自己定义优先队列的优先规则
题目链接:id=18684">点击进入 仅仅是题意比較难懂,读懂题后全然能够用优先队列水过去.这次学会自己定义优先队列的优先规则,事实上就是在结构体中重载一下<运算符. 代码例如 ...
- Gym - 101128C:Canvas Painting
这个就是哈夫曼树哇~ 我们仨英语太差了,跟榜时候才看出来是哈夫曼树雾 一个优先队列就可以搞定 #include <cstdio> #include <algorithm> #i ...
- uva11997 K Smallest Sums&&UVALive 3135 Argus(优先队列,多路归并)
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #inc ...
- UVaLive 4254 Processor (二分+优先队列)
题意:有n个任务,每个任务有三个参数,r,d,w,表示该任务必须在[r,d]之间执行,工作量是w,处理器执行速度可以变化,当执行速度是s的时候, 一个工作量是w的任务需要需要的执行时间是w/s个工作单 ...
- 【贪心】【堆】Gym - 101128C - Canvas Painting
一些画布,每块有其大小,一开始都是白的,你任意将它们排序,然后一次操作可以选择一段连续的相同颜色的画布,从中任选一个位置,左侧涂上任意一种颜色,右侧涂上另一种.消耗是这一段画布的总的大小.问你要将所有 ...
- 【优先级队列】Southwestern Europe Regional Contest Canvas Painting
https://vjudge.net/contest/174235#problem/D [题意] 给定n个已知size的帆布,要给这n块帆布涂上不同的颜色,规则是这样的: 每次选择一种颜色C 对于颜色 ...
- Canvas事件绑定
canvas事件绑定 众所周知canvas是位图,在位图里我们可以在里面画各种东西,可以是图片,可以是线条等等.那我们想给canvas里的某一张图片添加一个点击事件该怎么做到.而js只能监听到canv ...
- canvas 事件绑定
Canvas事件绑定 canvas事件绑定 众所周知canvas是位图,在位图里我们可以在里面画各种东西,可以是图片,可以是线条等等.那我们想给canvas里的某一张图片添加一个点击事件该怎么做到 ...
随机推荐
- 面试题_89_to_92_单元测试 JUnit 面试题
89)如何测试静态方法?(答案)可以使用 PowerMock 库来测试静态方法. 90)怎么利用 JUnit 来测试一个方法的异常?(答案) 91)你使用过哪个单元测试库来测试你的 Java 程序?( ...
- ASP.NET 数据绑定常用代码及其性能分析
用DataBinder.eval 绑定不必关心数据来源(Dataread或dataset).不必关心数据的类型eval会把这个数据对象转换为一个字符串.在底层绑定做了很多工作,使用了反射性能.正因为使 ...
- AspNet WebApi OData 学习
OData介绍:是一个查询和更新数据的Web协议.OData应用了web技术如HTTP.Atom发布协议(AtomPub)和JSON等来提供对不同应用程序,服务 和存储的信息访问.除了提供一些基本的操 ...
- Sublime-text markdown with Vim mode and auto preview
说明 最近看到markdown相关的东西,被其书写方式吸引,其实以前就在找这种类似的工具,但是也没找到,由于习惯了Vim,可Vim不支持markdown预览,这点可能不是很好,于是找到Sublime- ...
- I.MX6 Ubuntu core porting
/*********************************************************************** * I.MX6 Ubuntu core porting ...
- 【Mysql】初学命令行指南
MYSQL初学者使用指南与介绍 一.连接MYSQL 格式: mysql -h主机地址 -u用户名 -p用户密码 1.例1:连接到本机上的MYSQL. 首先在打开DOS窗口,然后进入目录 mysqlbi ...
- wifi详解(五)
1 Android平台的Wifi模块移植要点 1.1 Wifi结构 user interface Android WiFiService WPA_Supplicant DHD ...
- Android下载速度计算
long startTime = System.currentTimeMillis(); // 开始下载时获取开始时间 long curTime = System.currentTimeMillis( ...
- .net-C#代码判断
ylbtech-doc:.net-C#代码判断 C#代码判断 1.A,C#代码判断返回顶部 01.{ C#题目}public static void Main(string[] args){ ...
- Android控件之GridView
GridView是一项显示二维的viewgroup,可滚动的网格.一般用来显示多张图片. 以下模拟九宫图的实现,当鼠标点击图片时会进行相应的跳转链接. 目录结构 main.xml布局文件,存放Grid ...