题目链接
题目大意
有n个点,每两个点之间都有(u,v),(v,u)两条有向边,要求每条边都经过且仅经过一次,输出字典序最小的序列。由于数据过大,只要求输出[l,r]之间的序列。
题目思路
自己差不多想到怎么构造了,但是最后还是去看了题解。还是应该再想一想
模拟一下n=5的时候
开始肯定是1-2-1-3-1-4-1-5
注意这个时候不能再从5到1,否则无路可走。那么5出发贪心就是
2-3-2-4-2-5
其实规律已经出来了,剩下就靠模拟了。
1 2 1 3 1 4 1 5 … 1 n
2 3 2 4 2 5 … 2 n
3 4 3 5 3 6 … 3 n
…
1
代码
#include<cstdio>
#include<stack>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#define fi first
#define se second
#define debug printf("I am here\n");
using namespace std;
typedef long long ll;
const int maxn=3e5+5,inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
int t;
ll n,l,r,pre[maxn];
int main(){
scanf("%d",&t);
while(t--){
scanf("%lld%lld%lld",&n,&l,&r);
for(int i=1;i<=n;i++){
pre[i]=pre[i-1]+2*(n-i);
}
pre[n]+=1;
for(ll i=l;i<=r;i++){
ll ans;
int beg=lower_bound(pre+1,pre+1+n,i)-pre;
ll res=i-pre[beg-1];
if(res%2){
ans=beg;
}else{
ans=res/2+beg;
}
if(beg==n){//特判
ans=1;
}
printf("%lld%c",ans,i==r?'\n':' ');
}
}
return 0;
}
参考链接:https://www.cnblogs.com/iss-ue/p/12795188.html