Minimum Euler Cycle CodeForces

题目大意:构造一个从1到n字典序较小的环,要求所有的v[i]和v[i+1]都必须出现一次,然后输出所构造的序列,l到r这一部分。

题解:构造方法,假设n=5,[1,2,1,3,1,4,1,5]+[2,3,2,4,2,5]+[3,4,3,5]+[4,5]+[1],写的时候不太好些。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=1e5+7;
ll n,l,r;
ll ans[N];
ll f(ll x){
    return -x*x+2*n*x-x;
}
void solve(){
    cin>>n>>l>>r;
    ll t=1;
    while(f(t)<l&&t<n){
        t++;
    }
    if(t==n&&f(n)<l) {
        cout<<1<<endl;
        return ;
    }
    else { 
        ll c=f(t-1);
        ll time=0;
        while(c<l){
            c++;time++;
        }
        ll pos=0;
        ll i=t;
        bool flag=0;
        if(r==n*(n-1)+1) {
            r--;flag=1;
        }
        while(c<=r){
            if(time&1) ans[pos++]=i;
            else {
                ans[pos++]=i+time/2; 
                if(i+time/2==n){
                    i++;time=0; 
                }
            }
            time++;
            c++;
        }
        if(flag) ans[pos++]=1;
        for(ll i=0;i<pos;i++) cout<<ans[i]<<" ";
        cout<<endl;
    }
}
int main(){
    ll t;
    cin>>t;
    while(t--) solve();
    return 0;
}
原文地址:https://www.cnblogs.com/Accepting/p/12834366.html