zoukankan      html  css  js  c++  java
  • codeforces 812C. Sagheer and Nubian Market

    题目链接:http://codeforces.com/contest/812/problem/C

    题意:n个货物,每个货物基础价格是ai。当你一共购买k个货物时,每个货物的花费为a[i]+k*i,每个货物只能购买一次。给你s金币,问你最多可以购买多少个货物,这些货物的最小花费。

    分析:结构体存储每个货物的价格val和id,还有购买k个货物时购买他需要的价格sm,当购买k个货物时,每个货物的价格就会变成sm=val+id*k,sort结构体按sm从小到大即可求出购买k个货物的最小花费。由于可以购买货物的数量具有单调性,可以二分购买货物数量。注意开longlong,比赛的时候wa3发QAQ。

    AC代码:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 
     4 struct st{
     5     long long id;
     6     long long val;
     7     long long sum;
     8 }a[100005];
     9 bool cmp(st a,st b){
    10     return a.sum<b.sum;
    11 }
    12 int main() {
    13     ios_base::sync_with_stdio(0);
    14     cin.tie(0);
    15     int n;
    16     long long s;
    17     cin>>n>>s;
    18     for(int i=1;i<=n;i++){
    19         cin>>a[i].val;
    20         a[i].id=i;
    21     }
    22     int result=0;
    23     long long t=0;
    24     int low=0,high=n;
    25     while(low<=high){
    26         int mid=(low+high)/2;
    27         for(int i=1;i<=n;i++){
    28             a[i].sum=a[i].val+a[i].id*mid;
    29         }
    30         long long sm=0;
    31         sort(a+1,a+1+n,cmp);
    32         for(int i=1;i<=mid;i++){
    33             sm+=a[i].sum;
    34             if(sm>s){
    35                 break;
    36             }
    37         }
    38         if(sm<=s){
    39             result=mid;
    40             t=sm;
    41             low=mid+1;
    42         }
    43         else {
    44             high=mid-1;
    45         }
    46     }
    47     cout<<result<<" "<<t<<endl;
    48 return 0;
    49 }
    View Code
  • 相关阅读:
    less的安装使用
    bootStrap
    响应式布局——媒体查询
    发光渐变器
    好看的按钮
    CSS3:2D、3D属性
    CSS3渐变
    background
    过渡transition、opacity的兼容性
    【未完成】【java异常】java.lang.IllegalStateException: No suitable default RequestUpgradeStrategy
  • 原文地址:https://www.cnblogs.com/ls961006/p/6931861.html
Copyright © 2011-2022 走看看