zl程序教程

您现在的位置是:首页 >  其它

当前栏目

【codeforces 812C】Sagheer and Nubian Market

and Codeforces
2023-09-14 09:03:50 时间

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

【题意】

给你n个物品;
你可以选购k个物品;则
每个物品有一个基础价值;
然后还有一个附加价值;
即为k*i;
这里i是这个物品的下标;(1..n中的某个整数);
然后你有预算S;
问你最多能买多少个物品ans;
并求出买ans个物品的最小花费;

【题解】

二分买的物品数量k;
然后就能获取出每个物品的价格了;
即a[i]+i*k;
放到b数组里面;
升序排;
然后选取前k个;
看看超不超预算;
不超就记录答案,并让物品数量变大;
否则变小;
貌似在算的时候会爆int;

【Number Of WA

1

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define Open() freopen("F:\\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0),cin.tie(0)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 1e5+100;

LL n,s,tans;
LL a[N],b[N];

bool ok(LL k){
    for (LL i = 1;i <= n;i++){
        b[i] = a[i]+i*k;
    }
    sort(b+1,b+1+n);
    LL temp = 0;
    rep1(i,1,k){
        temp+=b[i];
        if (temp>s) return false;
    }
    tans = temp;
    return true;
}

int main(){
    //Open();
    Close();//scanf,puts,printf not use
    //init??????
    cin >> n >> s;
    rep1(i,1,n){
        cin >> a[i];
    }
    int l = 0,r = n,ans = 0;
    while (l <= r){
        int mid = (l+r)>>1;
        if (ok(mid)){
            ans = mid;
            l = mid+1;
        }
        else
            r = mid-1;
    }
    cout << ans <<' '<<tans<<endl;
    return 0;
}