码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces_731_F

时间:2016-10-17 07:15:50      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:

http://codeforces.com/problemset/problem/731/F

 

其实是暴力枚举,但是有些小技巧,直接保存每个数的数量。

枚举每个起点时,然后依次加上起点大小的分段的数量的值,用前缀和效率很高,并且能巧妙跳过重复元素。

 

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;

int cnt[200005] = {0},n;

int main()
{
    scanf("%d",&n);
    int temp,maxx = 0;
    while(n--)
    {
        scanf("%d",&temp);
        maxx = max(maxx,temp);
        cnt[temp]++;
    }
    for(int i = 2;i <= maxx;i++)    cnt[i] += cnt[i-1];
    long long ans = 0,tempmax;
    for(int i = 1;i <= maxx;i++)
    {
        if(cnt[i] != cnt[i-1])
        {
            tempmax = 0;
            for(int j = i;j <= maxx;j += i)    tempmax += (long long)j*(cnt[min(j+i-1,maxx)]-cnt[j-1]);
            ans = max(tempmax,ans);
        }
        
    }
    printf("%I64d\n",ans);
    return 0;
}

 

Codeforces_731_F

标签:

原文地址:http://www.cnblogs.com/zhurb/p/5968298.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!