P7902 「PMOI-0」儒略日题解
P7902题解
本题思路:
-
构造方法:将偶数全部放在奇数之间,并且按顺序输出奇数。
-
这题若无解我们分奇数和偶数的情况讨论。
-
当给定的 n 为奇数时,同一个奇数应相差
\dfrac{n}{2} \times 2 + \dfrac{n}{2} 并且应小于 d 否则,同一个奇数应相差\dfrac{n}{2} \times 2 + \dfrac{n}{2} 并且应小于等于 d 就输出负一。
接下来——上代码。
#include<bits/stdc++.h>
using namespace std;
int read()
{
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-')
f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=(x<<1)+(x<<3)+(ch^48);
ch=getchar();
}
return x*f;
}
int main()
{
cin.tie(0);
cout.tie(0);
int n=read(), d=read();
if((d>n/2*2+n/2&&n%2==1)||(d>=n/2*2+n/2&&n%2==0))
{
cout<<"-1";
return 0;
}
for(int i=1;i<=n;i+=2)
cout<<i<<" ";
for(int i=2;i<=n;i+=2)
cout<<i<<" "<<i<<" ";
for(int i=1;i<=n;i+=2)
cout<<i<<" ";
return 0;
}