codeforces 884 D. Boxes And Balls
2017-11-05 14:13
393 查看
D. Boxes And Balls
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Ivan has n different boxes. The first of them contains some balls of n different
colors.
Ivan wants to play a strange game. He wants to distribute the balls into boxes in such a way that for every i (1 ≤ i ≤ n) i-th
box will contain all balls with color i.
In order to do this, Ivan will make some turns. Each turn he does the following:
Ivan chooses any non-empty box and takes all balls from this box;
Then Ivan chooses any k empty boxes (the box from the
first step becomes empty, and Ivan is allowed to choose it), separates the balls he took on the previous step into k non-empty groups and puts each
group into one of the boxes. He should put each group into a separate box. He can choose either k = 2 or k = 3.
The penalty of the turn is the number of balls Ivan takes from the box during the first step of the turn. And penalty of
the game is the total penalty of turns made by Ivan until he distributes all balls to corresponding boxes.
Help Ivan to determine the minimum possible penalty of the game!
Input
The first line contains one integer number n (1 ≤ n ≤ 200000)
— the number of boxes and colors.
The second line contains n integer numbers a1, a2,
..., an (1 ≤ ai ≤ 109),
where ai is the number of
balls with color i.
Output
Print one number — the minimum possible penalty of the game.
Examples
input
output
input
output
Note
In the first example you take all the balls from the first box, choose k = 3 and sort all colors to corresponding boxes.
Penalty is 6.
In the second example you make two turns:
Take all the balls from the first box, choose k = 3,
put balls of color 3 to the third box, of color 4 — to the
fourth box and the rest put back into the first box. Penalty is 14;
Take all the balls from the first box, choose k = 2,
put balls of color 1 to the first box, of color 2 — to the
second box. Penalty is 5.
Total penalty is 19.
题意:三叉哈夫曼树
AC code
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn = 100003;
priority_queue<ll,vector<ll>,greater<ll> >q;
int main()
{
ll n;scanf("%lld",&n);
for(ll i=1;i<=n;i++)
{
ll x;scanf("%lld",&x);
q.push(x);
}
if(n%2==0) q.push(0);
ll ans=0;
while(q.size()>1)
{
ll a=q.top();q.pop();
ll b=q.top();q.pop();
ll c=q.top();q.pop();
ans+=a+b+c,q.push(a+b+c);
}
printf("%lld\n",ans);
return 0;
}
题意:
即3叉的哈夫曼树。
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Ivan has n different boxes. The first of them contains some balls of n different
colors.
Ivan wants to play a strange game. He wants to distribute the balls into boxes in such a way that for every i (1 ≤ i ≤ n) i-th
box will contain all balls with color i.
In order to do this, Ivan will make some turns. Each turn he does the following:
Ivan chooses any non-empty box and takes all balls from this box;
Then Ivan chooses any k empty boxes (the box from the
first step becomes empty, and Ivan is allowed to choose it), separates the balls he took on the previous step into k non-empty groups and puts each
group into one of the boxes. He should put each group into a separate box. He can choose either k = 2 or k = 3.
The penalty of the turn is the number of balls Ivan takes from the box during the first step of the turn. And penalty of
the game is the total penalty of turns made by Ivan until he distributes all balls to corresponding boxes.
Help Ivan to determine the minimum possible penalty of the game!
Input
The first line contains one integer number n (1 ≤ n ≤ 200000)
— the number of boxes and colors.
The second line contains n integer numbers a1, a2,
..., an (1 ≤ ai ≤ 109),
where ai is the number of
balls with color i.
Output
Print one number — the minimum possible penalty of the game.
Examples
input
3 1 2 3
output
6
input
4 2 3 4 5
output
19
Note
In the first example you take all the balls from the first box, choose k = 3 and sort all colors to corresponding boxes.
Penalty is 6.
In the second example you make two turns:
Take all the balls from the first box, choose k = 3,
put balls of color 3 to the third box, of color 4 — to the
fourth box and the rest put back into the first box. Penalty is 14;
Take all the balls from the first box, choose k = 2,
put balls of color 1 to the first box, of color 2 — to the
second box. Penalty is 5.
Total penalty is 19.
题意:三叉哈夫曼树
AC code
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn = 100003;
priority_queue<ll,vector<ll>,greater<ll> >q;
int main()
{
ll n;scanf("%lld",&n);
for(ll i=1;i<=n;i++)
{
ll x;scanf("%lld",&x);
q.push(x);
}
if(n%2==0) q.push(0);
ll ans=0;
while(q.size()>1)
{
ll a=q.top();q.pop();
ll b=q.top();q.pop();
ll c=q.top();q.pop();
ans+=a+b+c,q.push(a+b+c);
}
printf("%lld\n",ans);
return 0;
}
题意:
即3叉的哈夫曼树。
相关文章推荐
- CodeForces-884D:Boxes And Balls(合并石子)
- Codeforces 890 D. Boxes And Balls (哈夫曼)
- CodeForces - 260C Balls and Boxes(模拟)
- 数学,期望(Balls and Boxes,HDU 5810)
- hdu 5810:Balls and Boxes(期望)
- HDU 5810 Balls and Boxes(打表找规律)——2016 Multi-University Training Contest 7 1002
- hdu 5810 Balls and Boxes 推公式 ( 多校)
- UVALive 7500 Boxes and Balls (二分)
- Educational Codeforces Round 31 D. Boxes And Balls
- 【codeforces 768F】 Barrels and boxes
- CodeForces 653 A. Bear and Three Balls——(IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2))
- 【CodeForces】[653A]Bear and Three Balls
- Educational Codeforces Round 31- D. Boxes And Balls
- HDU 5810 Balls and Boxes(概率)
- HHUOJ 1020 Boxes and Balls(找规律)
- CodeForces 653 A. Bear and Three Balls
- Balls and Boxes
- codeforces 420-C. Okabe and Boxes
- hdu 5810 Balls and Boxes(2016 Multi-University Training Contest 7——数学题)
- HDU 5810 Balls and Boxes