Codeforces Round #376 (Div. 2)F. Video Cards(前缀和)
题目链接:http://codeforces.com/contest/731/problem/F
题意:有n个数,从里面选出来一个作为第一个,然后剩下的数要满足是这个数的倍数,如果不是,只能减小为他的倍数,否则就舍弃掉,然后把没有舍弃的数的值加起来,求和的最大值;
4
3 2 15 9 就拿这个来说,当拿3当做第一个数时结果是3+15+9=27因为2不是3的倍数;当拿2作为第一个数时,结果是2+2+14+8=26因为3,15,9都不是2的倍数,所以只能减小;同理...求最大的和; 我们可以记录每个数出现的次数,然后记录前缀和,然后枚举每个数作为第一个数,当x作为第一个数时,我们可以枚举x的整数倍,然后每次找到整数j倍和j+1倍中的数出现了几次,这些数都是数x的j倍,然后求出对当前结果的贡献即可
时间复杂度为O(nlogn);
#include<iostream>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<stdio.h>
#include<map>
#include<vector>
#include<queue>
using namespace std;
#define met(a, b) memset(a, b, sizeof(a))
#define mod 1000000007
typedef long long LL;
//////////////////////////////////////////////////////////////
const int INF = 0x3f3f3f3f;
const int N = ;
const double eps = 1e-; int n, a[N];
LL pre[N*]; int main()
{
while(scanf("%d", &n)!=EOF)
{
met(pre, );
for(int i=; i<n; i++)
{
scanf("%d", &a[i]);
pre[a[i]]++;
} sort(a, a+n);
int m = unique(a, a+n) - a; for(int i=; i<=a[m-]*; i++)
pre[i] += pre[i-]; LL ans = ;
for(int i=; i<m; i++)
{
LL ret = ;
for(int j=; a[i]*(j+) <= a[m-]*; j++)
{
LL cnt = pre[(j+)*a[i]-] - pre[j*a[i]-];
ret += a[i]*j * cnt;
}
ans = max(ans, ret);
}
printf("%lld\n", ans);
}
return ;
}
Codeforces Round #376 (Div. 2)F. Video Cards(前缀和)的更多相关文章
- Codeforces Round #376 (Div. 2) F. Video Cards —— 前缀和 & 后缀和
题目链接:http://codeforces.com/contest/731/problem/F F. Video Cards time limit per test 1 second memory ...
- 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 注意到一个事实,如果你要找一段区间中(从小到大的),有多少个数是能整除左端点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张卡片,每个卡片上有一个数字,每个人有一 ...
随机推荐
- 仿windows8 开始菜单 实现HubTileBase 以及仿鲜果联播实现 PulsingTile(脉冲磁贴)
http://blog.csdn.net/wangrenzhu2011/article/details/8750820 (转) 本文章将以如何实现 开始菜单上的tile 为主. 该控件代码经过测试可直 ...
- mysql 截取指定的两个字符串之间的内容(locate,substring)
如需转帖,请写明出处 http://blog.csdn.net/slimboy123/archive/2009/07/30/4394782.aspx 今天我同事在用mysql的时候,需要对一个字符串中 ...
- XmlElement 类
构造函数 名称 说明 XmlElement(String, String, String, XmlDocument) 此 API 支持 产品 基础结构,不能在代码中直接使用. 初始化 XmlEle ...
- 李洪强-C语言1-指针
C语言指针 前导程序 一.基本知识点 Int a=10; Int *p;//定义一个int类型的指针 P=&a;//指针变量p指向了变量a *p=20;//使用指针不通过变量直接修改变量a ...
- iOS中--NSArray调用方法详解 (李洪强)
下面的例子以 NSArray *array = [NSArray arrayWithObjects:@"wendy",@"andy",@"to ...
- hdu-acm stepsHumble Numbers
这是我做的第六道动态规划水题,对动态规划差不多有了一个大致的概念.动态规划有几个关键因素,第一是最优子结构,第二是状态和状态转移方程.整个过程都是以 最优 为中心的.因此在状态转移方程中常涉及到几 ...
- mof提权带回显带清楚命令版本.php
<?php $path="c:/caonimei.txt"; session_start(); if(!empty($_POST['submit'])){ setcookie ...
- [转]支付宝接口程序、文档及解读(ASP.NET)
本文转自:http://www.cnblogs.com/blodfox777/archive/2009/11/03/1595223.html 最近需要为网站加入支付宝的充值接口,而目前关于支付宝接口开 ...
- 获取android设备的IP
public class IPAddressUtils { public static String getLocalIpAddress() { try { String allIP = " ...
- empty()与remove([expr])的区别.转
jquery之empty()与remove()区别 要用到移除指定元素的时候,发现empty()与remove([expr])都可以用来实现.可仔细观察效果的话就可以发现.empty()是只移除了 ...