cf812c

Thinking

贪心 + 二分


Answer

#include <iostream>
#include <algorithm>
using namespace std;

int k; // the amount of items

struct item{
    long long price;
    long long pos;
};

bool compare(item a, item b){
    return a.price + a.pos * k < b.price + b.pos * k;
}

int main(){

    // input
    int n, S;
    cin >> n >> S;
    item items[n];
    for (int i = 0; i < n; ++i) {
        cin >> items[i].price;
        items[i].pos = i + 1;
    }

    // work
    int kok = 0; long long tot = S;
    int low = 1, high = n;

    while (low <= high){
        k = low + high >> 1;
        stable_sort(items, items+n, compare);
        // total prices
        long long t = 0;
        for (int i = 0; i < k; ++i) {
            t += items[i].price + items[i].pos * k;
        }

        if (t < S && t > 0){
            if ((t < tot && k == kok) || k > kok){
                tot = t;
                kok = k;
            }
            low = k+1;
        }
        else if (t == S){
            tot = t;
            kok = k;
            break;
        }
        else{
            high = k-1;
        }
    }

    if (kok == 0) tot = 0;
    cout << kok << " " << tot << endl;

}
posted @ 2017-08-04 15:54  Aneureka  阅读(398)  评论(0编辑  收藏  举报