812C - Sagheer and Nubian Market

Posted starry

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了812C - Sagheer and Nubian Market相关的知识,希望对你有一定的参考价值。

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 }

 

以上是关于812C - Sagheer and Nubian Market的主要内容,如果未能解决你的问题,请参考以下文章

codefoces812C-Sagheer and Nubian Market心得

Round #417 C. Sagheer and Nubian Market(Div.2)

#417(div2) C. Sagheer and Nubian Market

Codeforces Round #417 (Div. 2) C. Sagheer and Nubian Market 二分答案 +排序

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

Codeforces 812E Sagheer and Apple Tree