zoukankan      html  css  js  c++  java
  • [NOI 2015]荷马史诗

    [Noi2015]荷马史诗

    题目

    追逐影子的人,自己就是影子。 ——荷马

    Allison 最近迷上了文学。她喜欢在一个慵懒的午后,细细地品上一杯卡布奇诺,静静地阅读她爱不释手的《荷马史诗》。但是由《奥德赛》和《伊利亚特》组成的鸿篇巨制《荷马史诗》实在是太长了,Allison 想通过一种编码方式使得它变得短一些。
    一部《荷马史诗》中有 n 种不同的单词,从 1 到 n 进行编号。其中第 i 种单词出现的总次数为 wi。Allison 想要用 k 进制串 si 来替换第 i 种单词,使得其满足如下要求:
    对于任意的 1≤i,j≤n,i≠j,都有:si 不是 sj 的前缀。
    现在 Allison 想要知道,如何选择 si,才能使替换以后得到的新的《荷马史诗》长度最小。在确保总长度最小的情况下,Allison 还想知道最长的 si 的最短长度是多少?
    一个字符串被称为 k 进制字符串,当且仅当它的每个字符是 0 到 k−1 之间(包括 0 和 k−1)的整数。
    字符串 Str1 被称为字符串 Str2 的前缀,当且仅当:存在 1≤t≤m,使得 Str1=Str2[1..t]。其中,m 是字符串 Str2 的长度,Str2[1..t] 表示 Str2 的前 t 个字符组成的字符串。

    INPUT

    输入文件的第 1 行包含 2 个正整数 n,k,中间用单个空格隔开,表示共有 n 种单词,需要使用 k 进制字符串进行替换。

    接下来 n 行,第 i+1 行包含 1 个非负整数 wi,表示第 i 种单词的出现次数。

    OUTPUT

    输出文件包括 2 行。

    第 1 行输出 1 个整数,为《荷马史诗》经过重新编码以后的最短长度。
    第 2 行输出 1 个整数,为保证最短总长度的情况下,最长字符串 si 的最短长度。

    SAMPLE

    INPUT

    4 2
    1
    1
    2
    2

    OUTPUT

    12

    2

    解题报告

    麻麻,我会手打堆啦

    其实就是哈夫曼树的板子qwq

    瞎xx水过就好了qwq

     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 using namespace std;
     5 #define int long long
     6 inline int read(){
     7     int sum(0);char ch(getchar());
     8     for(;ch<'0'||ch>'9';ch=getchar());
     9     for(;ch>='0'&&ch<='9';sum=sum*10+(ch^48),ch=getchar());
    10     return sum;
    11 }
    12 struct node{
    13     int w,h;
    14     inline bool operator<(const node &x)const{return w^x.w?w<x.w:h<x.h;}
    15 }heap[500005];
    16 int top;
    17 inline void push(node x){
    18     heap[++top]=x;int now(top);
    19     while(1){
    20         if(now==1)break;
    21         if(heap[now]<heap[now>>1])swap(heap[now],heap[now>>1]),now>>=1;
    22         else break;
    23     }
    24 }
    25 inline node get_top(){
    26     node ret(heap[1]);swap(heap[1],heap[top--]);int now(1),son;
    27     while(1){
    28         if((now<<1)>top)break;
    29         if((now<<1|1)<=top&&heap[now<<1|1]<heap[now<<1])son=now<<1|1;
    30         else son=now<<1;
    31         if(heap[now]<heap[son])break;
    32         else swap(heap[now],heap[son]),now=son;
    33     }
    34     return ret;
    35 }
    36 int n,k,sum,mx;
    37 signed main(){
    38     freopen("epic.in","r",stdin);freopen("epic.out","w",stdout);
    39     n=read(),k=read();
    40     for(int i=1;i<=n;++i){
    41         int x(read());
    42         push((node){x,0});
    43     }
    44     if((n-1)%(k-1))
    45         for(int i=0;i<(k-1)-(n-1)%(k-1);++i)
    46             push((node){0,0});
    47     while(top>1){
    48         int hi(0),s(0);
    49         for(int i=0;i<k;++i){
    50             node top(get_top());
    51             s+=top.w;hi=max(hi,top.h);
    52         }
    53         push((node){s,hi+1});
    54         sum+=s;mx=max(mx,hi+1);
    55     }
    56     printf("%lld
    %lld",sum,mx);
    57 }
    View Code
  • 相关阅读:
    css笔记
    CSS基础
    HTML常用基础标签
    React-Native中列表SectionList学习
    React-Native中列表FlatList配合网络请求fetch学习
    React-Native选项卡(react-navigation)之createBottomTabNavigator学习
    React-Native导航条(react-navigation)之createStackNavigator学习
    iOS中touch事件传递(响应者链)
    ios中UIView和CALayer关系
    iOS中KVO使用理解
  • 原文地址:https://www.cnblogs.com/hzoi-mafia/p/7747304.html
Copyright © 2011-2022 走看看