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 ...
随机推荐
- 【算法笔记】A1063 Set Similarity
1063 Set Similarity (25 分) Given two sets of integers, the similarity of the sets is defined to be ...
- Openerp 添加修改报表
Report Designer 模块在生成新报表的时候是有BUG的不建议直接使用,不过我们也可以通过该插件再写简单的代码来实现新添加报表,插件安装成功后我们可以按照下列方法来添加报表 OpenERP ...
- Windows Git Bash命令行下创建git仓库并更新到github
大二的时候就听过老师说有一个叫git的版本管理工具,当时只是听老师说说而已,也没有去使用它,因为当时用过svn,就感觉自己没多少东西需要git管理. 最近几天,我经常在开源中国看别人的帖子,看到别人对 ...
- 四大组件之Service
1 通过startService 1.1 与调用者相对独立,没有返回,用于播放音乐,下载文件 1.2 如果是调用者自己直接退出而没有调用stopService的话,Service会一直在后台运行.下次 ...
- 大数据搭建各个子项目时配置文件技巧(适合CentOS和Ubuntu系统)(博主推荐)
不多说,直接上干货! 很多同行,也许都知道,对于我们大数据搭建而言,目前主流,分为Apache 和 Cloudera 和 Ambari. 后两者我不多说,是公司必备和大多数高校科研环境所必须的! 分别 ...
- Logback 学习笔记
来源:http://webinglin.github.io/2015/06/04/Logback-%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/ Logback背景 Log ...
- getSqlMapClientTemplate().insert()方法的返回值问题
insert方法的返回值 今天碰到一个问题,就是关于ibatis的insert方法的返回值的问题.在网上找了很多例子,自己也亲自试了一下. 最后得出结论:insert方法返回的是在表中插入记录 ...
- 第七章、Linux 文件与目录管理
第七章.Linux 文件与目录管理 1. 目录与路径 1.1 相对路径与绝对路径 1.2 目录的相关操作: cd, pwd, mkdir, rmdir 1.3 关於运行档路径的变量: $PATH ...
- oracle 树状结构递归 PL/SQL输出控制 包括空格输出控制
树状结构 存储过程中通过递归构建,类似BBS回帖显示,代码共三段: 建表,插入数据,创建存储过程显示: 1.create table article(id number primary key,con ...
- Oracle中的自连接(self join)-当表中的某一个字段与这个表中另外字段的相关时,我们可能用到自连接。
http://blog.163.com/wkyuyang_001/blog/static/10802122820091751049479/ 当表中的某一个字段与这个表中另外字段的相关时,我们可能用到自 ...