VK Cup 2016 - Round 1 (Div. 2 Edition) E. Bear and Contribution 单调队列
E. Bear and Contribution
题目连接:
http://www.codeforces.com/contest/658/problem/E
Description
Codeforces is a wonderful platform and one its feature shows how much someone contributes to the community. Every registered user has contribution — an integer number, not necessarily positive. There are n registered users and the i-th of them has contribution ti.
Limak is a little polar bear and he's new into competitive programming. He doesn't even have an account in Codeforces but he is able to upvote existing blogs and comments. We assume that every registered user has infinitely many blogs and comments.
Limak can spend b minutes to read one blog and upvote it. Author's contribution will be increased by 5.
Limak can spend c minutes to read one comment and upvote it. Author's contribution will be increased by 1.
Note that it's possible that Limak reads blogs faster than comments.
Limak likes ties. He thinks it would be awesome to see a tie between at least k registered users. To make it happen he is going to spend some time on reading and upvoting. After that, there should exist an integer value x that at least k registered users have contribution exactly x.
How much time does Limak need to achieve his goal?
Input
The first line contains four integers n, k, b and c (2 ≤ k ≤ n ≤ 200 000, 1 ≤ b, c ≤ 1000) — the number of registered users, the required minimum number of users with the same contribution, time needed to read and upvote a blog, and time needed to read and upvote a comment, respectively.
The second line contains n integers t1, t2, ..., tn (|ti| ≤ 109) where ti denotes contribution of the i-th registered user.
Output
Print the minimum number of minutes Limak will spend to get a tie between at least k registered users.
Sample Input
4 3 100 30
12 2 6 1
Sample Output
220
Hint
题意
有n个数,你想使得其中至少k个数相同,你使得一个数加5需要花费b,使得一个数加1需要花费c
问你最少花费多少才能满足题意。
题解:
首先对于整体来说,不一定是选择的是一个连续的区间的数,因为有加5这个操作
但是我现在分开考虑,按照%5的不同的值分开考虑,每一个模数里面所选择的数一定是连续的一段
知道这个之后,我们就暴力的去维护五个单调队列就好了。
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5+7;
int n,k;
long long b,c,a[maxn];
vector<long long>v[5];
queue<long long>Q[5];
long long cal(long long x,long long y)
{
return (y-x)/5*b+(y-x)%5*c;
}
int main()
{
scanf("%d%d%lld%lld",&n,&k,&b,&c);b=min(b,5*c);
for(int i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
a[i]=a[i]+1e9+1;
}
sort(a+1,a+1+n);
for(int i=1;i<=n;i++)
for(int j=0;j<5;j++)
v[(a[i]+j)%5].push_back(a[i]+j);
for(int i=0;i<5;i++)
{
sort(v[i].begin(),v[i].end());
v[i].erase(unique(v[i].begin(),v[i].end()),v[i].end());
}
long long ans = 1e18;
for(int p=0;p<5;p++)
{
for(int i=0;i<5;i++)while(!Q[i].empty())Q[i].pop();
long long tmp = 0;
int num = 0;
for(int i=0;i<v[p].size();i++)
{
if(i)tmp=tmp+min(num,k)*(v[p][i]-v[p][i-1])/5*b;
while(num<n&&a[num+1]<=v[p][i])
{
num++;
Q[a[num]%5].push(a[num]);
tmp+=cal(a[num],v[p][i]);
if(num>k)
{
long long s = 0;
for(int j=0;j<5;j++)
{
if(Q[j].size())
{
int now = Q[j].front();
s=max(s,cal(now,v[p][i]));
}
}
for(int j=0;j<5;j++)
{
if(Q[j].size())
{
int now = Q[j].front();
if(cal(now,v[p][i])==s)
{
Q[j].pop();
tmp-=s;
break;
}
}
}
}
if(num>=k)ans=min(ans,tmp);
}
}
}
cout<<ans<<endl;
}
VK Cup 2016 - Round 1 (Div. 2 Edition) E. Bear and Contribution 单调队列的更多相关文章
- VK Cup 2016 - Round 1 (Div. 2 Edition) C. Bear and Forgotten Tree 3
C. Bear and Forgotten Tree 3 time limit per test 2 seconds memory limit per test 256 megabytes input ...
- Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) D Bear and Two Paths
题目链接: http://codeforces.com/contest/673/problem/D 题意: 给四个不同点a,b,c,d,求是否能构造出两条哈密顿通路,一条a到b,一条c到d. 题解: ...
- Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) C - Bear and Colors
题目链接: http://codeforces.com/contest/673/problem/C 题解: 枚举所有的区间,维护一下每种颜色出现的次数,记录一下出现最多且最小的就可以了. 暴力n*n. ...
- Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) D. Bear and Two Paths 构造
D. Bear and Two Paths 题目连接: http://www.codeforces.com/contest/673/problem/D Description Bearland has ...
- Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) C. Bear and Colors 暴力
C. Bear and Colors 题目连接: http://www.codeforces.com/contest/673/problem/C Description Bear Limak has ...
- Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) A. Bear and Game 水题
A. Bear and Game 题目连接: http://www.codeforces.com/contest/673/problem/A Description Bear Limak likes ...
- VK Cup 2016 - Round 1 (Div. 2 Edition) D. Bear and Polynomials
D. Bear and Polynomials 题目连接: http://www.codeforces.com/contest/658/problem/D Description Limak is a ...
- VK Cup 2016 - Round 1 (Div. 2 Edition) C. Bear and Forgotten Tree 3 构造
C. Bear and Forgotten Tree 3 题目连接: http://www.codeforces.com/contest/658/problem/C Description A tre ...
- VK Cup 2016 - Round 1 (Div. 2 Edition) B. Bear and Displayed Friends 树状数组
B. Bear and Displayed Friends 题目连接: http://www.codeforces.com/contest/658/problem/B Description Lima ...
随机推荐
- redhat5.5 x64 安装oracle 11g
http://www.cnblogs.com/jamesf/p/4769086.html http://blog.csdn.net/yakson/article/details/9012129
- 某团队线下赛AWD writeup&Beescms_V4.0代码审计
还是跟上篇一样.拿别人比赛的来玩一下. 0x01 预留后门 连接方式: 0x02 后台登录口SQL注入 admin/login.php 在func.php当中找到定义的check_login函数 很 ...
- Ubuntu命令设置ip网关dns
本文系转载,介绍Ubuntu如何设置IP和网络来连接网络 如果是在虚拟机中使用Ubuntu,那么设置之前请先参照我的上一遍文章虚拟机Net方式设置连接外网中的网络设置部分,先设置好主机的网络,然后配置 ...
- MD5做为文件名。机器唯一码有电脑的CPU信息和MAC地址,这两个信息需要在linux或unix系统下才能获取吧。
可以采用机器(电脑)唯一码 + 上传IP + 当前时间戳 + GUID ( + 随机数),然后MD5做为文件名.机器唯一码有电脑的CPU信息和MAC地址,这两个信息需要在linux或unix系统下才能 ...
- ansible command模块将返回值写入变量
ansible 中command模块支持 register参数将远程命令执行的输出结果存储在变量中,后续可以在when中对该变量进行检索确定下一步任务. --- - name: cat /etc/re ...
- java版云笔记(二)
云笔记 基本的环境搭建好了,今天做些什么呢,第一是链接数据库(即搭建Spring-Batistas环境),第二是登录预注册. 注:这个项目的sql文件,需求文档,需要的html文件,jar包都可以去下 ...
- acm专题---dfs+bfs
题目来源:http://hihocoder.com/problemset/problem/1049 #1049 : 后序遍历 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描 ...
- WordPress插件:自定义登录注册插件:DX Login Register
众所周知,wordpress自带的注册系统比较简单,需要接收邮件密码才能完成.不过对于国内的站长来说,会碰到不少麻烦.首先个人站长一般都使用虚拟主机,有不少还是使用国外的,你的服务器不一定会提供邮件发 ...
- 面试题30:最小的K个数
方法一:利用partition void GetLeastNumbers_Solution1(int* input, int n, int* output, int k) { || k <= ) ...
- vscode vue配置和一些其它辅助【工具篇】
后续有补充就经常更新