Codeforces #Round 376 F 题解
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 3 should 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 power 3 + 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 be 8 + 2 + 2 + 6 = 18.
————————————分割线————————————
分析:
枚举每个数字,让它成为主视频卡ak,然后枚举倍数 j ,将j介于 ( j - 1 ) * ak<= x < j * ak , 全部减少到 ( j - 1 ) * ak,为了快速的找到这些数,可以使用前缀和优化思想。
例如:
a1为现在的主视频卡,让介于[ a1 * 1 , a1 * 2 ) 减少到a1 , 让介于[ a1 * 2 , a1 * 3 ) 减少到2 * a1 , 依此类推... ...
#include "bits/stdc++.h" using namespace std ;
const int maxN = 3e5 + 1e3 ;
const int INF = ;
typedef long long QAQ ; QAQ Sum[ maxN ] , buc[ maxN ] ; inline int INPUT ( ) {
int x = , f = ; char ch = getchar ( ) ;
while ( ch < '' || '' < ch ) { if ( ch == '-' ) f = - ; ch = getchar ( ) ; }
while ( '' <= ch && ch <= '' ) { x = ( x << ) + ( x << ) + ch - '' ; ch = getchar ( ) ; }
return x * f ;
} void Pre_Init ( int n ) {
for ( int i= ; i<=_max ; ++i )
Sum[ i ] = Sum[ i - ] + buc[ i ] ;
} inline QAQ gmax ( QAQ x , QAQ y ) { return x > y ? x : y ; }
inline QAQ gmin ( QAQ x , QAQ y ) { return x > y ? y : x ; } int main ( ) {
QAQ Ans = -INF , _max = -INF ;
int N = INPUT ( ) ;
for ( int i= ; i<=N ; ++i ) {
int tmp = INPUT ( ) ;
++ buc [ tmp ] ;
_max = gmax ( _max , tmp ) ;
} Pre_Init ( _max ) ; for ( int i= ; i<=_max ; ++i ) {
if ( !buc[ i ] ) continue ;
QAQ rest = ;
for ( int j=i ; j<=_max ; j+=i ) {
rest += ( Sum[ gmin ( i + j - , _max ) ] - Sum [ j - ] ) * j ;
}
Ans = gmax ( Ans , rest ) ;
}
cout << Ans << endl ;
return ;
}
2016-10-18 10:45:39
(完)
Codeforces #Round 376 F 题解的更多相关文章
- Codeforces #Round 376 部分题解
A: 题目传送门:http://codeforces.com/problemset/problem/731/A 直接根据题意模拟即可 #include "bits/stdc++.h" ...
- Codeforces Round #543 Div1题解(并不全)
Codeforces Round #543 Div1题解 Codeforces A. Diana and Liana 给定一个长度为\(m\)的序列,你可以从中删去不超过\(m-n*k\)个元素,剩下 ...
- Codeforces Round #545 Div1 题解
Codeforces Round #545 Div1 题解 来写题解啦QwQ 本来想上红的,结果没做出D.... A. Skyscrapers CF1137A 题意 给定一个\(n*m\)的网格,每个 ...
- Codeforces Round #539 Div1 题解
Codeforces Round #539 Div1 题解 听说这场很适合上分QwQ 然而太晚了QaQ A. Sasha and a Bit of Relax 翻译 有一个长度为\(n\)的数组,问有 ...
- Educational Codeforces Round 64 部分题解
Educational Codeforces Round 64 部分题解 不更了不更了 CF1156D 0-1-Tree 有一棵树,边权都是0或1.定义点对\(x,y(x\neq y)\)合法当且仅当 ...
- Educational Codeforces Round 64部分题解
Educational Codeforces Round 64部分题解 A 题目大意:给定三角形(高等于低的等腰),正方形,圆,在满足其高,边长,半径最大(保证在上一个图形的内部)的前提下. 判断交点 ...
- Educational Codeforces Round 40 F. Runner's Problem
Educational Codeforces Round 40 F. Runner's Problem 题意: 给一个$ 3 * m \(的矩阵,问从\)(2,1)$ 出发 走到 \((2,m)\) ...
- Codeforces Round div2 #541 题解
codeforces Round #541 abstract: I构造题可能代码简单证明很难 II拓扑排序 III并查集 启发式排序,带链表 IV dp 处理字符串递推问题 V 数据结构巧用:于二叉树 ...
- [Codeforces Round #461 (Div2)] 题解
[比赛链接] http://codeforces.com/contest/922 [题解] Problem A. Cloning Toys [算法] 当y = 0 , 不可以 当 ...
随机推荐
- SQLServer事务
指访问并可能更新数据库中各种数据项的一个程序执行单元(unit)由多个sql语句组成,必须作为一个整体执行这些sql语句作为一个整体一起向系统提交,要么都执行.要么都不执行 语法步骤:开始事务:BEG ...
- Eclipse中怎么设置Add cast to Clazz 快捷键
方法如下:window => preferences => 搜索keys => 然后点击进去,搜索add cast => 看到如图所示Quick Fix , 点击进去 => ...
- I-number
以下是真坑爹题目: 此题必须输出前导零,否则永远a不了 I-number Time Limit: 5000MS Memory limit: 65536K 题目描述 The I-number of x ...
- JAVA和PYTHON同时实现AES的加密解密操作---且生成的BASE62编码一致
终于有机会生产JAVA的东东了. 有点兴奋. 花了一天搞完.. java(关键key及算法有缩减): package com.security; import javax.crypto.Cipher; ...
- 算法系列:CSAPP 推荐
转载自:https://book.douban.com/review/6093947/ 如果你觉得这本书过于厚重担心看不下来的话,不妨跟着coursera的Hardware/Software Inte ...
- Implementing Navigation with UINavigationController
Implementing Navigation with UINavigationController Problem You would like to allow your users to mo ...
- Linux文本比较-diff&awk
最近为了完成工作,需要将两个文件A.old和A进行比较,然后将A中新增加的部分保存到A中,其他部分删除.经过查找相关资料,发现有两种比较好的方法. 1. 使用diff命令 diff old.file ...
- Comet:基于 HTTP 长连接的“服务器推”技术解析
原文链接:http://www.cnblogs.com/deepleo/p/Comet.html 一.背景介绍 传统web请求,是显式的向服务器发送http Request,拿到Response后显示 ...
- sqlserver 作业调度(作业常用的几个步骤)
--[作业常用的几个步骤] EXEC msdb.dbo.sp_delete_job EXEC msdb.dbo.sp_add_job EXEC msdb.dbo.sp_add_jobstep EXEC ...
- loadrunner资源过滤器
通过该功能可以实现排除某个资源,很实用 Download Filters功能 帮助在回放脚本的时候对某些特定的访问进行屏蔽,解决页面读取中跨服务器带来数据影响的问题. 过滤规则中有3中策略,即URL. ...