首页 > 代码库 > #417(div2) C. Sagheer and Nubian Market

#417(div2) C. Sagheer and Nubian Market

题意:给出n件商品价格和一个最大限额S,购买时商品的价格变为axj?+?xj·k,xj下标索引,k是购买数量,求最多能买几件和花费。

思路:二分+排序

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 
 5 ll a[100005];
 6 ll b[100005];
 7 ll n,s;
 8 ll sum;
 9 ll check(ll x){
10    sum=0;
11     for(int i=1;i<=n;i++)
12         b[i]=a[i]+i*x;
13     sort(b+1,b+1+n);
14     for(int i=1;i<=x;i++) sum+=b[i];
15     return sum;
16  }
17 int main(){
18     ll ssum=0;
19     ll x;
20     scanf("%I64d%I64d",&n,&s);
21     for(int i=1;i<=n;i++) {
22             scanf("%I64d",&a[i]);
23     }
24     ll l=1,r=n,mid,ans=0;
25     while(l<=r){
26         mid=(l+r)>>1;
27         if(check(mid)<=s){
28             l=mid+1,ans=mid;
29             ssum=sum;
30 
31         }
32         else r=mid-1;
33     }
34     cout<<ans<<" "<<ssum<<endl;
35 }

 

#417(div2) C. Sagheer and Nubian Market