【贪心+二分】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 ...
随机推荐
- neo4j(图数据库)是什么?
不多说,直接上干货! 作为一款强健的,可伸缩的高性能数据库,Neo4j最适合完整的企业部署或者用于一个轻量级项目中完整服务器的一个子集存在. 它包括如下几个显著特点: 完整的ACID支持 高可用性 轻 ...
- 解决IDEA Tomcat控制台乱码问题
1.在Tomcat Server的配置中添加一句命令: 神秘代码: -Dfile.encoding=UTF-8 重启Tomcat,ok. 如果还不行,则需要: 1.在Settings中修改文件编码 2 ...
- 013、BOM对象的应用
BOM结构图如下: DOM结构图如下: BOM和DOM BOM,Bowser Object Model浏览器对象模型.提供了访问和操作浏览器各组件的途径或方法. 比如:Navigator对象:浏览器的 ...
- MSDN值得学习的地方
作者:朱金灿 来源:http://blog.csdn.net/clever101 我一直认为:如果你没有乔布斯那样的天才,能够从头脑中原创出好产品,那么最好先学习分析好的产品,它到底好在哪里?哪些地方 ...
- MyBatis学习(四)
前言 最近比较松懈,下班回家后也懒得学习了.今晚实在是看不下去了,争取时间学习.社会上有这么多的资源,就看谁能抢的多吧.今天就说说MyBatis的动态SQL吧 正文 动态 SQL 通常要做的事情是有条 ...
- 【HEVC帧间预测论文】P1.4 Motion Vectors Merging: Low Complexity Prediction Unit Decision
Motion Vectors Merging: Low Complexity Prediction Unit Decision Heuristic for the inter-Prediction o ...
- 51nod 1272 最大距离
题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 收藏 关注 给出一个长度为N的整数数组A,对于每一个数组元素,如果他后面存在大于等 ...
- 配置nginx+tomcat支持websocket
问题情景: 最近开发新增加一个项目,需要支持https wss协议 访问https://test.aa.com 使用nginx反向代理到后端tomcat web应用 访问https://tes ...
- nodejs 安装 淘宝镜像
临时使用 npm --registry https://registry.npm.taobao.org install express 2.持久使用 npm config set registry h ...
- Dart开发环境搭建
一.SDK的安装与环境配置 1. 下载Dark SDK http://www.gekorm.com/dart-windows/ 2. 安装SDK 3. 配置环境变量(一般已经默认生成好了,这里可以 ...