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

第一次超时 优化后64MS。。。

Posted by Ink213 at 2013-02-22 14:55:58 on Problem 3663
#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
    int n,s;
    int i,j;
    int cow[20005];
    int n_sum;
    scanf("%d%d",&n,&s);
    for(i=0; i<n; i++)
        scanf("%d",&cow[i]);
    sort(cow,cow+n);
    j=0;
    n_sum=0;
    int k=0;
    for(i=n-1; i>=0; i--)
    {
        if(i>j)
        {
            n_sum+=k;
            for(; j<i; j++)
            {
                if(cow[i]+cow[j]<=s)
                {
                    k++;
                    n_sum++;
                }
                else
                    break;
            }
        }
        else
        {
            n_sum+=i*(i+1)/2;
            break;
        }
    }
    printf("%d\n",n_sum);
    return 0;
}

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