Canvas Painting
题目链接:
http://acm.hust.edu.cn/vjudge/contest/127406#problem/C
Description
http://7xjob4.com1.z0.glb.clouddn.com/a4717ad58f73aa6ff84a1ab3f051c3f8Input
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 5Sample Output
29 40##题意: 给出N张白布(顺序不定). 每次选出其中同一种颜色的若干张布染上某种跟之前不同的色,这种颜色剩下的布染上另一种颜色. 每次染色的花费是布的大小. 求要将N张布都染成不同的颜色的最小花费.
##题解: 一开始想的是面积大的布染尽量少的次数,先降序排列,对后缀和求和. 这个思路并不正确. (比如样例2) 这个问题反过来看就比较简单了: 最后的结果是N张颜色各异的布,反向过程是每次选出两种颜色不同的布刷成同一颜色. 这样一来,每次操作都使得集合的大小减一. 所以总次数固定是N-1. 对于每一次操作,选择最小的两张布染色一定是最小花费. 而每次的最小花费和就是总的最小花费.
维护一个优先队列,把所有大小都加进去并升序排列. 每次弹出最小的两个元素,计数并把和再push进去参与比较.
##代码: ``` cpp #include