Sagheer and Nubian Market CodeForces - 812C (二分)
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 aiEgyptian 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 SEgyptian 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?
Input
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.
Output
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.
Examples
3 11
2 3 5
2 11
4 100
1 2 5 6
4 54
1 7
7
0 0
Note
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 8pounds, so he cannot buy it.
题目比较狗血,中文题面点击这里。https://vjudge.net/problem/CodeForces-812C#author=ChineseOJ
思路:
根据题意可知,要买的商品个数ans越多,那么每一个商品的价格就越高,价格高导致实力能负担得起的商品数量又下降,
那么我们可以知道 可以购买到的商品数量和打算购买的商品数量呈单调关系,
那么如果是单调关系即可以进行二分了。
二分购买的商品数量ans,范围是[0,n]
然后每一次O(n*logn)去检查mid是否满足条件,
所以总时间复杂度为O( n*logn*logn ) n上限为1e5,所以可以满足。
那么如何检查一个ans是否满足呢?
首先根据题目给的公式把实际购买的价格算出来,然后排序,贪心的选取价格比较小的那ans个,如果sum和小于等于S,即满足条件return 1;
更多细节见我的代码哦:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== "<<x<<" =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=;while(b){if(b%)ans=ans*a%MOD;a=a*a%MOD;b/=;}return ans;}
inline void getInt(int* p);
const int maxn=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
ll n;
ll m;
ll a[maxn];
ll b[maxn];
ll sum[maxn];
bool check(ll mid)
{
sum[]=0ll;
for(ll i=1ll;i<=n;i++)
{
b[i]=a[i]+i*mid;
// sum[i]=b[i]+sum[i-1];
}
sort(b+,b++n);
for(ll i=1ll;i<=mid;i++)
{
sum[i]=b[i]+sum[i-];
}
return m>=sum[mid];
}
int main()
{
gbtb;
cin>>n>>m;
repd(i,,n)
{
cin>>a[i];
}
// sort(a+1,a+1+n);
ll l=;
ll r=n;
ll mid;
ll ans=;
ll ANS=0ll;
while(l<=r)
{
mid=(l+r)>>;
if(check(mid))
{
ans=mid;
ANS=sum[mid];
l=mid+;
}else
{
r=mid-;
}
}
cout<<ans<<" "<<ANS<<endl;
return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}
Sagheer and Nubian Market CodeForces - 812C (二分)的更多相关文章
- AC日记——Sagheer and Nubian Market codeforces 812c
C - Sagheer and Nubian Market 思路: 二分: 代码: #include <bits/stdc++.h> using namespace std; #defin ...
- CodeForce-812C Sagheer and Nubian Market(二分)
Sagheer and Nubian Market CodeForces - 812C 题意:n个货物,每个货物基础价格是ai. 当你一共购买k个货物时,每个货物的价格为a[i]+k*i. 每个货物只 ...
- Codeforces J. Sagheer and Nubian Market(二分枚举)
题目描述: Sagheer and Nubian Market time limit per test 2 seconds memory limit per test 256 megabytes in ...
- Codeforces Round #417 C. Sagheer and Nubian Market
C. Sagheer and Nubian Market time limit per test 2 seconds memory limit per test 256 megabytes O ...
- Codeforces812C Sagheer and Nubian Market 2017-06-02 20:39 153人阅读 评论(0) 收藏
C. Sagheer and Nubian Market time limit per test 2 seconds memory limit per test 256 megabytes input ...
- CF812C Sagheer and Nubian Market
CF812C Sagheer and Nubian Market 洛谷评测传送门 题目描述 On his trip to Luxor and Aswan, Sagheer went to a Nubi ...
- CodeForces - 812C Sagheer and Nubian Market 二分
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friend ...
- Codeforces Round #417 (Div. 2) C. Sagheer and Nubian Market
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- 【贪心+二分】codeforces C. Sagheer and Nubian Market
http://codeforces.com/contest/812/problem/C [题意] 如何花最少的钱买最多的纪念品?首要满足纪念品尽可能多,纪念品数量一样花钱要最少,输出纪念品数量以及最少 ...
随机推荐
- 语句调优基础知识-set statistics io on
set statistics io on --清空缓存数据 dbcc dropcleanbuffers go --清空缓存计划 dbcc freeproccache go set statistics ...
- 关于JBoss -“Closing a connection for you,please close them yourself”
使用JNDI的方式从Jboss里获取数据连接(Connection)的方式,Jboss会管理connection,不需要自己手动去关闭,但Jboss老是提示需要自己来关闭connection,针对Jb ...
- 对Can We MakeOperating SystemsReliable and Secure 的翻译
摘要:微内核-相对于大内核(monolithic kernels)来说,由于它的 lower performance,长期以来被认为是不可接受的.而现在,由于它潜 在的高可靠性(higher reli ...
- 《Java大学教程》—第5章 数组
5.6 增强的for循环:访问整个数组,读取数组元素,不基于数据下列5.7 数组方法:最大值.求和.成员访问.查找 1.答:P92存储固定个数相同数据类型的一组元素. 2.答:P92所有存储在一个特定 ...
- 《Java大学教程》--第1章 步入Java世界
1.2 软件:用于计算机执行的指令的集合称之为程序(program).单个程序或者一组程序称之为软件(software)1.3 编译:计算机的语言称为机器码(machine code).用编译器(co ...
- TCP长连接保持连接状态
对于TCP长连接保活是十分必要的,原因如下: 1.系统多在OA网和外网间有防火墙隔离,很多防火墙对一段时间内没有报文活动的socket会自动关闭. 2.对于非正常断开的连接系统并不能侦测到,比如防火墙 ...
- 牛客 小a与星际探索 bfs
链接:https://ac.nowcoder.com/acm/contest/317/C?&headNav=acm来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ ...
- .Net下的全局异常捕获问题
全局异常捕获主要目标并不是为了将异常处理掉防止程序崩溃.因为当错误被你的全局异常捕获器抓到的时候,已经证实了你程序中存在BUG. 一般而言,我们的全局异常捕获主要作用就是接收到异常之后进行异常的反馈. ...
- 转://RMAN跨平台可传输表空间和数据库
参考链接: http://blog.itpub.net/23135684/viewspace-776048/ http://blog.sina.com.cn/s/blog_69e7b8d7010164 ...
- 【转】具透 | 你可能不知道,iOS 10 有一个中国「特供」的联网权限功能
9 月底,苹果正式在北京成立了苹果中国研发中心.近几年,我们也在每年更新的 iOS 系统中不断看到,苹果对中国市场的关照.从早前的九宫格输入法,到最近的骚扰电话拦截,都照顾了国内用户的需求. 在 iO ...