Lotus and Characters (stronger)
题意:
有n类物品,第i种物品权值为$val(i)$,有$cnt(i)$个,现在你可以选出一些物品排成一个序列(假设有m个),
记第i个物品种类为$x_i$,最大化$\sum_{i=1}^m{i * val(x_i)}$
解法:
只要将物品分为两类即可。
对于$val(i) \ge 0$的直接从小到大排列插入序列末端即可,然后在序列首端插入物品,
记当前后缀和为$suffixsum$。
接下来从大到小插入$val(i)<0$的物品,每插入一个会产生$suffixsum+val(i)$的答案贡献,并且影响$suffixsum$
贪心正确性显然,接下来考虑加速插入物品的过程,
一次性插入t个i类物品不更新$suffixsum$对答案的贡献为$t*suffixsum + \frac{t*(t+1)}{2}*val(i)$,
且有$t \le \frac{-suffixsum}{val(i)}$。
从而$O(nlogn)$解决此问题。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> #define N 1010
#define LL long long using namespace std; int val[N],cnt[N],a[N]; bool cmp(int a,int b)
{
return val[a]<val[b];
} LL S(LL n)
{
return n*(n+1LL)/2LL;
} int main()
{
int T,n;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d%d",&val[i],&cnt[i]),a[i]=i;
sort(a+,a+n+,cmp);
int sum=;
LL ans=,suffix_sum=;
for(int i=;i<=n;i++)
{
if(val[a[i]]<) continue;
sum += cnt[a[i]];
ans += (S(sum)-S(sum-cnt[a[i]])+0LL) * (LL)val[a[i]];
suffix_sum += cnt[a[i]] * (LL)val[a[i]];
}
for(int i=n;suffix_sum> && i>=;i--)
{
if(val[a[i]]>=) continue;
LL tmp = (-suffix_sum)/val[a[i]];
tmp = min(tmp, (LL)cnt[a[i]]);
ans += tmp*suffix_sum + S(tmp)*val[a[i]];
suffix_sum += tmp*val[a[i]];
}
cout << ans << endl;
}
return ;
}
Lotus and Characters (stronger)的更多相关文章
- Best Coder Lotus and Characters
Lotus and Characters 问题描述 Lotus有nn种字母,给出每种字母的价值以及每种字母的个数限制,她想构造一个任意长度的串. 定义串的价值为:第1位字母的价值*1+第2位字母的 ...
- BestCoder Round #91 1001 Lotus and Characters
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6011 题意: Lotus有nn种字母,给出每种字母的价值以及每种字母的个数限制,她想构造一个任意长度的 ...
- hdu 6011 Lotus and Characters 贪心
http://acm.hdu.edu.cn/showproblem.php?pid=6011 先把数字从小到大排好,比如是-6.3.4这样, 然后处理出后缀和,当后缀和 <= 0的时候马上停止就 ...
- HDU 6011:Lotus and Characters(贪心)
http://acm.hdu.edu.cn/showproblem.php?pid=6011 题意:共有n种字符,每种字符有一个val和一个cnt,代表这个字符的价值和数量.可以制造的总价值是:第一个 ...
- OI 刷题记录——每周更新
每周日更新 2016.05.29 UVa中国麻将(Chinese Mahjong,Uva 11210) UVa新汉诺塔问题(A Different Task,Uva 10795) NOIP2012同余 ...
- LeetCode[3] Longest Substring Without Repeating Characters
题目描述 Given a string, find the length of the longest substring without repeating characters. For exam ...
- [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- [LeetCode] Sort Characters By Frequency 根据字符出现频率排序
Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: ...
- [LeetCode] Longest Substring with At Most K Distinct Characters 最多有K个不同字符的最长子串
Given a string, find the length of the longest substring T that contains at most k distinct characte ...
随机推荐
- 现成Android 5.0系统源代码
让Android融入我的生活! 写Android一段时间了,每次看到网上一些大牛的博客.分析Android底层Zygote启动.Activity启动.View的绘制过程.SurfaceFlinger. ...
- Google Chrome浏览器之删除Goolge搜索结果重定向插件Remove Google Redirects
https://chrome.google.com/webstore/detail/remove-google-redirects/ccenmflbeofaceccfhhggbagkblihpoh?h ...
- CentOS Linux搭建独立SVN Server全套流程(转)
环境为centos6.3 1.首先 看看机器上安装了svn了没有 rpm -qa |grep svn 2.如果没有安装 执行 yum -y install subversion 3.安装好了之后 新建 ...
- Laravel 5.4建站06--API 认证系统 Passport
介绍 在 Laravel 中,实现基于传统表单的登陆和授权已经非常简单,但是如何满足 API 场景下的授权需求呢?在 API 场景里通常通过令牌来实现用户授权,而非维护请求之间的 Session 状态 ...
- nvidia-docker_1.0.1-1_amd64.deb 百度云下载分享
nvidia-docker_1.0.1-1_amd64.deb 链接: https://pan.baidu.com/s/1i5pHFNZ 密码: xjui
- celery模块
Celery Celery是管理分布式任务队列的工具,其本身并不是任务队列. Celery常见概念有brokers.backend.workers.tasks. brokers:中间人,是Celery ...
- ipa验证错误问题总结
The following issues were found during validation.这个error的产生原因是因为代码中写的标示符或者方法名,与系统的命名空间冲突. 具体是哪个标示符或 ...
- 使用unidac 连接FB 3.0 (含嵌入版)
unidac 是delphi 最强大的数据库连接控件,没有之一.详细信息可以通过官网了解. Firebird是一个跨平台的关系数据库系统,目前能够运行在Windows.linux和各种Unix操作系 ...
- vmware虚拟机centos网络配置错误,执行/etc/init.d/network start 或 restart 提示Device eth0 has different MAC address than expected, ignoring
vmware虚拟机centos网络配置错误,执行/etc/init.d/network start 或 restart 提示Device eth0 has different MAC address ...
- ubuntu下配置rails环境遇到的错误
1.Could not find gem 'sqlite3 (>= 0)' in any of the gem sources listed in your Gemfile 解决:sudo ge ...