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 ...
随机推荐
- Python操作MySQL:pymysql和SQLAlchemy
本篇对于Python操作MySQL主要使用两种方式: 原生模块 pymsql ORM框架 SQLAchemy pymsql pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb ...
- C#给指定doc文件写入宏
private void InsertMacro() { Word.Application oWord; Word.Document oDoc; VBIDE.VBComponent oModule; ...
- C语言各种keyword
1.register 在函数内定义变量时.默认是 auto 类型,变量存储在内存中,当程序用到该变量时,由控制器发出指令将内存中该变量的值送到运算器,计算结束后再从运算器将数据送到内存.假设一个变量用 ...
- Apcahe Shiro学习笔记(二):通过JDBC进行权限控制
一.概述: 官方对Realm(领域)的描述:https://www.infoq.com/articles/apache-shiro 其功能本质上是一个安全特定的DAO,用于链接数据持久层(任何形式的都 ...
- windows下route命令详解(转载)
1.具体功能 该命令用于在本地IP路由表中显示和修改条目.使用不带参数的ROUTE可以显示帮助. 2.语法详解 route [-f] [-p] [co ...
- OpenCV 的四大模块
前言 我们都知道 OpenCV 是一个开源的计算机视觉库,那么里面到底有哪些东西?本文将为你解答这个问题. 模块一:CV 这个模块是 OpenCV 的核心,它包含了基本的图像处理函数和高级的计算机视觉 ...
- 【转】安卓逆向实践5——IDA动态调试so源码
之前的安卓逆向都是在Java层上面的,但是当前大多数App,为了安全或者效率问题,会把一些重要功能放到native层,所以这里通过例子记录一下使用IDA对so文件进行调试的过程并对要点进行总结. 一. ...
- Möbius strip
en.wikipedia.org/wiki/Möbius_strip http://mechproto.olin.edu/final_projects/average_jo.html Fabricat ...
- s1考试 图书管理系统 结构体版
#include <iostream> #include <string> #include <cstdio> #include <cstdlib> # ...
- 关于4Ps 、4Cs 、4Rs 、4Vs营销策略的内容及优劣比较