• Codeforces 812C. Sagheer and Nubian Market


    On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost ai Egyptian pounds. If Sagheer buys kitems with indices x1, x2, ..., xk, then the cost of item xj is axj + xj·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.

    Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?

    Input

    The first line contains two integers n and S (1 ≤ n ≤ 105 and 1 ≤ S ≤ 109) — the number of souvenirs in the market and Sagheer's budget.

    The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 105) — the base costs of the souvenirs.

    Output

    On a single line, print two integers kT — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these ksouvenirs.

    二分来找到答案,每次先排序O(nlogn)然后累加判断大概是O(n),所以大概是O(nlognlogn),实际也就用了400多ms,应该也算绰绰有余

    #include <bits/stdc++.h>
    #include <iomanip>
    typedef long long LL;
    using namespace std;
    #define SIZE 100005
    
    LL n,s;
    struct node{
      LL price;
      LL pos;
    } a[SIZE];
    
    
    LL k = 0;
    bool cmp(node a1,node a2){
      LL l = (a1.price + a1.pos * k),r = (a2.price + a2.pos * k);
      return (l < r);
    }
    
    
    bool ok(LL now){
      k = now;
      sort(a+1,a+1+n,cmp);
      LL sum = 0;
      for (LL i=1;i<=now;i++){
        sum += (a[i].price + a[i].pos * k);
        if (sum > s){
          return false;
        }
      }
      return true;
    }
    
    LL find(LL l,LL r){
    
      if (l == r-1){
        if (ok(r)){
          return r;
        }
        return l;
      }
    
      if (l == r){
        return l;
      }
    
      LL mid = (l + r) / 2;
      if (ok(mid)){
        return find(mid,r);
      }
      else {
        return find(l,mid-1);
      }
    }
    
    
    int main(){
      // freopen("test.in","r",stdin);
      cin >> n >> s;
      for (int i=1;i<=n;i++){
        cin >> a[i].price;
        a[i].pos = i;
      } 
    
      LL ans = find(0,n);
      cout << ans;
      k = ans;
      sort(a+1,a+1+n,cmp);
      LL sum = 0;
      for (int i=1;i<=ans;i++){
        sum += (a[i].price + a[i].pos * ans);
      }  
      cout << " " << sum;
      return 0;
    }
    View Code
  • 相关阅读:
    判断单链表中是否有环,找到环的入口节点的理论证明
    交叉熵代价函数(作用及公式推导)
    C#调用C++、Opencv的Dll
    腾讯机器学习一面面经
    C#调用C++类库的几种方式
    2017年腾讯基础研究笔试感受
    关于开源库或者SDK的文档问题
    卷积神经网络Lenet-5实现
    NULL、0、nullptr 区别分析
    C++中,new/delete和malloc/free的区别
  • 原文地址:https://www.cnblogs.com/ToTOrz/p/7743744.html
Copyright © 2020-2023  润新知