跳到正文

题解

難道這個題真的不用 dp???

ETO_leader约 5 分钟
原文
文章信息与版本

文章信息

发布
2026年9月18日
收录
2026年9月18日
更新
2026年11月10日
篇幅
约 5 分钟
题目
CF2045L

版本

  1. v1当前

考虑构造一个答案比较大的方案,发现可以类似下列的给构造:

  • 连接 22 和其他每个点,然后其他点以 11 为起点链接出一条链。

这样总数是 n(n+1)−4n(n+1)-4。

容易发现上述构造可以轻松拼接,任取一个链上的点作为起点走下一个就可以了。

考虑小范围做一下 2×1052 \times 10^5 以内的背包 dp,然后用一次构造缩到范围里面。

发现无法构造奇数,不难发现给 22 连一个孤立点出去就有奇数了,两种构造混合起来 dp 和跳第一步就可以轻松通过。

code
C++
#include<bits/stdc++.h>
#define rep(i,a,b) for(auto i=a;i<b;++i)
using namespace std;
constexpr auto constructn_cnt(int n){
    return n*(n+1)-4;
}
constexpr auto constructn2_cnt(int n){
    return constructn_cnt(n-1)+(n-1);
}
constexpr auto append(int s,int n,vector<pair<int,int>>&cur){
    if(n<2) return s+n-1;
    auto id=s;
    const auto p=++id;
    cur.emplace_back(s,p);
    rep(i,0,n-2) cur.emplace_back(++id,p);
    if(s+2<id+1) cur.emplace_back(s,s+2);
    rep(i,0,n-3) cur.emplace_back(s+i+2,s+i+3);
    return id;
}
constexpr auto appendex(int s,int n,vector<pair<int,int>>&cur){
    const auto z=s+1;
    const auto p=append(s,n-1,cur);
    auto r=p+1;
    if(z!=r) cur.emplace_back(z,r);
    return r;
}
static constexpr auto pchk=(int)(2e5);
int main(){
    ios::sync_with_stdio(false),cin.tie(nullptr);
    int n;cin>>n;
    bitset<pchk> st(0);
    auto c=-1;
    st[0]=true;
    for(auto i=2;constructn_cnt(i)<pchk;++i){
        c=i;
        const auto cc=constructn_cnt(i);
        const auto cx=constructn2_cnt(i);
        rep(i,cc,pchk) st[i]=st[i]|st[i-cc];
        if(i>2) rep(i,cx,pchk) st[i]=st[i]|st[i-cx];
    }
    auto l=0,r=32768,ans=-1;
    while(l-1<r){
        const auto mid=(l+r)/2;
        constructn_cnt(mid)>n?(r=mid-1):((l=mid+1),(ans=mid));
    }
    auto p=ans;
    auto ty=false;
    while(p>-1){
        const auto cp=n-constructn_cnt(p);
        if(cp>pchk){
            p=-1;
            break;
        }
        if(st[cp]) break;
        --p;
    }
    r=ans;
    while(constructn2_cnt(r+1)<n+1) ++r;
    while(r>2){
        const auto cr=n-constructn2_cnt(r);
        if(cr>pchk){
            r=-1;
            break;
        }
        if(st[cr]) break;
        --r;
    }
    if(p<2&&r>2) p=r,ty=true;
    if(p<2){
        cout<<-1<<' '<<-1<<'\n';
    }else{
        auto s=1;
        vector<pair<int,int>> ans;
        s=(ty?appendex(s,p,ans):append(s,p,ans));
        auto z=n-(ty?constructn2_cnt(p):constructn_cnt(p));
        for(auto w=c;w>1;--w){
            while(z-constructn_cnt(w)>-1){
                if(st[z-constructn_cnt(w)]) s=append(s,w,ans),z-=constructn_cnt(w);
                else break;
            }
            if(w>2) while(z-constructn2_cnt(w)>-1){
                if(st[z-constructn2_cnt(w)]) s=appendex(s,w,ans),z-=constructn2_cnt(w);
                else break;
            }
        }
        cout<<s<<' '<<ans.size()<<'\n';
        for(auto&[x,y]:ans) cout<<x<<' '<<y<<'\n';
    }
    return 0;
}
原文