zoukankan      html  css  js  c++  java
  • BZOJ 2151: 种树

    BZOJ 2151: 种树

    Description

    A城市有一个巨大的圆形广场,为了绿化环境和净化空气,市政府决定沿圆形广场外圈种一圈树。园林部门得到指令后,初步规划出n个种树的位置,顺时针编号1到n。并且每个位置都有一个美观度Ai,如果在这里种树就可以得到这Ai的美观度。但由于A城市土壤肥力欠佳,两棵树决不能种在相邻的位置(i号位置和i+1号位置叫相邻位置。值得注意的是1号和n号也算相邻位置!)。最终市政府给园林部门提供了m棵树苗并要求全部种上,请你帮忙设计种树方案使得美观度总和最大。如果无法将m棵树苗全部种上,给出无解信息。

    Input

    输入的第一行包含两个正整数n、m。第二行n个整数Ai。

    Output

    输出一个整数,表示最佳植树方案可以得到的美观度。如果无解输出“Error!”,不包含引号。

    Sample Input

    【样例输入1】
    7 3
    1 2 3 4 5 6 7
    【样例输入2】
    7 4
    1 2 3 4 5 6 7

    Sample Output

    【样例输出1】
    15

    【样例输出2】
    Error!
    【数据规模】
    对于全部数据:m<=n;
    -1000<=Ai<=1000
    N的大小对于不同数据有所不同:
    数据编号 N的大小 数据编号 N的大小
    1 30 11 200
    2 35 12 2007
    3 40 13 2008
    4 45 14 2009
    5 50 15 2010
    6 55 16 2011
    7 60 17 2012
    8 65 18 199999
    9 200 19 199999
    10 200 20 200000

    HINT


     

    Sol.

    完全不会。

    考虑用堆和双向链表模拟贪心。

    对每个点记l[i]和r[i]表示他的前驱和后继,v[i]表示它的权值。

    一开始把所有点放进堆里。

    每次取出堆顶,并把答案加上他的权值。

    并在双向链表中删掉他的前驱,后继。(有点像把这三个绑在一起)

    同时把v[l]+v[r]-v[i]放进堆里方便实现退回操作。

    取m次

    #include<cstdio>
    #include<iostream>
    #include<cstdlib>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #include<queue>
    #define maxn 400005
    using namespace std;
    int n,m,a[maxn],l[maxn],r[maxn];
    bool flag[maxn];
    struct node{
        int v,id;
    };
    bool operator <(node a,node b){return a.v<b.v;}
    priority_queue<node>q;
    void del(int x){
        flag[x]=1;
        int lx=l[x],rx=r[x];
        r[lx]=rx;l[rx]=lx;
    }
    int main(){
        cin>>n>>m;
        if(m+m>n){puts("Error!");return 0;}
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
            q.push((node){a[i],i});
            l[i]=i-1;r[i]=i+1;
            if(i==1)l[i]=n;
            if(i==n)r[i]=1;
        }
        int ans=0;
        while(m--){
            node t=q.top();q.pop();
            if(flag[t.id]){m++;continue;}
            ans+=t.v;
            t.v=a[l[t.id]]+a[r[t.id]]-t.v;a[t.id]=t.v;
            del(l[t.id]);del(r[t.id]);
            q.push(t);
        }
        cout<<ans<<endl;
        return 0;
    }
  • 相关阅读:
    启动与指定的文件或协议相关联的默认应用程序
    Windows phone msdn 索引
    34、ShareTarget
    36、UI contrast and settings
    Windows 8下默认管理员登录
    精益创业 Lean Startup
    38、animation
    access2003 基础 1008
    JQuery DOM
    用Javascript实现面向对象编程(封装,抽象,继承,多态)
  • 原文地址:https://www.cnblogs.com/liankewei/p/11791991.html
Copyright © 2011-2022 走看看