题目描述:

Sagheer and Nubian Market

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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 \(a_i\) Egyptian pounds. If Sagheer buys k items with indices x1, x2, ..., \(x_k\), then the cost of item x**j is \(a_{x_j}\) + \(x_j\)·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?

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, ..., a**n (1 ≤ a**i ≤ 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

Input

Copy

3 112 3 5

Output

Copy

2 11

Input

Copy

4 1001 2 5 6

Output

Copy

4 54

Input

Copy

1 77

Output

Copy

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 8 pounds, so he cannot buy it.

思路:

题目是说现在有S块钱,有N个物品,物品的价格是本价加上购买的物品数乘物品编号,每个物品只能买一次。求S最多能买多少个物品,如果在买的数量相同的情况下求出用钱最少的。

刚开始,一看n是\(10^5\)量级,可以枚举来做,从n到0,每次枚举都算一下每件物品的花费,为了买到的物品最多的情况下花费要尽可能少,给每样物品的花费排一个序,再求一个花费前缀和,再用lower_bound二分查找大于S的值的位置,减一就是小于等于S的花费的位置记为pos。

下面详细说一说pos的含义:如果找到了一个位置pos(这里是前缀和,位置实际上就是购买的物品数目)花费小于等于S,这个位置pos表示了在资金足够的情况下,在枚举的物品数i的限制下,最多可以买pos个物品。如果pos等于i,就刚刚好是可以购买的最大物品数。如果pos>i,因为现在可能还没用完S,就说明有可能买更多的物品(同时买更多的物品时每个物品的花费要高些),如果pos<i,就说明现在资金用到最大程度都买不到i个物品,说明i不满足条件,继续往下枚举。(这是当时意识不太清醒时想出来的,现在来看竟然感觉不是很好理解,佩服我自己)。

由于i是从大到小枚举的,那pos>i的状态表示的购买更多物品的可能性已经枚举过了,现在只有两种可能,就是当前枚举值满足和不满足。一旦出现满足,跳出循环,输出答案。这种方法的时间复杂度是\(O(n^2log_2n)\)的,过不了太多数据。

代码:

#include <iostream>
#include <algorithm>
#include <memory.h>
#include <cstdio>
#define max_n 100005
#define INF 0x3f3f3f3f
using namespace std;
int n;
int S;
int a[max_n];
long long sum[max_n];
template<typename T>
inline void read(T& x)
{
x=0;int f=0;char ch=getchar();
while('0'>ch||ch>'9'){if(ch=='-')f=1;ch=getchar();}
while('0'<=ch&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
x=f?-x:x;
}
#pragma optimize(2)
int main()
{
read(n);
read(S);
int minm = INF;
for(int i = 1;i<=n;i++)
{
read(a[i]);
minm = min(minm,a[i]);
}
int items = S/minm;//降一下枚举的起点
items = min(items,n);
long long money = 0;
int pos = 0;
for(int i = items;i>0;i--)
{
memset(sum,0,sizeof(sum));
for(int j = 1;j<=n;j++)
{
sum[j] = a[j] + i*j;//求花费
}
sort(sum+1,sum+n+1);
/*for(int j = 1;j<=n;j++)
{
cout << sum[j] << " ";
}
cout << endl;*/
for(int j = 1;j<=n;j++)
{
sum[j] += sum[j-1];//求花费的前缀和
}
/*for(int j = 1;j<=n;j++)
{
cout << sum[j] << " ";
}
cout << endl;
cout << endl;*/
pos = upper_bound(sum+1,sum+n+1,S)-sum-1;
//cout << "pos " << pos << endl;
if(pos>=i)
{ if(pos<=n)//这里是所有元素都小于S时的情况
{
money = sum[pos];
}
else
{
money = sum[pos-1];
}
break;
}
}
printf("%d %I64d\n",pos,money);
return 0;
}

其实枚举是对的,不过不是线性枚举,采用二分枚举,复杂度降到\(O(log_2(nlog_2n))\)。

注意返回的是记录下的最后的满足条件的枚举的物品数量。

代码:

#include <iostream>
#include <algorithm>
#define max_n 100005
using namespace std;
int n;
int S;
int a[max_n];
long long sum[max_n];
long long cost = 0;
long long mincost = 0;
int items = 0;
template<typename T>
inline void read(T& x)
{
x=0;int f=0;char ch=getchar();
while('0'>ch||ch>'9'){if(ch=='-')f=1;ch=getchar();}
while('0'<=ch&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
x=f?-x:x;
}
#pragma optimize(2)
int main()
{
read(n);
read(S);
for(int i = 1;i<=n;i++)
{
read(a[i]);
}
int l = 0;
int r = n;
int mid = 0;
while(l<=r)
{
//cout << "l " << l << " r " << r << endl;
cost = 0;
mid = (l+r)>>1;
//cout << "mid " << mid << endl;
for(int i = 1;i<=n;i++)
{
sum[i] = (long long)a[i]+(long long)mid*i;
}
sort(sum+1,sum+n+1);
for(int i = 1;i<=mid;i++)
{
cost += sum[i];
}
//cout << "cost " << cost << endl;
if(S>=cost)
{
items = mid;
mincost = cost;
l=mid+1;
}
else
{
r=mid-1;
}
}
printf("%d %I64d",items,mincost);
return 0;
}

Codeforces J. Sagheer and Nubian Market(二分枚举)的更多相关文章

  1. 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 ...

  2. CodeForce-812C Sagheer and Nubian Market(二分)

    Sagheer and Nubian Market CodeForces - 812C 题意:n个货物,每个货物基础价格是ai. 当你一共购买k个货物时,每个货物的价格为a[i]+k*i. 每个货物只 ...

  3. 【贪心+二分】codeforces C. Sagheer and Nubian Market

    http://codeforces.com/contest/812/problem/C [题意] 如何花最少的钱买最多的纪念品?首要满足纪念品尽可能多,纪念品数量一样花钱要最少,输出纪念品数量以及最少 ...

  4. #417 Div2 Problem C Sagheer and Nubian Market (二分 && std::accumulate)

    题目链接 : http://codeforces.com/problemset/problem/812/C 题意 : 给你 n 件物品和你拥有的钱 S, 接下来给出这 n 件物品的价格, 这些物品的价 ...

  5. CF812C Sagheer and Nubian Market 二分+贪心

    模拟赛给他们出T1好了~ code: #include <bits/stdc++.h> #define ll long long #define N 100006 #define setI ...

  6. 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 ...

  7. AC日记——Sagheer and Nubian Market codeforces 812c

    C - Sagheer and Nubian Market 思路: 二分: 代码: #include <bits/stdc++.h> using namespace std; #defin ...

  8. CF812C Sagheer and Nubian Market

    CF812C Sagheer and Nubian Market 洛谷评测传送门 题目描述 On his trip to Luxor and Aswan, Sagheer went to a Nubi ...

  9. 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 ...

随机推荐

  1. Python例题集

    例题1:任意输入一组数据比较其最大值并记录输入的数据个数. 源代码: def max(*a): m=a[0] i=0 for x in a: i+=1 if x>m: m=x print('参数 ...

  2. Linux性能优化实战学习笔记:第五十讲

    一.上节回顾 上一节,我以 ksoftirqd CPU 使用率高的问题为例,带你一起学习了内核线程 CPU 使用率高时的分析方法.先简单回顾一下. 当碰到内核线程的资源使用异常时,很多常用的进程级性能 ...

  3. IDEA 部署Tomcat教程(透彻理解操作)

    目录 首先我们看一下 IDEA 里的当前项目结构配置 设置 Web 资源目录和 Tomcat读取的 web.xml 配置文件 Tomcat 的 Run/Debug 配置 处理常见问题 Web资源找不到 ...

  4. 拒绝后门程序-Alibabaprotect和AliPaladin

    详细参考帖子及评论区:流氓进程AlibabaProtect的删除[程序员吧]_百度贴吧 首先打开服务找到AlibabaProtect,然后找到他的位置(C:\Program Files (x86)\A ...

  5. 修改Launchpad的命令

    修改Launchpad命令 1.设置Launchpad 图标的列数 defaults write com.apple.dock springboard-columns -int 10 2.设置 Lau ...

  6. 《TP5.0学习笔记---模板变量输出、替换和赋值篇》

    原文地址:http://blog.csdn.net/self_realian/article/details/75214922 模板变量输出.替换和赋值 我们看一下文件编译的结果,我们知道我们现在写的 ...

  7. pom中更换阿里云仓库时不要忽略了pluginRepositories

    用maven也大几年了,也一直在用阿里云的中央仓库. 不喜欢在maven的settings.xml里改,更喜欢直接在pom.xml里改,因为受git管理,小伙伴们拉下来即可. 然而网上的大部分技术文章 ...

  8. numpy delete方法

    import numpy as np lines = np.loadtxt(r'./test.txt',delimiter=',',dtype=int) print(lines) lines_copy ...

  9. RBAC类在ThinkPHP中的四种使用方法

    第一类:放在登陆控制器的登陆操作中 1.RBAC::authenticate(); 用于在用户表中查找表单提交的用户名的数据,实质上就是一条用户表查寻语句,=====> return M(mod ...

  10. Java 微信支付分对接记录 (先享后付)

    微信支付分(先享后付)对接记录: 微信支付分对接步骤 填写开通支付分的申请表格 此步骤大概需要审核 1-3 个工作日; (模板-服务信息配置表-[先享后付免确认]-[商户名].xls) 填写商户信息 ...