我的代码在oj上超时了,请帮我优化代码,使之能过oj。
oj链接https://codeforces.com/gym/103117/problem/K
#include
#define N 1000010
using namespace std;
int p[N]={0};
int s[N]={0};
int main()
{
int n=0,k=0;
int s1=0,s2=0;
int f=0;
scanf("%d%d",&n,&k);
f=n-1;
int i=0;
for (i=0;ido
{
s1=0;
for (i=0;iif(p[i]+k==p[i+1]) ++s1;
if (s1>s2)
{
for (i=0;is2=s1;
}
if (s2==f) break;
}
while (next_permutation(p,p+n));
for (i=0;i"%d ",s[i]);
printf("%d",s[f]);
return 0;
}
有个思路 比如 n和k输入是7和3
以 1和2 3为3轮 轮流输出 1 4 7 ,2 5 ,3 6这样可以递推了。 算法是以1到k 轮流加上k来输出,直到n个数输出完。细节可以再疏理一下