Codeforces Round #417 C. 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 buysk items 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?
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.
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these ksouvenirs.
3 11
2 3 5
2 11
4 100
1 2 5 6
4 54
1 7
7
0 0
Note
In the first example, he cannot take the three items because they will cost him [, , ] with total cost . If he decides to take only two items, then the costs will be [, , ]. So he can afford the first and second items. In the second example, he can buy all items as they will cost him [, , , ]. In the third example, there is only one souvenir in the market which will cost him pounds, so he cannot buy it.
Note
题目大意:
在一个商店里一共有n件商品,每一件都有一个基础价格,但是这个商店有一个奇怪的规定,
每一件商品最后的价格为 基础价格+买的商品总件数*商品的坐标(即这是第几件商品)
现给定 商品的件数和你所拥有的钱数, 问你最多能卖几件商品 花的总钱数是多少 解题思路:
总体思路为:二分查找 先找出最多能买的商品的件数,然后在计算出总花费 AC代码:
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll; int main ()
{
ll n,s,sum,x,y,l,r;
ll ans[],a[];
int i;
cin>>n>>s;
for (i = ; i <= n; i ++)
cin>>a[i]; l = ,r = n; // 左标签和右标签
x = y = ;
while (l <= r){
sum = ;
ll mid = (r+l)>>; // 二分枚举能购买的商品件数
for (i = ; i <= n; i ++)
ans[i] = a[i]+i*mid;
sort(ans+,ans++n);
for (i = ; i <= mid; i ++)
sum += ans[i];
if (sum <= s){
x = mid;
y = sum;
l = mid+;
}
else
r = mid-;
}
cout<<x<<" "<<y<<endl;
return ;
}
以下版本思路同上;
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; int main ()
{
__int64 n,s;
__int64 ans[],a[];
int i;
while (~scanf("%I64d%I64d",&n,&s))
{
for (i = ; i <= n; i ++)
scanf("%I64d",&a[i]); int l = ,r = n;
__int64 sum,x=,y=;
while (l <= r){
sum = ;
long long mid = (l+r)>>;
for (i = ; i <= n; i ++)
ans[i] = a[i]+i*mid;
sort(ans+,ans++n);
for (i = ; i <= mid; i ++)
sum += ans[i];
if (sum > s)
r = mid-;
else
l = mid+;
}
for (i = ; i <= n; i ++)
ans[i] = a[i]+i*r;
sort(ans+,ans++n);
for (i = ; i <= r; i ++)
y += ans[i];
printf("%d %I64d\n",r,y);
}
return ;
}
Codeforces Round #417 C. Sagheer and Nubian Market的更多相关文章
- Codeforces Round #417 B. Sagheer, the Hausmeister
B. Sagheer, the Hausmeister time limit per test 1 second memory limit per test 256 megabytes Som ...
- AC日记——Sagheer and Nubian Market codeforces 812c
C - Sagheer and Nubian Market 思路: 二分: 代码: #include <bits/stdc++.h> using namespace std; #defin ...
- Codeforces J. Sagheer and Nubian Market(二分枚举)
题目描述: Sagheer and Nubian Market time limit per test 2 seconds memory limit per test 256 megabytes in ...
- CodeForce-812C Sagheer and Nubian Market(二分)
Sagheer and Nubian Market CodeForces - 812C 题意:n个货物,每个货物基础价格是ai. 当你一共购买k个货物时,每个货物的价格为a[i]+k*i. 每个货物只 ...
- [Codeforces Round#417 Div.2]
来自FallDream的博客,未经允许,请勿转载,谢谢. 有毒的一场div2 找了个1300的小号,结果B题题目看错没交 D题题目剧毒 E题差了10秒钟没交上去. 233 ------- A.Sag ...
- Codeforces812C Sagheer and Nubian Market 2017-06-02 20:39 153人阅读 评论(0) 收藏
C. Sagheer and Nubian Market time limit per test 2 seconds memory limit per test 256 megabytes input ...
- Codeforces Round #417 (Div. 2)A B C E 模拟 枚举 二分 阶梯博弈
A. Sagheer and Crossroads time limit per test 1 second memory limit per test 256 megabytes input sta ...
- Codeforces Round #417 (Div. 2) 花式被虐
A. Sagheer and Crossroads time limit per test 1 second memory limit per test 256 megabytes input sta ...
- codeforces round 417 div2 补题 CF 812 A-E
A Sagheer and Crossroads 水题略过(然而被Hack了 以后要更加谨慎) #include<bits/stdc++.h> using namespace std; i ...
随机推荐
- GoLand 调试 Go
Goland 调试 Go 从百度得知 VS Code 不能很好的支持 Go 的调试真让人肝儿疼 -- 引言 准备 Win 10 Pro Go(Version 1.10) GoLand(2018.3) ...
- python的socket.recv函数陷阱
目录 前言 一个粘包实验 执行结果 排错思路 解决和总结 前言 惯例练习历史实验,在编写tcp数据流粘包实验的时候,发现一个奇怪的现象.当远程执行的命令返回结果很短的时候可以正常执行,但返回结果很长时 ...
- docker 日志管理
高效的监控和日志管理对保持生产系统持续稳定地运行以及排查问题至关重要. 在微服务架构中,由于容器的数量众多以及快速变化的特性使得记录日志和监控变得越来越重要.考虑到容器短暂和不固定的生命周期,当我们需 ...
- 事务控制语句,begin,rollback,savepoint,隐式提交的SQL语句
事务控制语句 在MySQL命令行的默认设置下,事务都是自动提交的,即执行SQL语句后就会马上执行COMMIT操作.因此开始一个事务,必须使用BEGIN.START TRANSACTION,或者执行SE ...
- Android studio--几个生成文件的区别:Make Project、Make Module、Build apk、Signed apk
参考资料: https://stackoverflow.com/questions/35334319/difference-between-make-project-make-module-app-b ...
- Android中数据的保存
1. 概述 在Android中有以下几种保存数据方式: 1). 使用sharedPreference去保存:只有应用程序自己可以访问 2). 保存在应用程序私有的文件夹下:只有应用程序自己可以访问 3 ...
- JavaScript的原型链继承__propt__、prototype、constructor的理解、以及他们之间相互的关系。
回想自己已经工作了有一段时间了,但是自己对JavaScript的原型链.和继承的理解能力没有到位,最近他们彻底的整理并且复习了一遍. 本案例中部分文案来自网络和书籍,如有侵权请联系我,我只是把我的理解 ...
- wpf 窗口最小化后,触发某事件弹出最小化窗口并置顶
//如果窗口最小化了弹出并置顶----事件触发调用 ShowWindowAsync(new System.Windows.Interop.WindowInteropHelper(CommonHelpe ...
- 如鹏网学习笔记(五)MySql基础
MySQL基础 一.数据库概念 1,网友装备信息.论坛帖子信息.QQ好友关系信息.学籍管理系统中的学生信息等都要“持久化”的保存到一个地方, 如果通过IO写到文件中,那么会非常麻烦,而且不利于多人共享 ...
- groovy普通方法、抽象方法、接口、trait
/** * Created by Jxy on 2018/12/21 14:07 * trait关键字 * 声明trait中的方法和任何常规方法一样 * trait声明抽象方法需要在实现类中实现 * ...