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

bzoj 1150&2151&2288(双向链表+堆)(贪心)

时间:2017-08-24 01:14:08      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:algo   最小   amp   选择   eve   one   std   err   连接   

  经典模型:在n个点中选k个点,要求两两不相邻,且总权值最大/最小。

  做法:用双向链表串起来,把所有点丢进堆里,选择一个点的时候把它左右两个点从双向链表和堆中去除,然后把这个点的权值加进ans,出堆后改为左右两边的权值-当前权值重新入堆,重复k次,ans即为答案

  原理:左右两边的权值-当前权值相当于这个点不选,改为选左右两边的点,并且多选了一个点,重复k次后必然取到k个点

  三道同类型题

  bzoj1150:显然一段网线肯定接在相邻的两个点,于是把相邻的点的距离求出来,问题就变成经典模型了

技术分享
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cmath>
#include<map>
#define ll long long 
using namespace std;
const int maxn=500010,inf=1e9;
struct poi{int pos,dis;};
priority_queue<poi>q;
bool operator<(poi a,poi b){return a.dis>b.dis;}
int n,k,a[maxn],dis[maxn],pre[maxn],next[maxn];
ll ans;
bool v[maxn];
void read(int &k)
{
    int f=1;k=0;char c=getchar();
    while(c<0||c>9)c==-&&(f=-1),c=getchar();
    while(c<=9&&c>=0)k=k*10+c-0,c=getchar();
    k*=f;
}
inline void del(int x)
{
    int l=pre[x],r=next[x];
    pre[x]=next[x]=0;
    next[l]=r;pre[r]=l;
}
int main()
{
    read(n);read(k);
    for(int i=1;i<=n;i++)read(a[i]);
    for(int i=1;i<n;i++)dis[i]=a[i+1]-a[i],q.push((poi){i,dis[i]});dis[0]=dis[n]=inf;
    for(int i=1;i<n;i++)pre[i]=i-1,next[i]=i+1;
    for(int i=1;i<=k;i++)
    {
        poi t=q.top();
        while(v[t.pos])q.pop(),t=q.top();
        ans+=t.dis;v[pre[t.pos]]=v[next[t.pos]]=1;
        dis[t.pos]=dis[pre[t.pos]]+dis[next[t.pos]]-dis[t.pos];
        q.pop();q.push((poi){t.pos,dis[t.pos]});
        del(pre[t.pos]);del(next[t.pos]);
    }
    printf("%lld\n",ans);
    return 0;
}
View Code

  bzoj2151:双向链表首尾串起来,问题就变成经典模型了

技术分享
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cmath>
#include<map>
#define ll long long 
using namespace std;
const int maxn=500010,inf=1e9;
struct poi{int pos,dis;};
priority_queue<poi>q;
bool operator<(poi a,poi b){return a.dis<b.dis;}
int n,k,a[maxn],dis[maxn],pre[maxn],next[maxn];
ll ans;
bool v[maxn];
void read(int &k)
{
    int f=1;k=0;char c=getchar();
    while(c<0||c>9)c==-&&(f=-1),c=getchar();
    while(c<=9&&c>=0)k=k*10+c-0,c=getchar();
    k*=f;
}
inline void del(int x)
{
    int l=pre[x],r=next[x];
    pre[x]=next[x]=0;
    next[l]=r;pre[r]=l;
}
int main()
{
    read(n);read(k);
    if(k>n>>1)return puts("Error!"),0;
    for(int i=1;i<=n;i++)read(dis[i]),q.push((poi){i,dis[i]});
    for(int i=1;i<=n;i++)pre[i]=i-1,next[i]=i+1;pre[1]=n;next[n]=1;
    for(int i=1;i<=k;i++)
    {
        poi t=q.top();
        while(v[t.pos])q.pop(),t=q.top();
        ans+=t.dis;v[pre[t.pos]]=v[next[t.pos]]=1;
        dis[t.pos]=dis[pre[t.pos]]+dis[next[t.pos]]-dis[t.pos];
        q.pop();q.push((poi){t.pos,dis[t.pos]});
        del(pre[t.pos]);del(next[t.pos]);
    }
    printf("%lld\n",ans);
    return 0;
}
View Code

  bzoj2288:

  这题比较复杂。首先可以发现正数连续的一段和负数连续的一段要取肯定是都得同时取的,那么就可以把连续的同正负的数缩成一个了。然后如果正数个数不大于k,直接取所有正数即可。如果正数个数大于k,我们考虑舍弃一些正数,或者选择一些负数。只要将所有数取绝对值,问题就变成经典模型了。为什么呢?

  我们选择的是总权值最小的数,如果他是个正数,相当于舍弃,如果是负数,相当于把左右两端的正数连成一段。显然不可能同时选两个相邻的数,否则一定是正负一起选,那负数完全可以不选,因为既然你要丢弃这个正数,为何还要选择一个负数呢,选择负数的作用只有连接左右两端的正数。

  因此,这个题也能转化为经典模型,就可以写了。

技术分享
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cmath>
#include<map>
#define ll long long 
using namespace std;
const int maxn=500010,inf=1e9;
struct poi{int pos,dis;};
priority_queue<poi>q;
bool operator<(poi a,poi b){return a.dis>b.dis;}
int n,m,flag,N,now,l,r,ans;
int a[maxn],s[maxn],pre[maxn],next[maxn];
bool v[maxn];
void read(int &k)
{
    int f=1;k=0;char c=getchar();
    while(c<0||c>9)c==-&&(f=-1),c=getchar();
    while(c<=9&&c>=0)k=k*10+c-0,c=getchar();
    k*=f;
}
inline void del(int x)
{
    int l=pre[x],r=next[x];
    pre[x]=next[x]=0;
    pre[r]=l;next[l]=r;
}
int main()
{
    read(n);read(m);
    for(int i=1;i<=n;i++)read(a[i]);
    int l=1,r=n;
    while(a[l]<=0&&l<=n)l++;
    while(a[r]<=0&&r)r--;
    for(int i=l;i<=r;)
    {
        if(a[i]>=0)flag=1;else flag=-1;
        for(now=0;flag*a[i]>=0&&i<=r;i++)now+=a[i];
        s[++N]=now;
    }
    for(int i=1;i<=N;i+=2)ans+=s[i];
    if(m>=(N+1)>>1)return printf("%d\n",ans),0;
    for(int i=1;i<=N;i++)q.push((poi){i,s[i]=(s[i]>0?s[i]:-s[i])});
    for(int i=1;i<=N;i++)pre[i]=i-1,next[i]=i+1;s[0]=inf;s[N+1]=inf;
    int up=((N+1)>>1)-m;
    for(int i=1;i<=up;i++)
    {
        poi t=q.top();
        while(v[t.pos])q.pop(),t=q.top();q.pop();
        s[t.pos]=s[pre[t.pos]]+s[next[t.pos]]-s[t.pos];
        ans-=t.dis;q.push((poi){t.pos,s[t.pos]});
        v[pre[t.pos]]=v[next[t.pos]]=1;
        del(pre[t.pos]);del(next[t.pos]);
    }
    printf("%d\n",ans);
    return 0;
}
View Code

 

bzoj 1150&2151&2288(双向链表+堆)(贪心)

标签:algo   最小   amp   选择   eve   one   std   err   连接   

原文地址:http://www.cnblogs.com/Sakits/p/7420904.html

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