Codeforces Round #376 (Div. 2) F. Video Cards —— 前缀和 & 后缀和
题目链接:http://codeforces.com/contest/731/problem/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.
题意:
给定n个数,在其中选择1个数,使得其他数经过自减后能够被它整除,求经过处理后,这n个数之和所能达到的最大值?
题解:
1.sum[]数组记录a[i]的个数,即为sum[a[i]]。
2.对sum[]数组求后缀和。则sum[i]为:在这n个数之中,大小能够达到i(>=i)的个数。
3.可以形象地把这n个数想象成n条长度分别为a[i]的木棍,然后把这n条木棍左对齐,而sum[len]即为长度>=len的木棍条数。
4.当被选中的数为a[i]时,就依次在长度为a[i]*k的位置上切一刀,得到sum[a[i]*k]条木棍,每条木棍长度为a[i], 那么总和 tmp += sum[a[i]*k]*a[i] (k=1,2,3,4……,a[i]<=a[i]*k<=最长的那条木棍)。然后枚举a[i],取ans = max(tmp);
注意:如果a[i]是a[j]的倍数,那么a[i]就没有枚举的必要了,因为a[i]的情况已经包含于a[j]里面了。即:a[i]情况下的tmp<=a[j]情况下的tmp。所以加个vis[]数组。
学习之处:
1.以后看到数据范围在 200,000 之内的,可以考虑开大小为 200,000 的数组,实现y=x的映射。
2.前缀和的逆向求和,即后缀和sum[val]。其表示为:大小能够达到val的数的个数。
代码如下:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const double eps = 1e-6;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+7;
const int maxn = 2e5+10; int n, N, a[maxn], sum[maxn];
bool vis[maxn]; void init()
{
memset(sum,0,sizeof(sum));
memset(vis,0,sizeof(vis)); scanf("%d",&n);
for(int i = 1; i<=n; i++)
{
scanf("%d",&a[i]);
sum[a[i]]++;
}
sort(a+1, a+1+n);
for(int i = a[n]-1; i>0; i--)
sum[i] += sum[i+1];
} int solve()
{
LL ans = 0;
for(int i = 1; i<=n; i++)
{
int len = a[i];
if(vis[len]) continue; LL tmp = 0;
for(int pos = len; pos<=a[n]; pos += len)
{
tmp += 1LL*sum[pos]*len;
vis[pos] = 1;
}
ans = max(ans, tmp);
}
cout<<ans<<endl;
} int main()
{
init();
solve();
}
Codeforces Round #376 (Div. 2) F. Video Cards —— 前缀和 & 后缀和的更多相关文章
- Codeforces Round #376 (Div. 2) F. Video Cards 数学,前缀和
F. Video Cards time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Codeforces Round #376 (Div. 2)F. Video Cards(前缀和)
题目链接:http://codeforces.com/contest/731/problem/F 题意:有n个数,从里面选出来一个作为第一个,然后剩下的数要满足是这个数的倍数,如果不是,只能减小为他的 ...
- Codeforces Round #376 (Div. 2) F. Video Cards 数学 & 暴力
http://codeforces.com/contest/731/problem/F 注意到一个事实,如果你要找一段区间中(从小到大的),有多少个数是能整除左端点L的,就是[L, R]这样.那么,很 ...
- Codeforces Round #552 (Div. 3) F. Shovels Shop (前缀和预处理+贪心+dp)
题目:http://codeforces.com/contest/1154/problem/F 题意:给你n个商品,然后还有m个特价活动,你买满x件就把你当前的x件中最便宜的y件价格免费,问你买k件花 ...
- Codeforces Round #485 (Div. 2) F. AND Graph
Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...
- Codeforces Round #486 (Div. 3) F. Rain and Umbrellas
Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...
- Codeforces Round #501 (Div. 3) F. Bracket Substring
题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60 ...
- Codeforces Round #499 (Div. 1) F. Tree
Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...
- Codeforces Round #490 (Div. 3) :F. Cards and Joy(组合背包)
题目连接:http://codeforces.com/contest/999/problem/F 解题心得: 题意说的很复杂,就是n个人玩游戏,每个人可以得到k张卡片,每个卡片上有一个数字,每个人有一 ...
随机推荐
- codevs——2181 田忌赛马
2181 田忌赛马 时间限制: 1 s 空间限制: 32000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Description 中国古代的历史故事“田忌赛马”是为大 ...
- Java 浅析,生成OFD文件
摘要:这几天遇到个需要,需要提供用户下载电子证照,最简单的方法实现:word做了一份模板,利用网页工具转成OFD文件,http://www.yozodcs.com/page/example.html用 ...
- MyBatis_SelectKey使用oracle 序列插入主键
mapper 如下: 使用<selectkey>实现 也可以使用oracle的row 级触发器trigger实现: <?xml version="1.0" enc ...
- javascript --- 移除DOM节点
在IE中移除容器类节点,会引起内存泄露,最好是创建一个新的节点,比如div,然后将要删除的节点放入这个div中,再将div的innerHTML清空.其它的直接removeChild就可以了. var ...
- 手机微硬盘读取速度>50MB/s eMMC技术浅析
转载:http://mobile.zol.com.cn/296/2968659_all.html#p2968659 手机微硬盘读取速度>50MB/s 在开始今天的话题之前,请大家随笔者一起时光倒 ...
- win10安装 迅雷
下载安装文件所在目录 ,右键,选择以管理员身份运行,输入程序地址,如D:\download\ThunderMini_dl1.5.3.288.exe,回车,这样就出来了程序安装页面,之后就可以就行正常的 ...
- 关于C语言中二维数组传參————————【Badboy】
直接上代码: #include void Fun(int *a[],int m,int n)// { printf("%d\t",*a);//[0][0] /* int e[2][ ...
- C 标准库 - <float.h>
C 标准库 - <float.h> 简介 C 标准库的 float.h 头文件包含了一组与浮点值相关的依赖于平台的常量.这些常量是由 ANSI C 提出的,这让程序更具有可移植性.在讲解这 ...
- Google的分布式计算模型Map Reduce map函数将输入分割成key/value对
http://www.nowamagic.net/librarys/veda/detail/1768 上一篇 大规模分布式数据处理平台Hadoop的介绍 中提到了Google的分布式计算模型Map R ...
- 可空类型Nullable
Nullable类型: 值类型变量默认为0,不可空,为了使它可空,出现了Nullable类型,类型前面加? 变为引用类型 值类型是没有null值的,比如int,DateTime,它们都有默认值.举个 ...