Sagheer and Nubian Market 812c

原创 2018年04月16日 22:14:20

题目大意:

给你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;
}

codeforces 812 C. Sagheer and Nubian Market(二分答案)

题意: n个商品,有s块钱,买第i件商品的价格为a[i]+i*k,a[i]为商品的基础价格,k为总共要买的商品数量,问最多能买多少件商品,在买相同数量的商品情况下,尽量让花的钱最少。 解题思路: ...
  • johsnows
  • johsnows
  • 2017-06-02 17:20:35
  • 295

Codeforces812C Sagheer and Nubian Market

C. Sagheer and Nubian Market time limit per test 2 seconds memory limit per test 256 meg...
  • Jelly_acm
  • Jelly_acm
  • 2017-06-02 20:39:28
  • 512

C. Sagheer and Nubian Market

C. Sagheer and Nubian Market time limit per test 2 seconds memory limit per test 256 meg...
  • qq_33362864
  • qq_33362864
  • 2017-06-02 15:26:12
  • 274

Sagheer and Nubian Market(二分)

time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandar...
  • zhuanshunzhe
  • zhuanshunzhe
  • 2017-06-02 09:15:33
  • 397

codeforces 812C——Sagheer and Nubian Market(二分)

C. Sagheer and Nubian Market time limit per test 2 seconds memory limit per test 256 meg...
  • say_c_box
  • say_c_box
  • 2017-06-02 10:22:36
  • 445

Codeforces 812C-Sagheer and Nubian Market

Sagheer and Nubian Market time limit per test 2 seconds memory limit per test 256 megaby...
  • a664607530
  • a664607530
  • 2017-06-02 10:47:19
  • 325

Codeforces Round #417 (Div. 2) C. Sagheer and Nubian Market(二分查找)

C. Sagheer and Nubian Market time limit per test 2 seconds memory limit per test 256 meg...
  • HHH_go_
  • HHH_go_
  • 2017-06-02 18:56:08
  • 314

Codeforces 812C Sagheer and Nubian Market

二分买的产品的数量,排序,最小cost,简单题。#include #include #include #include #include #include #include #inclu...
  • Electrodeless_key
  • Electrodeless_key
  • 2017-09-26 15:09:56
  • 119

codeforces 812C Sagheer and Nubian Market

C. Sagheer and Nubian Market time limit per test 2 seconds memory limit per test 256 meg...
  • qq_37943488
  • qq_37943488
  • 2017-10-21 11:25:52
  • 136
收藏助手
不良信息举报
您举报文章:Sagheer and Nubian Market 812c
举报原因:
原因补充:

(最多只允许输入30个字)