题目大意:
给你n个物品,还有m钱
每个物品的价值为k【i】;
请你用不超过m钱买最多个物品,有多少个 ,花了多少钱?
每个物品的价格为该物品的价格k【i】+ (该物品的下标+1)* 买的物品个数
思路 :简单的二分枚举
一开始就想到要开longlong
不过在cf输入输出c++不能交%lld 新手的我实在不知道该怎么办只能按照他的提示改为定义int64
改为% I64d
AC代码:
#include<bits/stdc++.h>
#define ll __int64
using namespace std;
struct node {
ll val;
ll ii;
ll b;
} k[100005];
ll num;
ll n, m;
bool cmp(node aa, node bb) {
return (aa.val + aa.ii * aa.b) < (bb.val + bb.ii * bb.b);
}
int search(int mid) {
for(int i = 1; i <= n; i++) {
k[i].b = mid;
}
sort(k+1,k+1+n,cmp);
num = 0;
for(int i = 0; i <= mid; i++) {
num += k[i].val + k[i].b * k[i].ii;
if(num > m)
return 0;
}
if(num <= m)
return 1;
else
return 0;
}
int main() {
scanf("%I64d%I64d",&n,&m);
for(int i = 1; i <= n; i++) {
scanf("%I64d",&k[i].val);
k[i].ii = i;
}
int l = 0, r = n;
ll ans = -1;
ll ans2 = -1;
int mid;//中值
while(r - l >= 0) {
mid = (l + r) / 2;
if(search(mid) == 1) {
ans = mid;
ans2 = num;
l = mid + 1;
}
else {
r = mid - 1;
}
}
printf("%I64d %I64d",ans,ans2);
return 0;
}