cf812 C 二分
2 seconds
256 megabytes
standard input
standard output
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost ai Egyptian pounds. If Sagheer buys k items with indices x1, x2, ..., xk, then the cost of item xj is axj + xj·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
The first line contains two integers n and S (1 ≤ n ≤ 105 and 1 ≤ S ≤ 109) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 105) — the base costs of the souvenirs.
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
3 11
2 3 5
2 11
4 100
1 2 5 6
4 54
1 7
7
0 0
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
当时有想过二分,不过昨晚状态真的差到爆以后状态不好还是睡觉吧ccc
显然对于一个可能的件数k,要想花费最小需要先计算出在这个k约束下的每个物品的价值然后排序找到前k件尝试。
每件物品对应的价值是不确定的每次改变k的值都需要排序时间不允许,于是想到二分件数减少排序次数。
#include<bits/stdc++.h>
using namespace std;
#define LL long long
LL a[100005],b[100005];
LL s,n;
LL Find(int k)
{
LL sum=0;
for(int i=1;i<=n;++i) b[i]=a[i]+i*k;
sort(b+1,b+1+n);
for(int i=1;i<=k;++i) sum+=b[i];
return sum<=s?sum:-1;
}
int main()
{
int i,j;
cin>>n>>s;
for(i=1;i<=n;++i) cin>>a[i];
int l=0,r=n,mid;
while(l<r){
mid=r-(r-l)/2;
//cout<<l<<" "<<r<<" "<<mid<<endl;
if(Find(mid)>0){
l=mid;
}
else{
r=mid-1;
}
}
cout<<r<<" "<<Find(r)<<endl;
return 0;
}
cf812 C 二分的更多相关文章
- BZOJ1012: [JSOI2008]最大数maxnumber [线段树 | 单调栈+二分]
1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec Memory Limit: 162 MBSubmit: 8748 Solved: 3835[Submi ...
- BZOJ 2756: [SCOI2012]奇怪的游戏 [最大流 二分]
2756: [SCOI2012]奇怪的游戏 Time Limit: 40 Sec Memory Limit: 128 MBSubmit: 3352 Solved: 919[Submit][Stat ...
- 整体二分QAQ
POJ 2104 K-th Number 时空隧道 题意: 给出一个序列,每次查询区间第k小 分析: 整体二分入门题? 代码: #include<algorithm> #include&l ...
- [bzoj2653][middle] (二分 + 主席树)
Description 一个长度为n的序列a,设其排过序之后为b,其中位数定义为b[n/2],其中a,b从0开始标号,除法取下整. 给你一个长度为n的序列s. 回答Q个这样的询问:s的左端点在[a,b ...
- [LeetCode] Closest Binary Search Tree Value II 最近的二分搜索树的值之二
Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...
- [LeetCode] Closest Binary Search Tree Value 最近的二分搜索树的值
Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...
- jvascript 顺序查找和二分查找法
第一种:顺序查找法 中心思想:和数组中的值逐个比对! /* * 参数说明: * array:传入数组 * findVal:传入需要查找的数 */ function Orderseach(array,f ...
- BZOJ 1305: [CQOI2009]dance跳舞 二分+最大流
1305: [CQOI2009]dance跳舞 Description 一次舞会有n个男孩和n个女孩.每首曲子开始时,所有男孩和女孩恰好配成n对跳交谊舞.每个男孩都不会和同一个女孩跳两首(或更多)舞曲 ...
- BZOJ 3110 [Zjoi2013]K大数查询 ——整体二分
[题目分析] 整体二分显而易见. 自己YY了一下用树状数组区间修改,区间查询的操作. 又因为一个字母调了一下午. 貌似树状数组并不需要清空,可以用一个指针来维护,可以少一个log 懒得写了. [代码] ...
随机推荐
- pta 习题集5-6 堆栈操作合法性
假设以S和X分别表示入栈和出栈操作.如果根据一个仅由S和X构成的序列,对一个空堆栈进行操作,相应操作均可行(如没有出现删除时栈空)且最后状态也是栈空,则称该序列是合法的堆栈操作序列.请编写程序,输入S ...
- ubuntu ---QQ install/desktop/ibus reinstall
http://www.linuxidc.com/Linux/2016-09/134923.htm ( Ubuntu 16.04安装QQ国际版图文详细教程) [ sudo apt-get install ...
- 洛谷P1373 小a和uim之大逃离 dp
正解:dp 解题报告: 传送门! 同样是看到列表发的题解就想着跟着做下dp的题目趴 然后发现还挺难的,,,反正我只大概想到怎么转移但是初始化什么的都不会TT 所以还是大概说下QAQ 首先可以想到设f[ ...
- java-mybaits-00103-入门程序原生的【查、增、删、改】
一.需求 实现以下功能: 根据用户id查询一个用户信息 根据用户名称模糊查询用户信息列表 添加用户 更新用户 删除用户 二.具体步骤 1.增加pom引用 2.增加log4j.properties # ...
- Python学习开发资源大全列表
1 机器学习和计算机视觉 Crab:灵活.快速的推荐引擎 gensim:人性化的话题建模库 hebel:GPU 加速的深度学习库 NuPIC:智能计算 Numenta 平台 pattern:Pytho ...
- 跟我学Makefile(六)
shell 函数 :和反引号“`”是相同的功能 . shell 函数把执行操作系统命令后的输出作为函数返回. contents := $(shell cat foo) files := $(shell ...
- PAT 1100 Mars Numbers[难]
1100 Mars Numbers (20 分) People on Mars count their numbers with base 13: Zero on Earth is called &q ...
- 002-字段不为null
1.尽量不要在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索引而进行全表扫描,强烈建议where涉及的列,不要留空,创建表时赋予初始值. 比如 select id from ...
- ZW云推客即将登场
ZW云推客即将登场 即"4K云字库"框架文件发布后,ZW团队即将发布一全功能的:ZW云推客系统. ZW云推客系统,是原zBrow"百万社区营销系统".&qu ...
- this指向 - 总结
/* 总结: this 的指向: 1.this 的指向 是在执行上下文时才确定的, 并且确定后不可更改: 2.this 指向 “其执行上下文的环境对象”; “其执行上下文的环境对象” 判读依据如下: ...