zoukankan      html  css  js  c++  java
  • P2672 推销员

    题面

    这tm线段树的题可以用贪心去写???

    不多bb直接上代码

    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    
    struct home{
        int s,v;
    }a[100010];
    int q[100010];
    int h[100010],qm[100010];
    int n;
    
    bool cmp(home a,home b){
        return a.v>b.v;
    }
    
    int main(){
        scanf("%d",&n);
        for (int i=1;i<=n;i++){
            scanf("%d",&a[i].s);
        }
        for (int i=1;i<=n;i++){
            scanf("%d",&a[i].v);
        }
        sort(a+1,a+1+n,cmp);
        for (int i=n;i>=1;i--){
            h[i]=max(h[i+1],2*a[i].s+a[i].v);
        }
        for (int i=1;i<=n;i++){
            qm[i]=max(qm[i-1],a[i].s);
        }
        for (int i=1;i<=n;i++){
            q[i]=q[i-1]+a[i].v;
        }
        for (int i=1;i<=n;i++){
            printf("%d
    ",max(q[i-1]+h[i],q[i]+2*qm[i]));
        }
    } 
    

      但是!!!有趣的来了

    我还写了一篇线段树的。。。

    大家一起看看??

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<string>
    #include<algorithm>
    using namespace std;
    const int N=100005;
    const int INF=599518803;
    int n; int s[N],a[N]; struct Tree { int l,r,mid,lazy; int id,val; }tree[N<<2]; int last,now;
    int read(){//快读 char c=getchar();int num=0; for(;!isdigit(c);c=getchar()); for(;isdigit(c);c=getchar()) num=num*10+c-'0'; return num; }
    void push_up(int root){ if(tree[root<<1].val>=tree[root<<1|1].val){ tree[root].id=tree[root<<1].id; tree[root].val=tree[root<<1].val; } else{ tree[root].id=tree[root<<1|1].id; tree[root].val=tree[root<<1|1].val; } }
    void build(int root,int l,int r){ tree[root].l=l,tree[root].r=r,tree[root].mid=l+r>>1; if(l==r){ tree[root].id=l; tree[root].val=(s[l]<<1)+a[l]; return; } build(root<<1,l,tree[root].mid); build(root<<1|1,tree[root].mid+1,r); push_up(root); }
    void push_down(int root){ tree[root<<1].lazy+=tree[root].lazy; tree[root<<1|1].lazy+=tree[root].lazy; tree[root<<1].val+=tree[root].lazy; tree[root<<1|1].val+=tree[root].lazy; tree[root].lazy=0; return; }
    void update(int root){ if(tree[root].r<=last) //因为在last之前的一定已经被走过了,已经被置为了a[i],没必要再次置一次,所以直接返回 return; if(tree[root].l>now){ //在当前的人右边的 tree[root].val-=(s[now]-s[last])<<1; //减路程 tree[root].lazy-=(s[now]-s[last])<<1; //lazy数组也要减 return; } if(tree[root].l==tree[root].r){ //在last和now之间的,换成他们的推销值 tree[root].val=a[tree[root].l]; return; } if(tree[root].lazy){ push_down(root);
    } update(root<<1); update(root<<1|1); push_up(root); } void _delete(int root){ //将now删除 if(tree[root].l==tree[root].r){ tree[root].val=-INF; //val置为极小值 return; } if(now<=tree[root].mid) _delete(root<<1); else _delete(root<<1|1); push_up(root); }
    int main(){ n=read(); for(int i=1;i<=n;++i) s[i]=read(); for(int i=1;i<=n;++i) a[i]=read(); build(1,1,n); int ans=0; for(int i=1;i<=n;++i){ ans+=tree[1].val; //取出最大的贡献 now=tree[1].id; printf("%d ",ans); if(now>last){ update(1); last=now; //更新last } _delete(1); //删除now } return 0; }

      

  • 相关阅读:
    【NOIP2007】守望者的逃离
    20200321(ABC)题解 by 马鸿儒 孙晨曦
    20200320(ABC)题解 by 王一帆
    20200319(ABC)题解 by 王一帆 梁延杰 丁智辰
    20200314(ABC)题解 by 董国梁 蒋丽君 章思航
    20200309(ABC)题解 by 梁延杰
    20200307(DEF)题解 by 孙晨曦
    20200306(ABC)题解 by 孙晨曦
    20200305(DEF)题解 by 孙晨曦
    20200303(ABC)题解 by 王锐,董国梁
  • 原文地址:https://www.cnblogs.com/hahaha2124652975/p/11206574.html
Copyright © 2011-2022 走看看