2019年个人训练赛第一场-E - Equalizing by Division (hard version)
Posted gbtj
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2019年个人训练赛第一场-E - Equalizing by Division (hard version)相关的知识,希望对你有一定的参考价值。
The only difference between easy and hard versions is the number of elements in the array.
You are given an array aa consisting of nn integers. In one move you can choose any aiai and divide it by 22 rounding down (in other words, in one move you can set ai:=⌊ai2⌋ai:=⌊ai2⌋).
You can perform such an operation any (possibly, zero) number of times with any aiai.
Your task is to calculate the minimum possible number of operations required to obtain at least kk equal numbers in the array.
Don‘t forget that it is possible to have ai=0ai=0 after some operations, thus the answer always exists.
Input
The first line of the input contains two integers nn and kk (1≤k≤n≤2⋅1051≤k≤n≤2⋅105) — the number of elements in the array and the number of equal numbers required.
The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤2⋅1051≤ai≤2⋅105), where aiai is the ii-th element of aa.
Output
The first line of the input contains two integers nn and kk (1≤k≤n≤2⋅1051≤k≤n≤2⋅105) — the number of elements in the array and the number of equal numbers required.
The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤2⋅1051≤ai≤2⋅105), where aiai is the ii-th element of aa.
Exanple
5 3 1 2 2 4 5
1
5 3 1 2 3 4 5
2
5 3 1 2 3 3 3
0
#include<iostream> #include<algorithm> #include<queue> #include<cstdio> #define INF 0x3f3f3f3f using namespace std; priority_queue<int,vector<int>,greater<int> > q[(int)3e5]; int main() int x,y,a; scanf("%d%d",&x,&y); for(int i=0;i<x;i++) scanf("%d",&a); q[a].push(0); int cnt=1; while(a) a/=2; q[a].push(cnt); cnt++; int asn=INF; for(int i=0;i < 3e5;i++) if(q[i].size()<y) continue; else int sum=0; for(int j=0;j<y;j++) sum=sum+q[i].top(); q[i].pop(); asn=min(asn, sum); printf("%d\n",asn); return 0;
以上是关于2019年个人训练赛第一场-E - Equalizing by Division (hard version)的主要内容,如果未能解决你的问题,请参考以下文章
Contest1539 - 2019年我能变强组队训练赛第十一场
2018年全国多校算法寒假训练营练习比赛(第一场)E 恋与程序员
2019个人训练赛第二场-A - BowWow and the Timetable