Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
O(n)算法,居然用了我797ms...#include<iostream> using namespace std; #include<cstdlib> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> const int maxN=500002; int result[maxN]; bool used[maxN]; int main() { int n; long m; int i,j,k; while(scanf("%d%ld",&n,&m)&&!(-1==n&&-1==m)) { memset(used,false,sizeof(bool)*(n+1)); for(i=0;i<n;++i) result[i]=i+1; int restN=ceil((-3.0+sqrt(9.0+8.0*m))/2.0); int tempRestN=restN+1; int need=m-(tempRestN%2==0?tempRestN/2*(tempRestN-1):(tempRestN-1)/2*tempRestN); int nbase=n-restN-1; int preIndex=n-2-restN; int value=need+nbase; result[preIndex]=value; for(i=0;i<=preIndex;++i) { used[result[i]-1]=true; } int start=n-1; for(i=preIndex+1;i<n;++i) { while(used[start]) --start; result[i]=start+1; used[start]=true; } for(i=0;i<n-1;++i) { printf("%d",result[i]); printf(" "); } printf("%d\n",result[n-1]); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator