• 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
  • 相关阅读:
    多组件共享-vuex —— 使用vuex 报错 actions should be function or object with ”handler“
    时间复杂度/空间复杂度
    Nodejs学习(三)-安装nodejs supervisor,提高点效率吧。
    Nodejs学习(二)-express生成器
    Nodejs学习(一)-Nodejs和express的安装和配置
    PHP连接MySQL的时候报错SQLSTATE[HY000] [2002] No such file or directory
    phpstorm 16.1 注册码
    Express安装过程
    NodeJs解析web一例
    NodeJs 连接mysql一例。
  • 原文地址:https://www.cnblogs.com/ToTOrz/p/7743744.html
Copyright © 2020-2023  润新知