UVALive 7274 Canvas Painting (优先队列)
2016-08-13 22:59
411 查看
Canvas Painting
题目链接:
http://acm.hust.edu.cn/vjudge/contest/127406#problem/CDescription
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 bytwo 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 inorder to have all canvasses with different colors.
Sample Input
23
7 4 7
4
5 3 7 5
Sample Output
2940
题意:
给出N张白布(顺序不定).每次选出其中同一种颜色的若干张布染上某种跟之前不同的色,这种颜色剩下的布染上另一种颜色.
每次染色的花费是布的大小.
求要将N张布都染成不同的颜色的最小花费.
题解:
一开始想的是面积大的布染尽量少的次数,先降序排列,对后缀和求和. 这个思路并不正确. (比如样例2)这个问题反过来看就比较简单了:
最后的结果是N张颜色各异的布,反向过程是每次选出两种颜色不同的布刷成同一颜色.
这样一来,每次操作都使得集合的大小减一. 所以总次数固定是N-1.
对于每一次操作,选择最小的两张布染色一定是最小花费. 而每次的最小花费和就是总的最小花费.
维护一个优先队列,把所有大小都加进去并升序排列.
每次弹出最小的两个元素,计数并把和再push进去参与比较.
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <queue> #include <map> #include <set> #include <stack> #include <vector> #include <list> #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<LL>, greater<LL> > 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 6440 Emergency Handling 优先队列
- UVaLive 2757 Supermarket (贪心+优先队列)
- UVALive 4290 Easy Climb(dp + 精简状态 + 优先队列)
- UVALive - 4254 Processor 二分+优先队列
- UVALive 3135 Argus(优先队列)
- UVALive 6485 Electric Car Rally (BFS,优先队列)
- UVALive 6093 Emergency Room --优先队列实现的模拟
- UVALive - 3135 Argus 优先队列
- UVALive 6093 Emergency Room --优先队列实现的模拟
- UVALive - 3135 Argus——优先队列
- Problem on Group Trip UVALive - 7219 (模拟+优先队列处理)
- UVALive - 2757 Supermarket 贪心+优先队列
- UVALive - 3135 Argus 优先队列
- Beijing 2004 / UVa 1203 / UVALive 3135 / ZOJ 2212 Argus (优先队列)
- UVaLive 4254 Processor (二分+优先队列)
- UvaLive 4254 Processor 优先队列
- UVALive3135 UVA1203 POJ2051 ZOJ2212 Argus【优先队列+优先搜索】
- UVALive3638 UVA12100 POJ3125 HDU1972 Printer Queue(优先队列+队列)
- uvalive4254(贪心+优先队列)
- UVALive 3135 Argus(优先队列)