ZOJ 3963 Heap Partition 贪心

这篇博客介绍了ZOJ 3963题目,要求使用最少的二叉树构造法,使得父亲节点的权值小于儿子节点且坐标较大的点作为儿子。文章指出,最初的贪心策略——普通线段树寻找比当前点大的前两个点是错误的,因为链状结构更适合。正确解法是采用权值线段树寻找大于当前点的最小点,通过这种方式可以使用集合(set)来解决。文中提供了赛中错误的贪心代码和赛后正确的AC代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ZOJ 3963

题意 给你n个点 叫你用最少的二叉树构造法构造出 父亲节点比儿子节点权值小 然后要坐标大的点做儿子

这道题一开始写的是贪心 普通线段树找比他大的前两个点 然后不断递归下去 实际上是错的 因为链的情况更符合 于是应该写权值线段树找大于他的最小点 这样就可以用set写了

下面是赛中贪心错的代码

#include <iostream>
#include <cstdio>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <cstdlib>
#include <set>
typedef long long ll;
typedef double db;
using namespace std;
const int MAX_N = 100025;
int maxx[MAX_N<<2],cnt,n;
vector<int > ans[MAX_N];
bool flag[MAX_N];
void up(int rt)
{
    maxx[rt] = max(maxx[rt<<1],maxx[rt<<1|1]);
}
void build(int rt,int l,int r)
{
    maxx[rt] = 0;
    if(l==r)
    {
        scanf("%d",&maxx[rt]);
        return ;
    }
    int mid = (l+r)>>1;
    build(rt<<1,l,mid);
    build(rt<<1|1,mid+1,r);
    up(rt);
}
void update(int rt,int l,int r,int x)
{
    if(l==r)
    {
        maxx[rt] = 0;
        return ;
    }
    int mid =(l+r)>>1;
    if(x<=mid) update(rt<<1,l,mid,x);
    else update(rt<<1|1,mid+1,r,x);
    up(rt);
}
int query(int rt,int l,int r,int x,int y)
{
    if(x<=l&&r<=y)
    {
        return maxx[rt];
    }
    int mid = (l+r)>>1,maxx1 = 0,maxx2 = 0;
    if(x<=mid) maxx1 = query(rt<<1,l,mid,x,y);
    if(mid<y) maxx2 = query(rt<<1|1,mid+1,r,x,y);
    return max(maxx1,maxx2);
}
int query_(int rt,int l,int r,int x,int v)
{
    if(l==r)
    {
        return l;
    }
    int mid = (l+r)>>1;
    if(maxx[rt<<1]>=v&&mid>=x) return query_(rt<<1,l,mid,x,v);
    else return query_(rt<<1|1,mid+1,r,x,v);
}
void ljq(int xb,int now)
{
    int maxx = query(1,1,n,xb,n);
    //cout<< "xb" << " = " << (xb) << "now" << " = " << (now) << "maxx" << " = " <<(maxx)<<endl ;
    if(maxx<now) return ;
    int XB = query_(1,1,n,xb,now);
    int NOW = query(1,1,n,XB,XB);
    //cout << "XB = " << XB << " NOW = " <<NOW << endl;
    update(1,1,n,XB);
    maxx = query(1,1,n,xb,n);
    //cout <<" maxx " <<maxx << endl;
    if(maxx<now)
    {
        flag[XB] = true;
        if(NOW!=0) ans[cnt].push_back(XB);
        if(NOW!=0) ljq(XB,NOW);
    }
    else
    {
        int XB_ = query_(1,1,n,xb,now);
        int NOW_ = query(1,1,n,XB_,XB_);
        if(NOW!=0) ans[cnt].push_back(XB);
        if(NOW_!=0) ans[cnt].push_back(XB_);
        flag[XB] = true;
        flag[XB_] = true;
        update(1,1,n,XB_);
        //cout << "XB = " << XB << " XB_ = " << XB_ << endl;
        //cout << "XB_ = " << XB_ << " NOW_ = " <<NOW << endl;
        //cout << "NOW = " << NOW << "  NOW_ = " << NOW_  << endl;
        if(NOW!=0) ljq(XB,NOW);
        if(NOW_!=0) ljq(XB_,NOW_);
    }
    return ;
}
int main(){
    int t;
    scanf("%d",&t);
    while(t--)
    {
        cnt = 0;
        scanf("%d",&n);
        for(int i = 1;i<=n;++i)
        {
            vector<int >vt;
            swap(ans[i],vt);
        }
        memset(flag,false,sizeof(bool)*(n+1));
        build(1,1,n);
        for(int i = 1;i<=n;++i)
        {
            if(flag[i]) continue;
            cnt++;
            int now = query(1,1,n,i,i);
            if(now==0) continue;
            ans[cnt].push_back(i);
            //cout <<"now = " << now << "i = " << i << endl;
            update(1,1,n,i);
            flag[i] = true;
            ljq(i,now);
        }
        printf("%d\n",cnt);
        for(int i = 1;i<=cnt;++i)
        {
            printf("%d ",ans[i].size());
            for(int j = 0;j<ans[i].size();++j)
            {
                j==ans[i].size()-1?printf("%d\n",ans[i][j]):printf("%d ",ans[i][j]);
            }
        }
    }
    return 0;
}

下面是赛后AC代码

/*
    if you can't see the repay
    Why not just work step by step
    rubbish is relaxed
    to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;

#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))

typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod =  (int)1e9+7;

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
const int MAX_N = 100025;
int arr[MAX_N],vis[MAX_N];
struct node
{
    int a,b;
}p;
bool operator <(node a,node b)
{
    if(arr[a.b] == arr[b.b]) return a.b<b.b;
    return arr[a.b]<arr[b.b];
}
vector<int > ans[MAX_N];
set<node > st;
set<node>::iterator xb ;
int main()
{
    //ios::sync_with_stdio(false);
    //freopen("a.txt","r",stdin);
    //freopen("b.txt","w",stdout);
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,cnt = 0;
        scanf("%d",&n);
        memset(vis,0,sizeof(int)*(n+1));
        st.clear();
        for(int i = 1;i<=n;++i)
        {
            vector<int > vt;
            swap(ans[i],vt);
        }
        for(int i = 1;i<=n;++i)
        {
            scanf("%d",&arr[i]);
            p.b = i;
            xb = st.upper_bound(p);
            if(xb==st.begin())
            {
                cnt++;
                p.a = cnt;
                st.insert(p);
                ans[cnt].push_back(i);
            }
            else
            {
                xb--;
                node p = *xb;
                vis[p.b]++;
                if(vis[p.b]==2)
                    st.erase(xb);
                p.b = i;
                st.insert(p);
                ans[p.a].push_back(i);
            }
        }
        printf("%d\n",cnt);
        for(int i = 1;i<=cnt;++i)
        {
            printf("%d ",ans[i].size());
            for(int j = 0;j<ans[i].size();j++)
            {
                j==ans[i].size()-1?printf("%d\n",ans[i][j]):printf("%d ",ans[i][j]);
            }
        }
    }
    //fclose(stdin);
    //fclose(stdout);
    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值