码迷,mamicode.com
首页 > 其他好文 > 详细

P2672 推销员

时间:2019-07-18 13:34:54      阅读:77      评论:0      收藏:0      [点我收藏+]

标签:string   std   str   http   直接   amp   return   void   更新   

题面

这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\n",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\n",ans); if(now>last){ update(1); last=now; //更新last } _delete(1); //删除now } return 0; }

  

P2672 推销员

标签:string   std   str   http   直接   amp   return   void   更新   

原文地址:https://www.cnblogs.com/hahaha2124652975/p/11206574.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!