cf812 C 二分
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;
}