Codeforces Round #417 (Div. 2) C. Sagheer and Nubian Market(二分)
题目链接:Codeforces Round #417 (Div. 2) C. Sagheer and Nubian Market
题意:
有n个物品,S的钱,买每个物品有个计算价钱的公式。
让你在S钱中买最多的物品。
题解:
二分瞎搞一下就行了。
1 #include<bits/stdc++.h> 2 #define F(i,a,b) for(int i=(a);i<=(b);++i) 3 using namespace std; 4 typedef long long ll; 5 const int N=1e5+7; 6 7 struct Node 8 { 9 int a,idx; 10 ll v; 11 bool operator <(const Node &b)const{return v<b.v;} 12 }a[N]; 13 14 int n,m; 15 16 int check(int mid) 17 { 18 F(i,1,n)a[i].v=a[i].a+1ll*a[i].idx*mid; 19 sort(a+1,a+1+n); 20 ll an=0; 21 F(i,1,mid)an+=a[i].v; 22 return an<=m?an:-1; 23 } 24 25 int main() 26 { 27 scanf("%d%d",&n,&m); 28 F(i,1,n)scanf("%d",&a[i].a),a[i].idx=i; 29 int l=1,r=n,mid,ans=0,ans2=0,tp; 30 while(l<=r) 31 { 32 mid=l+r>>1; 33 if((tp=check(mid))!=-1)ans=mid,ans2=tp,l=mid+1; 34 else r=mid-1; 35 } 36 printf("%d %d\n",ans,ans2); 37 return 0; 38 }