标签:
构造
小于10^5的直接输出1
大于10^5的构造如下的串
1111...12222...233....3.....t,t+1,t+2..
设长度为n,每种字符出现了L[i]次则不同的串有 n*n+n-sigma(L[i])=2*K
大约估计一个n,用贪心求出L
10
4 1 2 3 4
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long int LL;
const int maxn=100100;
LL K,n;
LL L[maxn];
LL bs(LL x)
{
LL low=1,high=100000;
LL ans=1;
while(low<=high)
{
LL mid=(low+high)/2;
if(mid*(mid-1)<=x)
{
ans=mid;
low=mid+1;
}
else
{
high=mid-1;
}
}
return ans;
}
void solve(LL k)
{
if(k<=100000)
{
cout<<k<<endl;
for(int i=1;i<=k;i++)
printf("%d%c",1,(i==k)?'\n':' ');
return ;
}
int nt=0;
n=1000;
while(n*n+n-2*K<=0) n+=100;
LL x=n*n+n-2*K;
while(x)
{
LL y=bs(x);
L[++nt]=y;
x-=y*(y-1);
}
printf("%d\n",(int)n);
int sum=0;
int now=0;
for(int i=1;i<=nt;i++)
{
sum+=L[i];
for(int j=0;j<L[i];j++)
{
now++;
printf("%d%c",i,(now==n)?'\n':' ');
}
}
for(int i=0;i<n-sum;i++)
{
printf("%d%c",++nt,(now==n)?'\n':' ');
}
cout<<endl;
}
int main()
{
while(cin>>K)
{
solve(K);
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
HDOJ 5534 Virtual Participation 构造
标签:
原文地址:http://blog.csdn.net/ck_boss/article/details/47255609