zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    复习提纲
    查看版本和存储的地方
    0到255的颜色
    stixel-world和psmnet结合出现的问题
    python plt 保存jpg出错
    三和韓長庚 著 正易 對讀 161-200
    startActivity、 startActivityForResult 、广播的使用
    01背包+卡精度 Hdu 2955
    c++ string 之 find_first_not_of 源码
    java:[1,0] illegal character: 65279 问题
  • 原文地址:https://www.cnblogs.com/ToTOrz/p/7743744.html
Copyright © 2011-2022 走看看