812C

Sagheer and Nubian Market

二分+排序

比赛时没对,赛后检查才发现check()里的参数类型是int 改成ll就对了。好气哦!

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 const int N = 1e5+10;
 5 ll a[N], b[N], k, t, n, l, r, s, ans;
 6 
 7 bool check(ll x ){
 8     for(int i =1; i <=n ; i ++){
 9         b[i] = a[i]+i*x;
10     }
11     sort(b+1,b+n+1);
12     ans=0;
13     for(int i = 1; i <=x; i ++){
14         ans+=b[i];
15     }
16     if(ans>s)return false;
17     else return true;
18 }
19 
20 int main(){
21     cin >> n >> s;
22     for(int i = 1; i <= n; i ++)scanf("%lld",&a[i]);
23     l = 1, r = n;
24     while(l<=r){
25         ll mid = (l+r)/2;
26         if(check(mid)){
27             k = mid;
28             t = ans;
29             l = mid+1;
30         }else{
31             r = mid-1;
32         }
33     }
34     cout << k << " " << t << endl;
35     return 0;
36 }
原文地址:https://www.cnblogs.com/xingkongyihao/p/6932724.html