【贪心+二分】codeforces C. Sagheer and Nubian Market
http://codeforces.com/contest/812/problem/C
【题意】
如何花最少的钱买最多的纪念品?首要满足纪念品尽可能多,纪念品数量一样花钱要最少,输出纪念品数量以及最少花费。
纪念品的价钱是这么定义的:
,其中a是基价,k是总共要买的纪念品数量,x是纪念品的index。
题目给出各个纪念品的基价a(当然,x也随之确定)
【思路】
二分纪念品数量,判断是否满足题意直接贪心,O(n)算出每个纪念品的价钱,O(nlogn)排序,选出最小的mid个;
二分时间复杂度O(n).
所以总的时间复杂度是O(nlogn^2),1000 00刚好满足
【Accepted】
#include <iostream>
#include <stdio.h>
#include <cmath>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <bitset>
#include <ctime>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=1e5+;
struct souv
{
int base;
int x;
}a[maxn];
int n,S;
typedef long long ll;
int judge(int mid)
{
ll b[maxn];
for(int i=;i<=n;i++)
{
b[i]=(ll)a[i].base+(ll)a[i].x*(ll)mid;
}
sort(b+,b+n+);
ll sum=;
for(int i=;i<=mid;i++)
{
sum+=b[i];
}
if(sum<=(ll)S)
{
return sum;
}
return -; }
int main()
{
while(~scanf("%d%d",&n,&S))
{
for(int i=;i<=n;i++)
{
scanf("%d",&a[i].base);
a[i].x=i;
}
int l=;
int r=n;
int res=S;
while(l<=r)
{
int mid=(l+r)>>;
int ans=judge(mid);
if(ans!=-)
{
res=ans;
l=mid+;
}
else
{
r=mid-;
}
}
cout<<r<<" "<<res<<endl;
}
return ;
}
二分
【贪心+二分】codeforces C. Sagheer and Nubian Market的更多相关文章
- Codeforces J. Sagheer and Nubian Market(二分枚举)
题目描述: Sagheer and Nubian Market time limit per test 2 seconds memory limit per test 256 megabytes in ...
- 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 ...
- CodeForce-812C Sagheer and Nubian Market(二分)
Sagheer and Nubian Market CodeForces - 812C 题意:n个货物,每个货物基础价格是ai. 当你一共购买k个货物时,每个货物的价格为a[i]+k*i. 每个货物只 ...
- 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 ...
- AC日记——Sagheer and Nubian Market codeforces 812c
C - Sagheer and Nubian Market 思路: 二分: 代码: #include <bits/stdc++.h> using namespace std; #defin ...
- CF812C Sagheer and Nubian Market
CF812C Sagheer and Nubian Market 洛谷评测传送门 题目描述 On his trip to Luxor and Aswan, Sagheer went to a Nubi ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- 前端之CSS语法及选择器
一.css语法: css由两大部分组成:选择符和声明,声明由属性和属性值两部分组成; 选择符{属性:属性值;属性:属性值;} 注: a) 属性和属性值之间用冒号连接: b)每条声明结束要加分号: 二. ...
- 【学习笔记】深入理解js原型和闭包(15)——闭包
前面提到的上下文环境和作用域的知识,除了了解这些知识之外,还是理解闭包的基础. 至于“闭包”这个词的概念的文字描述,确实不好解释,我看过很多遍,但是现在还是记不住. 但是你只需要知道应用的两种情况即可 ...
- FPGA原型验证
为什么要做FPGA原型验证? FPGA原型验证可以在IC流片前对芯片功能和性能做出评估,同时,可以给软件设计人员提供验证平台.所有的设计,无论是SOC还是ASIC都需要被验证(功能和时序验证),以确保 ...
- .net4.5注册到iis
开始->所有程序->附件->鼠标右键点击“命令提示符”->以管理员身份运行->%windir%\Microsoft.NET\Framework\v4.0.30319\as ...
- 项目中常用git命令操作指令(一般正常的话够用不够再看相关git命令)
配置git1.首先在本地创建ssh key:ssh-keygen -t rsa -C "github上注册的邮箱" //(一路回车)2.进入c:/Users/xxxx_000/.s ...
- 在Oracle用SQL处理以 System.currentTimeMillis
有時為了系統的需求會紀錄到毫秒(Millisecond),我們會接將得到的值寫入db,但是如果要用SQL 做時間範圍的搜尋,有以下做法( systemdate欄位存放System.currentTim ...
- 2019年今日头条机试_JAVA后台岗_第一题
广度优先遍历: import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; class Nod ...
- docker的网络配置
Docker的4种网络模式 我们在使用docker run创建Docker容器时,可以用–net选项指定容器的网络模式,Docker有以下4种网络模式: host模式:使用–net=host指定. c ...
- mysql登录(linux)
一.修改配置文件 查询路径 find / -name my.cnf 修改文件 vi my.cnf [mysqld]下面加上skip-grant-tables 二.登录数据库 重启mysql servi ...
- 目录下 shift 右键菜单 打开cmd 或者在 地址栏输入cmd 回车进入cmd
目录下 shift 右键菜单 打开cmd 或者在 地址栏输入cmd 回车进入cmd