心如止水
Je n'ai pas le temps
posts - 400,comments - 130,trackbacks - 0

直接对n*(n-1)/2个数字排序,规模太大!注意到每个数字的取值范围并不大,两两之和最大不过10000,用time[i]记录数字i在和中出现的次数。
以下是我的代码:

#include<iostream>
#include
<algorithm>
#include
<cstdio>
using namespace std;
const int kMaxn = 3001;
const int kMaxs = 10001;

int n,m,r[kMaxn],times[kMaxs];

int main()
{
    
/*
    freopen("data.in","r",stdin);
    freopen("data.out","w",stdout);
    //
*/
    
    
while(cin>>n>>m)
    {
        
for(int i=1;i<=n;i++)
            cin
>>r[i];
        
        fill(times,times
+kMaxs,0);
        
for(int i=2;i<=n;i++)
            
for(int j=1;j<i;j++)
                times[r[i]
+r[j]]++;
        
        
bool first=true;
        
for(int i=kMaxs-1,j=0;j<&& i>=0;i--)
            
while(times[i])
            {
                
if(first) first=false;
                
else cout<<" ";
                cout
<<i;
                j
++;
                
if(j>=m) break;
                times[i]
--;
            }
        cout
<<endl;
    }
    
return 0;
}
posted on 2011-02-28 21:37 lee1r 阅读(233) 评论(0)  编辑 收藏 引用 所属分类: 题目分类:基础/模拟

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理