Codeforces_731F_(前缀和)
1 second
256 megabytes
standard input
standard output
Little Vlad is fond of popular computer game Bota-2. Recently, the developers announced the new add-on named Bota-3. Of course, Vlad immediately bought only to find out his computer is too old for the new game and needs to be updated.
There are n video cards in the shop, the power of the i-th video card is equal to integer value ai. As Vlad wants to be sure the new game will work he wants to buy not one, but several video cards and unite their powers using the cutting-edge technology. To use this technology one of the cards is chosen as the leading one and other video cards are attached to it as secondary. For this new technology to work it's required that the power of each of the secondary video cards is divisible by the power of the leading video card. In order to achieve that the power of any secondary video card can be reduced to any integer value less or equal than the current power. However, the power of the leading video card should remain unchanged, i.e. it can't be reduced.
Vlad has an infinite amount of money so he can buy any set of video cards. Help him determine which video cards he should buy such that after picking the leading video card and may be reducing some powers of others to make them work together he will get the maximum total value of video power.
The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the number of video cards in the shop.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 200 000) — powers of video cards.
The only line of the output should contain one integer value — the maximum possible total power of video cards working together.
4
3 2 15 9
27
4
8 2 2 7
18
In the first sample, it would be optimal to buy video cards with powers 3, 15 and 9. The video card with power 3should be chosen as the leading one and all other video cards will be compatible with it. Thus, the total power would be 3 + 15 + 9 = 27. If he buys all the video cards and pick the one with the power 2 as the leading, the powers of all other video cards should be reduced by 1, thus the total power would be 2 + 2 + 14 + 8 = 26, that is less than 27. Please note, that it's not allowed to reduce the power of the leading video card, i.e. one can't get the total power3 + 1 + 15 + 9 = 28.
In the second sample, the optimal answer is to buy all video cards and pick the one with the power 2 as the leading. The video card with the power 7 needs it power to be reduced down to 6. The total power would be8 + 2 + 2 + 6 = 18.
题意:一串数字,选择一个作为leading,再找多个Secondary,要求Secondary要能被leading整除,Secondary可以任意减小,leading不能减小,将leading和Secondary加和。问最大的和为多少。
思路:一眼看去没有思路,看了别人的题解。枚举leading,找p*leading—(p+1)*leading之间数字的个数;所以使用前缀和,cnt[num-1]-cnt[num-x-1]为[num-x,num)之间数字的个数。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define LL long long
#define N 200005 int num[N];
int cnt[N];
bool vis[N];
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
memset(vis,,sizeof(vis));
memset(cnt,,sizeof(cnt));
int maxn=;
for(int i=; i<n; i++)
{
scanf("%d",&num[i]);
maxn=max(maxn,num[i]);
cnt[num[i]]++;
}
sort(num,num+n);
for(int i=; i<=maxn; i++)
cnt[i]+=cnt[i-];
LL res=;
for(int i=; i<n; i++)
{
LL tmp=;
if(!vis[num[i]])
{
vis[num[i]]=;
for(int j=num[i]; j<=maxn; j+=num[i])
{
tmp+=(cnt[j-]-cnt[j-num[i]-])*(LL)(j-num[i]);
if(j+num[i]>maxn)
tmp+=(cnt[maxn]-cnt[j-])*(LL)j;
}
res=max(tmp,res);
}
}
if(n==)
printf("%d\n",num[]);
else
printf("%I64d\n",res);
}
return ;
}
Codeforces_731F_(前缀和)的更多相关文章
- HDU1671——前缀树的一点感触
题目http://acm.hdu.edu.cn/showproblem.php?pid=1671 题目本身不难,一棵前缀树OK,但是前两次提交都没有成功. 第一次Memory Limit Exceed ...
- 【手记】注意BinaryWriter写string的小坑——会在string前加上长度前缀length-prefixed
之前以为BinaryWriter写string会严格按构造时指定的编码(不指定则是无BOM的UTF8)写入string的二进制,如下面的代码: //将字符串"a"写入流,再拿到流的 ...
- ASP.NET Core MVC 配置全局路由前缀
前言 大家好,今天给大家介绍一个 ASP.NET Core MVC 的一个新特性,给全局路由添加统一前缀.严格说其实不算是新特性,不过是Core MVC特有的. 应用背景 不知道大家在做 Web Ap ...
- 如何处理CSS3属性前缀
今天闲来无聊,重新来说说CSS3前缀的问题.在春节前和@一丝姐姐说起Sass中有关于gradient的mixins.姐姐说: 为什么还要用mixin呢?为什么不使用Autoprefixer?使用Aut ...
- context:component-scan" 的前缀 "context" 未绑定。
SpElUtilTest.testSpELLiteralExpressiontestSpELLiteralExpression(cn.zr.spring.spel.SpElUtilTest)org.s ...
- 解决adobe air sdk打包 apk后自动在包名前面加上air. (有个点)前缀的问题
早就找到了这个方法,但是一直忙没心思写博客. 默认情况下,所有 AIR Android 应用程序的包名称都带 air 前缀.若不想使用此默认行为,可将计算机环境变量 AIR_NOANDROIDFLAI ...
- adobe air类app 接入腾讯开放平台移动游戏使用带tencent包名前缀的问题
作者:Panda Fang 出处:http://www.cnblogs.com/lonkiss/p/4209159.html 原创文章,转载请注明作者和出处,未经允许不可用于商业营利活动 ------ ...
- [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- [LeetCode] Longest Common Prefix 最长共同前缀
Write a function to find the longest common prefix string amongst an array of strings. 这道题让我们求一系列字符串 ...
随机推荐
- BC #62 div1 02
/* 数位DP题,设dp[n][k][j]为前n位最后一位是k时mod为j的个数.操作都相同,可以使用矩阵加速.本来对于每一位是7*10,可以把它压向一个向量. 加速矩阵为70*70,再加一维计算前缀 ...
- PHP中错误与异常的日志记录用法分析
原文:http://www.jb51.net/article/89548.htm ----------------------------------------------------------- ...
- 3.5 在批处理模式下使用mysql
在前面的章节中,你交互式地使用mysql输入查询而且查看结果.你也能够以批模式执行mysql.为了做到这些.把你想要执行的命令放在一个文件里,然后告诉mysql从文件读取它的输入: shell> ...
- Android Studio怎样改动快捷键
在Android Studio中.如果你认为某个快捷键太复杂,要想改动.怎么改动呢,首先要找到这个快捷键,可是Android Studio搜索快捷键有一个bug,就是你不能依据快捷键来搜索相应的功能点 ...
- ios block 内存管理时使用注意
XMGStudent *stu = [[XMGStudent alloc] init]; __weak XMGStudent *weakStu = stu; stu.block = ^{ NSLog( ...
- LeetCode 811. Subdomain Visit Count (子域名访问计数)
题目标签:HashMap 题目给了我们一组域名,让我们把每一个域名,包括它的子域名,计数. 遍历每一个域名,取得它的计数,然后把它的所有子域名和它自己,存入hashmap,域名作为key,计数作为va ...
- java Map 转 List
public static void testMapVoid () { Map map = new HashMap(); map.put("a", "a1"); ...
- Linux/Android多点触摸协议【转】
本文转载自: 链接点击打开链接 关于Linux多点触摸协议大家可以参考kernel中的文档:https://www.kernel.org/doc/Documentation/input/multi-t ...
- kentico11 教程,
create master page with css list menu Add the navigation menu Add a dynamic web part that will repre ...
- Ubuntu环境下安装nodejs和npm
1.安装python-software-properties sudo apt-get install python-software-properties 2.添加ppa curl -sL http ...