观察到 于是合理猜测 可以通过。
我们考虑延续堆贪心的做法,此做法的瓶颈是 是因为堆里面需要放入过多的数字,由于 和值域差距极大,所以必然有很多数字重复,我们考虑重复数字只插入 个,如果用完了就继续插入 个,这样可以极大的减小堆的大小。
由于时间限制比较死,我们实现的时候可以使用 splitmix64 的哈希表,在手写堆即可通过,代码中 取到了 可以轻松通过。
设整个过程中出现过的不同权值数量为 。
由于相同权值只在堆中保留一个代表,因此堆中元素个数始终不超过 。
初始时,对每种出现过的权值入堆一次,复杂度为:
合并过程中一共会进行 次操作,每次操作需要:
- 两次取最小值;
- 一次插入新的合并权值;
哈希表操作均摊复杂度为:
而堆操作复杂度为:
因此总复杂度为:
在最坏情况下,合并过程中可能产生大量不同的新权值,此时:
因此最坏时间复杂度为:
由于本题初始值域较小,且大量相同权值会被压缩存储,因此实际运行时 往往远小于 ,常数表现优于普通堆实现。
code
//Author:kevinZ99
#include <bits/stdc++.h>
#define up(a,b,c) for(int (a)=(b);(a)<=(c);(a)=-~(a))
#define dn(a,b,c) for(int (a)=(b);(a)>=(c);(a)=~-(a))
#define fst first
#define sed second
#define pref static inline
#define gc() p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<12,stdin),p1==p2)?EOF:*p1++
using namespace std; using hint = __int128;using pii = pair< int , int > ;
using us = unsigned short ;using ldb = long double ;using ll = long long;
using ull= unsigned long long;using ui=unsigned int;using pll=pair<ll,ll>;
using pil= pair<int,ll> ;using vpil = vector<pil>;using vl = vector<ll>;
using pli= pair<ll,int>;using vpli = vector<pli>;using vi =vector<int>;
using vpi= vector< pii > ;using vpl = vector<pll> ; using db = double ;
namespace mystl{
char buf[1<<20], *p1=buf, *p2=buf, sr[1<<23], z[23], nc;int C=-1 ,Z=0;
template <typename T>pref void read ( T & x ) { bool flag = false ;
while( nc = gc() ,(nc<48||nc>57) && nc!=-1)flag|=(nc==45);x=nc-48;
while(nc=gc(),47<nc&&nc<58)x=(x<<3)+(x<<1)+(nc^48); if(flag)x=-x;}
template < typename T , typename ... Args_Arrays_Typename_KevinZ99 >
void read(T&x,Args_Arrays_Typename_KevinZ99&...a){read(x);read(a...);}
pref void ot( ) { fwrite( sr , 1 , C + 1, stdout ) ; C = - 1 ; }
pref void flush( ) { if ( C > 1<<22 ) ot() ; } template <typename T>
pref void write(T x,char t) { int y = 0 ; if ( x < 0 ) y = 1, x = -x;
while( z [ ++ Z ] = x % 10 + 48 , x /= 10) ; if( y ) z[ ++Z ]='-';
while( sr[ ++ C ] = z[ Z ] , -- Z ) ; sr [ ++C ] = t ; flush() ; }
pref void write(char x) {sr[C=-~C]=x;}pref void write(string s){for(
char t:s)write(t);}pref ll qpow(ll a , ll b,ll p) {if(a==0)return 0;
ll c=1ll; while(b) { if(b & 1) c=a*c%p; a=a*a%p; b>>=1; } return c ; }
pref ll lcm ( ll x , ll y ) {return x / std :: __gcd( x , y ) * y ;}
};
using namespace mystl;
template<class K, class V>
class Unordered_map {
static_assert(std::is_integral<K>::value,
"Unordered_map super-fast mode requires integral K.");
private:
struct Node {
K k;
V v;
};
std::vector<Node> tab_;
std::vector<unsigned char> st_;
std::size_t mask_;
std::size_t sz_;
std::size_t used_;
float max_lf_;
static inline uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15ULL;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9ULL;
x = (x ^ (x >> 27)) * 0x94d049bb133111ebULL;
return x ^ (x >> 31);
}
static inline std::size_t ceil_pow2(std::size_t x) {
std::size_t n = 1;
while (n < x) n <<= 1;
return n;
}
inline std::size_t hash_key(const K& key) const {
return (std::size_t)splitmix64((uint64_t)key) & mask_;
}
inline void init_pow2(std::size_t cap) {
std::size_t n = ceil_pow2(cap < 8 ? 8 : cap);
tab_.assign(n, Node());
st_.assign(n, 0);
mask_ = n - 1;
sz_ = 0;
used_ = 0;
}
inline std::size_t find_slot(const K& key) const {
std::size_t i = hash_key(key);
std::size_t first_del = (std::size_t)-1;
while (true) {
unsigned char s = st_[i];
if (s == 0) return (first_del == (std::size_t)-1 ? i : first_del);
if (s == 1 && tab_[i].k == key) return i;
if (s == 2 && first_del == (std::size_t)-1) first_del = i;
i = (i + 1) & mask_;
}
}
inline std::size_t find_existing_slot(const K& key) const {
std::size_t i = hash_key(key);
while (true) {
unsigned char s = st_[i];
if (s == 0) return (std::size_t)-1;
if (s == 1 && tab_[i].k == key) return i;
i = (i + 1) & mask_;
}
}
inline void maybe_rebuild_before_insert() {
if (tab_.empty()) init_pow2(8);
if ((float)(used_ + 1) > (float)tab_.size() * max_lf_) {
rehash(tab_.size() << 1);
return;
}
if (used_ > sz_ + (sz_ >> 1) && tab_.size() >= 64) {
rehash(tab_.size());
}
}
public:
class iterator {
friend class Unordered_map;
Unordered_map* mp_;
std::size_t i_;
inline void skip_to_full() {
while (i_ < mp_->tab_.size() && mp_->st_[i_] != 1) ++i_;
}
inline iterator(Unordered_map* mp, std::size_t i) : mp_(mp), i_(i) {
if (mp_) skip_to_full();
}
public:
typedef std::forward_iterator_tag iterator_category;
typedef std::pair<const K, V> value_type;
typedef std::ptrdiff_t difference_type;
inline iterator() : mp_(0), i_(0) {}
inline std::pair<const K&, V&> operator*() const {
return std::pair<const K&, V&>(mp_->tab_[i_].k, mp_->tab_[i_].v);
}
inline iterator& operator++() {
++i_; skip_to_full();
return *this;
}
inline bool operator==(const iterator& o) const { return mp_ == o.mp_ && i_ == o.i_; }
inline bool operator!=(const iterator& o) const { return !(*this == o); }
inline std::size_t index() const { return i_; }
};
class const_iterator {
friend class Unordered_map;
const Unordered_map* mp_;
std::size_t i_;
inline void skip_to_full() {
while (i_ < mp_->tab_.size() && mp_->st_[i_] != 1) ++i_;
}
inline const_iterator(const Unordered_map* mp, std::size_t i) : mp_(mp), i_(i) {
if (mp_) skip_to_full();
}
public:
typedef std::forward_iterator_tag iterator_category;
typedef std::pair<const K, const V> value_type;
typedef std::ptrdiff_t difference_type;
inline const_iterator() : mp_(0), i_(0) {}
inline std::pair<const K&, const V&> operator*() const {
return std::pair<const K&, const V&>(mp_->tab_[i_].k, mp_->tab_[i_].v);
}inline const_iterator& operator++() {
++i_; skip_to_full();
return *this;
}inline bool operator==(const const_iterator& o) const { return mp_ == o.mp_ && i_ == o.i_; }
inline bool operator!=(const const_iterator& o) const { return !(*this == o); }
inline std::size_t index() const { return i_; }
};inline Unordered_map() : mask_(0), sz_(0), used_(0), max_lf_(0.70f) {
init_pow2(8);
}inline explicit Unordered_map(std::size_t reserve_n) : mask_(0), sz_(0), used_(0), max_lf_(0.70f) {
std::size_t cap = 8;
while ((float)cap * max_lf_ < (float)reserve_n) cap <<= 1;
init_pow2(cap);
}inline Unordered_map(std::initializer_list<std::pair<K, V> > il)
: mask_(0), sz_(0), used_(0), max_lf_(0.70f) {
std::size_t cap = 8;
while ((float)cap * max_lf_ < (float)il.size()) cap <<= 1;
init_pow2(cap);
for (typename std::initializer_list<std::pair<K, V> >::const_iterator it = il.begin(); it != il.end(); ++it) {
insert_or_assign(it->first, it->second);
}
}inline iterator begin() { return iterator(this, 0); }
inline iterator end() { return iterator(this, tab_.size()); }
inline const_iterator begin() const { return const_iterator(this, 0); }
inline const_iterator end() const { return const_iterator(this, tab_.size()); }
inline const_iterator cbegin() const { return const_iterator(this, 0); }
inline const_iterator cend() const { return const_iterator(this, tab_.size()); }
inline bool empty() const { return sz_ == 0; }
inline std::size_t size() const { return sz_; }
inline std::size_t bucket_count() const { return tab_.size(); }
inline float load_factor() const {
return tab_.empty() ? 0.0f : (float)sz_ / (float)tab_.size();
}inline float max_load_factor() const { return max_lf_; }
inline void max_load_factor(float z) {
if (z <= 0.1f) z = 0.1f;
if (z >= 0.95f) z = 0.95f;
max_lf_ = z;
if (load_factor() > max_lf_) rehash(tab_.size() << 1);
}inline void clear() {
std::fill(st_.begin(), st_.end(), (unsigned char)0);
sz_ = 0;
used_ = 0;
}inline void reserve(std::size_t n) {
std::size_t cap = 8;
while ((float)cap * max_lf_ < (float)n) cap <<= 1;
if (cap > tab_.size()) rehash(cap);
}void rehash(std::size_t n) {
std::vector<Node> old_tab;
std::vector<unsigned char> old_st;
old_tab.swap(tab_);
old_st.swap(st_);
init_pow2(n);
for (std::size_t i = 0; i < old_tab.size(); ++i) {
if (old_st[i] == 1) {
insert_or_assign(old_tab[i].k, old_tab[i].v);
}
}
}inline void swap(Unordered_map& o) {
tab_.swap(o.tab_);
st_.swap(o.st_);
std::swap(mask_, o.mask_);
std::swap(sz_, o.sz_);
std::swap(used_, o.used_);
std::swap(max_lf_, o.max_lf_);
}inline std::size_t count(const K& key) const {
return find_existing_slot(key) == (std::size_t)-1 ? 0u : 1u;
}inline bool contains(const K& key) const {
return find_existing_slot(key) != (std::size_t)-1;
}inline iterator find(const K& key) {
std::size_t p = find_existing_slot(key);
return (p == (std::size_t)-1) ? end() : iterator(this, p);
}inline const_iterator find(const K& key) const {
std::size_t p = find_existing_slot(key);
return (p == (std::size_t)-1) ? end() : const_iterator(this, p);
}inline V* find_ptr(const K& key) {
std::size_t p = find_existing_slot(key);
return (p == (std::size_t)-1) ? 0 : &tab_[p].v;
}inline const V* find_ptr(const K& key) const {
std::size_t p = find_existing_slot(key);
return (p == (std::size_t)-1) ? 0 : &tab_[p].v;
}inline V& operator[](const K& key) {
maybe_rebuild_before_insert();
std::size_t p = find_slot(key);
if (st_[p] != 1) {
if (st_[p] == 0) ++used_;
st_[p] = 1;
tab_[p].k = key;
tab_[p].v = V();
++sz_;
}
return tab_[p].v;
}
inline V& at(const K& key) {
std::size_t p = find_existing_slot(key);
if (p == (std::size_t)-1) throw std::out_of_range("Unordered_map::at");
return tab_[p].v;
}
inline const V& at(const K& key) const {
std::size_t p = find_existing_slot(key);
if (p == (std::size_t)-1) throw std::out_of_range("Unordered_map::at");
return tab_[p].v;
}
inline std::pair<iterator, bool> insert(const std::pair<K, V>& kv) {
maybe_rebuild_before_insert();
std::size_t p = find_slot(kv.first);
if (st_[p] == 1) return std::make_pair(iterator(this, p), false);
if (st_[p] == 0) ++used_;
st_[p] = 1;
tab_[p].k = kv.first;
tab_[p].v = kv.second;
++sz_;
return std::make_pair(iterator(this, p), true);
}
inline bool insert_or_assign(const K& key, const V& val) {
maybe_rebuild_before_insert();
std::size_t p = find_slot(key);
if (st_[p] == 1) {
tab_[p].v = val;
return false;
}
if (st_[p] == 0) ++used_;
st_[p] = 1;
tab_[p].k = key;
tab_[p].v = val;
++sz_;
return true;
}
template<class... Args>
inline std::pair<iterator, bool> emplace(const K& key, Args&&... args) {
maybe_rebuild_before_insert();
std::size_t p = find_slot(key);
if (st_[p] == 1) return std::make_pair(iterator(this, p), false);
if (st_[p] == 0) ++used_;
st_[p] = 1;
tab_[p].k = key;
tab_[p].v = V(std::forward<Args>(args)...);
++sz_;
return std::make_pair(iterator(this, p), true);
}
template<class... Args>
inline std::pair<iterator, bool> try_emplace(const K& key, Args&&... args) {
return emplace(key, std::forward<Args>(args)...);
}
inline std::size_t erase(const K& key) {
std::size_t p = find_existing_slot(key);
if (p == (std::size_t)-1) return 0;
st_[p] = 2;
--sz_;
return 1;
}
inline iterator erase(iterator it) {
if (it == end()) return end();
std::size_t p = it.index();
st_[p] = 2;
--sz_;
++it;
return it;
}
};
template<class K, class V>
inline void swap(Unordered_map<K, V>& a, Unordered_map<K, V>& b) {
a.swap(b);
}
#include <vector>
#include <functional>
#include <utility>
#include <cassert>
template <class T, class Container = std::vector<T>, class Compare = std::less<T>>
class Priority_queue {
public:
using value_type = T;
using container_type = Container;
using value_compare = Compare;
using size_type = typename Container::size_type;
using const_reference = const value_type&;
private:
Container c;
Compare comp;
inline void sift_up(size_type i) {
value_type v = std::move(c[i]);
while (i) {
size_type p = (i - 1) >> 1;
if (!comp(c[p], v)) break;
c[i] = std::move(c[p]);
i = p;
}
c[i] = std::move(v);
}
inline void sift_down(size_type i) {
const size_type n = c.size();
value_type v = std::move(c[i]);
while (true) {
size_type l = (i << 1) + 1;
if (l >= n) break;
size_type r = l + 1;
size_type best = (r < n && comp(c[l], c[r])) ? r : l;
if (!comp(v, c[best])) break;
c[i] = std::move(c[best]);
i = best;
}
c[i] = std::move(v);
}
inline void build_heap() {
if (c.empty()) return;
for (size_type i = c.size() / 2; i-- > 0;)
sift_down(i);
}
public:
Priority_queue() = default;
explicit Priority_queue(const Compare& compare)
: c(), comp(compare) {}
Priority_queue(const Compare& compare, const Container& cont)
: c(cont), comp(compare) {
build_heap();
}
Priority_queue(const Compare& compare, Container&& cont)
: c(std::move(cont)), comp(compare) {
build_heap();
}
explicit Priority_queue(const Container& cont)
: c(cont), comp() {
build_heap();
}
explicit Priority_queue(Container&& cont)
: c(std::move(cont)), comp() {
build_heap();
}
template <class It>
Priority_queue(It first, It last, const Compare& compare = Compare())
: c(first, last), comp(compare) {
build_heap();
}
[[nodiscard]] inline bool empty() const noexcept {
return c.empty();
}
[[nodiscard]] inline size_type size() const noexcept {
return c.size();
}
inline const_reference top() const {
assert(!c.empty());
return c.front();
}
inline void push(const value_type& x) {
c.push_back(x);
sift_up(c.size() - 1);
}
inline void push(value_type&& x) {
c.push_back(std::move(x));
sift_up(c.size() - 1);
}
template <class... Args>
inline void emplace(Args&&... args) {
c.emplace_back(std::forward<Args>(args)...);
sift_up(c.size() - 1);
}
inline void pop() {
assert(!c.empty());
if (c.size() == 1) {
c.pop_back();
return;
}
c.front() = std::move(c.back());
c.pop_back();
sift_down(0);
}
inline void swap(Priority_queue& other) noexcept(
noexcept(std::swap(c, other.c)) && noexcept(std::swap(comp, other.comp))) {
using std::swap;
swap(c, other.c);
swap(comp, other.comp);
}
inline void reserve(size_type n) {c.reserve(n);}
inline bool erase(const value_type& x) {
for (size_type i = 0; i < c.size(); ++i) {
if (c[i] == x) {
const size_type last = c.size() - 1;
if (i == last) {
c.pop_back();
return true;
}
c[i] = std::move(c.back());
c.pop_back();
if (i > 0 && comp(c[(i - 1) >> 1], c[i]))
sift_up(i);
else
sift_down(i);
return true;
}
}
return false;
}
};
template <class T, class Container, class Compare>
inline void swap(Priority_queue<T, Container, Compare>& a,
Priority_queue<T, Container, Compare>& b) noexcept(noexcept(a.swap(b))) {
a.swap(b);
}
namespace my{
Unordered_map<ll,int>mp;
Priority_queue<ll,vl,greater<ll>>q;
int n,x;
pref ll get_min(){
while (true){
ll val=q.top();
if (!mp.contains(val)){
q.pop();
continue;
}
int* cnt_ptr=mp.find_ptr(val);
if (cnt_ptr==nullptr||*cnt_ptr==0){
q.pop();
if(cnt_ptr) mp.erase(val);
continue;
}
(*cnt_ptr)--;
if (*cnt_ptr==0)
mp.erase(val);
return val;
}
}
pref void SOLVE(){
read(n);
up(i,1,n)read(x),mp[x]++;
for (auto it=mp.begin();it!=mp.end();++it) {
ll key=0;
auto kv_copy=*it;
key = kv_copy.fst;
q.push(key);
}
ll ans=0;
int siz=n;
while(siz>1){
ll x=get_min();
ll y=get_min();
ll sum=x+y;
ans+=sum;
if(mp.contains(sum))mp[sum]++;
else mp[sum]=1,q.push(sum);
siz=~-siz;
}
write(ans,'\n');
ot();
}
}
int main(){
my::SOLVE();
return 0;
}
/*
*/