CF812C 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 nn different items numbered from 11 to nn . The ii -th item has base cost a_{i}a**i Egyptian pounds. If Sagheer buys kk items with indices x_{1},x_{2},...,x_{k}x1,x2,...,x**k , then the cost of item x_{j}x**j is a_{xj}+x_{j}·kaxj+x**jk for 1<=j<=k1<=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 kk .

Sagheer wants to buy as many souvenirs as possible without paying more than SS 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 nn and SS ( 1<=n<=10^{5}1<=n<=105 and 1<=S<=10^{9}1<=S<=109 ) — the number of souvenirs in the market and Sagheer's budget.

The second line contains nn space-separated integers a_{1},a_{2},...,a_{n}a1,a2,...,a**n ( 1<=a_{i}<=10^{5}1<=a**i<=105 ) — the base costs of the souvenirs.

输出格式

On a single line, print two integers kk , TT — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these kk souvenirs.

题意翻译

题目描述

Sagheer在去Luxor和 Aswan的旅途中去了Nubian市场给它的朋友和亲戚买纪念品。这个市场有一些奇怪的规则。市场上有nn件商品,标号为11到nn。第ii件商品有一个基本花费为a_{i}a**i埃及镑。如果Sagheer买了kk件下标分别为x_{1},x_{2},...,x_{k}x1,x2,...,x**k的商品,那么第jj件商品的实际花费就是a_{xj}+x_{j}·k(1 <= j <= k)axj+x**jk(1<=j<=k)换句话说,每件商品的实际花费就是它的基本花费加上(它的下标×总购买件数)

Sagheer想花最多SS埃及镑来买尽量多的纪念品,注意它只能买同一种物品一件。如果有多种方式使纪念品数量最大,他会选择花费最小的方案。你能帮助他完成他的任务吗?

输入格式

第一行包含2个整数nn和SS (1 <= n <= 10^5(1<=n<=105且1 <= S <= 10^9)1<=S<=109)

第二行包含nn个以空格分开的整数a_{1},a_{2},...,a_{n}(1 <= a_{i} <= 10^5)a1,a2,...,a**n(1<=a**i<=105)

输出格式

一行,输出2个整数kk - Sagheer最大能买到的纪念品数量和TT - 最小Sagheer的总花费。

输入输出样例

输入 #1复制

输出 #1复制

输入 #2复制

输出 #2复制

输入 #3复制

输出 #3复制

说明/提示

In the first example, he cannot take the three items because they will cost him [5,9,14][5,9,14] with total cost 2828 . If he decides to take only two items, then the costs will be [4,7,11][4,7,11] . So he can afford the first and second items.

In the second example, he can buy all items as they will cost him [5,10,17,22][5,10,17,22] .

In the third example, there is only one souvenir in the market which will cost him 88 pounds, so he cannot buy it.

题解:

2019.11.1模拟赛T1 60分场

感谢出题人给我一次重新再来的机会改到了100分。

一开始看到这道题,往DP那里想了想,后来发现明显不行,因为这道题还需要统计最多拿多少件以及拿这些件的钱数。这些信息仅用DP是维护不了的。

然后考虑贪心。贪心思路是从\(n-1\)枚举,反复更新当前状态下所有物品的实际价值,然后排序判断合法与否,如果合法,那么当前状态一定是最优解,直接输出\(exit\)就好。

时间复杂度的话应该是\(O(n^2+nlogn)\)(把排序算上了)过不了全部数据,只能拿60分。

后来出题人@zcs0724友情提示是二分。

所以这道题一下子就显得没难度了。我们二分的对象是最多拿多少个(这是显然的,因为最多拿多少个决定了钱数)。那么我们二分的区间就是\(0-n\),注意要考虑0的情况(样例3不是特判过的)。然后\(check()\)函数的写法思路是和暴力思路一样的。退出二分的时候统计当前状态(最优解状态)的答案即可。

简单总结一下,我们的暴力思路是枚举判断,正解思路是二分判断,显然二分可以优化枚举的效率。这道题也是因此而把复杂度降到了log级别,然后得以AC的。所以与其把这道题说成贪心+二分,还不如说成暴力枚举的优化。(当初和七哥@littleseven讨论的时候说起了Log级别的算法,硬是没想到二分,我太菜了)

代码:

#include<cstdio>
#include<algorithm>
#define int long long
using namespace std;
const int maxn=1e5+1;
int n,s,temp,ans;
int l,r;
struct node
{
int a,t,order;
}p[maxn];
bool cmp(node a,node b)
{
return a.t<b.t;
}
bool check(int x)
{
temp=0;
for(int j=1;j<=n;j++)
p[j].t=p[j].a+p[j].order*x;
sort(p+1,p+n+1,cmp);
for(int j=1;j<=x;j++)
temp+=p[j].t;
if(temp<=s)
return 1;
return 0;
}
signed main()
{
scanf("%lld%lld",&n,&s);
for(int i=1;i<=n;i++)
{
scanf("%lld",&p[i].a);
p[i].order=i;
}
l=0,r=n;
while(l<r)
{
int mid=(l+r+1)/2;
if(check(mid))
l=mid;
else
r=mid-1;
}
printf("%lld ",l);
for(int i=1;i<=n;i++)
p[i].t=p[i].a+p[i].order*l;
sort(p+1,p+n+1,cmp);
for(int i=1;i<=l;i++)
ans+=p[i].t;
printf("%lld",ans);
return 0;
}

CF812C Sagheer and Nubian Market的更多相关文章

  1. CF812C Sagheer and Nubian Market 二分+贪心

    模拟赛给他们出T1好了~ code: #include <bits/stdc++.h> #define ll long long #define N 100006 #define setI ...

  2. 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 ...

  3. Codeforces Round #417 C. Sagheer and Nubian Market

    C. Sagheer and Nubian Market time limit per test  2 seconds memory limit per test  256 megabytes   O ...

  4. AC日记——Sagheer and Nubian Market codeforces 812c

    C - Sagheer and Nubian Market 思路: 二分: 代码: #include <bits/stdc++.h> using namespace std; #defin ...

  5. Codeforces J. Sagheer and Nubian Market(二分枚举)

    题目描述: Sagheer and Nubian Market time limit per test 2 seconds memory limit per test 256 megabytes in ...

  6. CodeForce-812C Sagheer and Nubian Market(二分)

    Sagheer and Nubian Market CodeForces - 812C 题意:n个货物,每个货物基础价格是ai. 当你一共购买k个货物时,每个货物的价格为a[i]+k*i. 每个货物只 ...

  7. Sagheer and Nubian Market CodeForces - 812C (二分)

    On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friend ...

  8. Codeforces Round #417 (Div. 2) C. Sagheer and Nubian Market

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  9. CodeForces - 812C Sagheer and Nubian Market 二分

    On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friend ...

随机推荐

  1. Day6 - Python基础6 模块shelve、xml、re、subprocess、pymysql

    本节目录: 1.shelve模块 2.xml模块 3.re模块 4.subprocess模块 5.logging模块 6.pymysql 1.shelve 模块 shelve模块是一个简单的k,v将内 ...

  2. 算法设计与分析 1.1 Joyvan的矩阵

    ★题目描述 Joyvan有一个大小为n * m的矩阵,现在他要对矩阵进行q次操作,操作分为如下三种: 0 x y:交换矩阵的x.y两行. 1 x y:交换矩阵的x.y两列. 2 x y:求当前矩阵第x ...

  3. MySQL多表查询综合练习答案

    目录 一.综合练习 1.1 init.sql文件内容 1.2 从init.sql文件中导入数据 1.3 基础练习 1.4 进阶练习 二.基础练习答案 三.进阶练习答案 一.综合练习 1.1 init. ...

  4. Python连载52-SOCKET编程示例

    一.UDP编程 1.客户端Client:发起访问的一方. 2.服务器端 3.server段编程 (1)建立socket,socket是负责具体通信的一个实例 (2)绑定,为创建的socket指派固定的 ...

  5. 【TCP/IP网络编程】:02套接字类型与协议设置

    本篇文章主要介绍创建套接字函数相关的3个输入参数的含义,它们最终确定了套接字通信所采用的协议.同时,也简单对比了TCP和UDP传输方式的区别. 什么是协议?协议是对话中使用的通信规则,而在计算机领域则 ...

  6. laravel集成workerman,使用异步mysql,redis组件时,报错EventBaseConfig::FEATURE_FDS not supported on Windows

    由于laravel项目中集成了workerman,因业务需要,需要使用异步的mysql和redis组件. composer require react/mysql composer require c ...

  7. laravel中控制器的创建和使用(五)

    laravel中我们可以使用 artisan 命令来帮助我们创建控制器文件. php artisan make:controller TestController TestController 控制器 ...

  8. Jenkins 有关 Maven 的内容

    Jenkins Maven 插件安装 在安装完 Jenkins 后,我们想添加新的项目 为 Maven 项目时,发现找不到这个选项. 原因是我们没有安装插件 Maven Integration. 在 ...

  9. fastjson的值过滤器ValueFilter

    https://blog.csdn.net/linyifan_/article/details/83060408 原创林天乐 发布于2018-10-15 16:20:25 阅读数 1462  收藏 展 ...

  10. Revit二次开发 屏蔽复制构件产生的重复类型提示窗

    做了很久码农,也没个写博客的习惯,这次开始第一次写博客. 这个问题也是折腾了我接近一天时间,网上也没有任何的相关博文,于是决定分享一下,以供同样拥有此问题的小伙伴们参考. 内容源于目前在做的一个项目, ...