Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

求大佬帮忙看看两份几乎一模一样的程序,一份能过,一份TLE

Posted by mbath at 2018-07-28 21:47:11 on Problem 2100
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <utility>
#include <queue>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
typedef pair<LL, pair<LL, LL> > p;
p ans[1010];
 
int main()
{
    LL n, st, en, sum;
    while (~scanf("%I64d", &n)){
        st = 1, en = 1, sum = 0;
        int k = 0;
        while (1){
            if (sum == n) ans[k++] = p(en-st, pair<LL, LL>(st, en-1));
            if (sum >= n) sum -= st*st, st++;
            else{
                if (en*en <= n) sum += en*en, en++;
                else break;
            }
        }
        printf("%d\n", k);
        for (int i = 0; i < k; i++){
            printf("%I64d ", ans[i].first);
            for (int j = ans[i].second.first; j <= ans[i].second.second; j++) printf("%I64d ", j);
            puts("");
        }
    }
    return 0;
}

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <cstdio>
using namespace std;
typedef long long ll;
typedef pair<ll,ll>T;
T a[1010];
int lo,hi,k;
ll s;
int main()
{
    ll n;
    while(~scanf("%lld",&n)){
        lo=1,hi=1,k=0;
        s=0;
        while(true){
            if(s==n)a[k++]=T(lo,hi);
            if(s>=n)s-=lo*lo,lo++;
            else{
                if(hi*hi<=n)s+=hi*hi,hi++;
                else break;
            }
        }
        printf("%d\n",k);
        for(int i=0;i<k;++i){
            printf("%d ",a[i].second-a[i].first);
            for(int j=a[i].first;j<a[i].second;++j){
                printf("%d ",j);
            }
            printf("\n");
        }
    }
    return 0;
}


第一份可以过,第二份TLE,但是这个复杂度是完全相同的,为什么呢?


Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator