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. 这道题让我们求一系列字符串 ...
随机推荐
- Mac OS忘记password怎么办?无光盘破解Mac OS的管理员password
mac系统10.8.5升级10.10 出现故障,重新启动系统无法登陆,降级系统10.9 后更新10.9.3 finder无法打开,root权限没了,又再一次覆盖安装10.9(为了保护原系统文件.所以覆 ...
- 你不知道的JavaScript--Item34 大白话解说Promise
去年6月份. ES2015正式公布(也就是ES6.ES6是它的乳名),当中Promise被列为正式规范.作为ES6中最重要的特性之中的一个,我们有必要掌握并理解透彻.本文将由浅到深,解说Promise ...
- FloatingActionMenu 向上弹出菜单
本人在github上找到了一个FloatingActionsMenu,精简了其效果(原效果有上下左右四个方向)仅仅保留向上的效果,并做了一定的优化. github上的源代码:地址 ,精简后的源代码地址 ...
- xcode执行后没看到输出的解决的方法
今天刚在虚拟机中的mac苹果里安装xcode.然后開始看Objective-C. 安装完后由于有默认的代码输出Hello world! ,所以就尝试执行了一发: 可惜没看到输出在哪,尼玛.还以为刚開始 ...
- 【JAVA】merge two array by order
public class MergeSort { static void show(int a[]) { int i; for (i = 0; i < a.length; i++) { Syst ...
- TCP传输工作原理
引言 在TCP/IP体系结构中,IP协议只管将数据包尽力传送到目的主机,无论数据传输正确与否,它都不做验证,不发确认,也不保证数据包的顺序,因而不具有可靠性.这一问题要由传输层TCP协议来解决,TCP ...
- HDU - 3631 Shortest Path(Floyd最短路)
Shortest Path Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u SubmitStat ...
- java事件处理机制(自定义事件)
java中的事件机制的参与者有3种角色: 1.event object:事件状态对象,用于listener的相应的方法之中,作为参数,一般存在与listerner的方法之中 2.event sourc ...
- la3713
2-sat...求解2-sat方案直接每个变量枚举就行了,lrj的代码很靠谱... #include<bits/stdc++.h> using namespace std; ; struc ...
- yum -y --downloadonly --downloaddir=/root/ruiy update
依赖关系解决 ============================================================================================= ...