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里的某一张图片添加一个点击事件该怎么做到 ...
随机推荐
- NDK xxxxx could not be resolved解决方法
Type '*****' could not be resolved Method '******' could not be resolved 问题解决 以下为未尝试方法,如果上面方法解 ...
- Android开发之获取时间SystemClock
转载:http://blog.csdn.net/tianfeng701/article/details/7562359 在Andriod中关于线程一部分中经常会遇到计算时间的操作,这里面应用较多的是S ...
- Java开发之反射的使用
通过类名获取类. Class serviceManager = Class.forName("android.os.ServiceManager"); 获取方法 Method me ...
- POJ1037A decorative fence(好dp)
1037 带点组合的东西吧 黑书P257 其实我没看懂它写的嘛玩意儿 这题还是挺不错的 一个模糊的思路可能会好想一些 就是大体的递推方程 dp1[][]表示降序 dp2[][]表示升序 数组的含义为长 ...
- 结构体key
http://www.cnblogs.com/xpchild/p/3770823.html http://blog.sae.sina.com.cn/archives/3968 实例 http://bl ...
- ASP.NET MVC 学习7、为Model Class的字段添加验证属性(validation attribuate)
Adding Validation to the Model ,在Model中添加数据验证 参考:http://www.asp.net/mvc/tutorials/mvc-4/getting-star ...
- bzoj2326: [HNOI2011]数学作业
矩阵快速幂,分1-9,10-99...看黄学长的代码理解...然而他直接把答案保存在最后一行(没有说明...好吧应该是我智障这都不知道... #include<cstdio> #inclu ...
- Deferred的那些知识
在移动开发中的各种中,我们一定会遇到异步回调的问题,比如: 1:Css3执行动画完毕, 回调 2:Jquery Animate动画的执行完毕, 回调 3:Ajax的执行(并行.串行),回调 等等 ...
- 02day1
淘汰赛制 递推 [问题描述] 淘汰赛制是一种极其残酷的比赛制度.2^n名选手分别标号1,2,3,…,2^n-1,2^n,他们将要参加n轮的激烈角逐.每一轮中,将所有参加该轮的选手按标号从小到大排序后, ...
- Android自定义的webView——可实现的网页文本的复制
package com.example.customlinearlayout.view; import android.app.ProgressDialog; import android.conte ...