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 (优先队列)的更多相关文章

  1. UVALive 6093 Emergency Room --优先队列实现的模拟

    题意:给n个医生,这些医生有一个上班时间,然后给一些病人,病人有一个到达的时间,以及一些诊断,诊断有property(优先级)和duration(诊断时间)这两个属性,每个病人可能要诊断多次,最后问每 ...

  2. UVALive 3135--Argus+自己定义优先队列的优先规则

    题目链接:id=18684">点击进入 仅仅是题意比較难懂,读懂题后全然能够用优先队列水过去.这次学会自己定义优先队列的优先规则,事实上就是在结构体中重载一下<运算符. 代码例如 ...

  3. Gym - 101128C:Canvas Painting

    这个就是哈夫曼树哇~ 我们仨英语太差了,跟榜时候才看出来是哈夫曼树雾 一个优先队列就可以搞定 #include <cstdio> #include <algorithm> #i ...

  4. uva11997 K Smallest Sums&&UVALive 3135 Argus(优先队列,多路归并)

    #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #inc ...

  5. UVaLive 4254 Processor (二分+优先队列)

    题意:有n个任务,每个任务有三个参数,r,d,w,表示该任务必须在[r,d]之间执行,工作量是w,处理器执行速度可以变化,当执行速度是s的时候, 一个工作量是w的任务需要需要的执行时间是w/s个工作单 ...

  6. 【贪心】【堆】Gym - 101128C - Canvas Painting

    一些画布,每块有其大小,一开始都是白的,你任意将它们排序,然后一次操作可以选择一段连续的相同颜色的画布,从中任选一个位置,左侧涂上任意一种颜色,右侧涂上另一种.消耗是这一段画布的总的大小.问你要将所有 ...

  7. 【优先级队列】Southwestern Europe Regional Contest Canvas Painting

    https://vjudge.net/contest/174235#problem/D [题意] 给定n个已知size的帆布,要给这n块帆布涂上不同的颜色,规则是这样的: 每次选择一种颜色C 对于颜色 ...

  8. Canvas事件绑定

    canvas事件绑定 众所周知canvas是位图,在位图里我们可以在里面画各种东西,可以是图片,可以是线条等等.那我们想给canvas里的某一张图片添加一个点击事件该怎么做到.而js只能监听到canv ...

  9. canvas 事件绑定

    Canvas事件绑定   canvas事件绑定 众所周知canvas是位图,在位图里我们可以在里面画各种东西,可以是图片,可以是线条等等.那我们想给canvas里的某一张图片添加一个点击事件该怎么做到 ...

随机推荐

  1. Android开发之创建App Widget和更新Widget内容

    App WidgetsApp Widgets are miniature application views that can be embedded in other applications (s ...

  2. [Codeforces137B]Permutation(贪心?思路?,水题)

    题目链接:http://codeforces.com/contest/137/problem/B 给n个数字,要求修改成1~n的全排列数中的一个,修改的次数尽可能少,问最少需要修改几个数. 记下数组里 ...

  3. hdu 携程全球数据中心建设 (球面距离 + 最小生成树)

    题目 #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib&g ...

  4. VB VS2003获取当前进程用户登录

    Page.User.Identity.Name获取当前进程用户名称,VS03才可以用

  5. VI使用的小白教程

    vi 使用方法vi编辑器是所有Unix及Linux系统下标准的编辑器,它的强 大不逊色于任何最新的文本编辑器,这里只是简单地介绍一下它的用法和一小部分指令.由于对Unix及Linux系统的任何版本,v ...

  6. 纯css做的安卓开机动画

    随着css3的发展,越来越多的负责绚丽的效果可以由纯css来完成了.用css3实现的动画效果丝毫不必js实现的逊色,而且浏览器对css渲染的速度远比js快,大多数时候css的体积也不js小.其中css ...

  7. 【Java学习笔记】Hello world

    package aaa; public class aaa { public static void main(String args[]){ System.out.println("hel ...

  8. 配置ORACLE 客户端连接到数据库

    --================================= -- 配置ORACLE 客户端连接到数据库 --================================= Oracle ...

  9. Oracle 手工清除回滚段的几种方法

    关于回滚段的问题,之前也小整理过一个,参考: Current online Redo 和 Undo 损坏的处理方法 http://blog.csdn.net/tianlesoftware/articl ...

  10. 【转】如何用WINDBG分析64位机上32位程序的DUMP文件

    将dump拖入到windbg中后,在command输入栏输入 .load wow64exts 回车 !sw 回车,就将windbg的dump,从64位模式切换到了32位模式,否则看到的call sta ...