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

Re:不知道为什么排了序不行,暴搜反而可以...

Posted by o00o at 2007-11-02 19:12:25 on Problem 2231
In Reply To:不知道为什么排了序不行,暴搜反而可以... Posted by:yuanyirui at 2007-03-30 00:10:43
> #include <stdio.h>
> #include <iostream>
> using namespace std;
> int main()
> {
>   //  freopen("in.txt","r",stdin);
>   //  freopen("out.txt","w",stdout);
>     int a[10001],i,j,n;
>     long long ans;
>     while(scanf("%d",&n)!=EOF){
>         for(i=0;i<n;i++) scanf("%d",&a[i]);
>         ans=0;
>         sort(a,a+n);
>         for(i=0;i<n-1;i++) ans += (a[i+1]-a[i])*(i+1)*(n-i-1);
>         cout<<ans*2<<endl;
>     }
>     return 1;
> }
> 不行
> #include <stdio.h>
> #include <iostream.h>
> int main()
> {
>    // freopen("in.txt","r",stdin);
>    // freopen("out.txt","w",stdout);
>     int a[10001],i,j,n;
>     long long ans;
>     while(scanf("%d",&n)!=EOF){
>         for(i=0;i<n;i++) scanf("%d",&a[i]);
>         ans=0;
>         for(i=0;i<n;i++){
>             for(j=i+1;j<n;j++){
>                 ans+=abs(a[i]-a[j]);
>             }
>         }
>         cout<<ans*2<<endl;
>     }
>     return 1;
> }
> 反而可以过

这一句int可能溢出ans += (a[i+1]-a[i])*(i+1)*(n-i-1);
改为ans += (long long )(a[i+1]-a[i])*(i+1)*(n-i-1);即可

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