#417 Div2 Problem C Sagheer and Nubian Market (二分 && std::accumulate)
题目链接 : http://codeforces.com/problemset/problem/812/C
题意 : 给你 n 件物品和你拥有的钱 S, 接下来给出这 n 件物品的价格, 这些物品的价值不是固定不变的, 价格的变化公式是 a[i]+k*i (i代表第 i 件物品, k 代表你选择买的物品数量, a[i]为物品的底价), 现问你最多能够买多少件物品和所买物品总和, 输出时应该使得所买物品总和尽量小
分析 : 如果我当前能买 k 件物品, 那我肯定能买数量小于 k 的物品, 如果我当前买不起 k 件物品, 那我肯定也不能买比 k 件要多的物品。所以可以考虑二分解法, 在1~n之间二分查找 k, 这里需要注意的是, 在二分的过程中应该需要对当前的价格进行更新和排序, 才能保证最后输出的物品总和尽量小!
技巧 : 这里有需要计算数组前 k 个的和, 可以考虑使用 std::accumulate(begin, end, base), 代表从数组的arr[begin]加到arr[end]的和再加上base, 也就是在base的基础上求arr数组的begin~end的和, 这里有详细介绍(值得一提的是复杂度是 O(n) ) http://classfoo.com/ccby/article/Y749fK
std::accumulate函数应用举例 :
#include <vector>
#include <numeric>
#include <functional>
#include <iostream>
using namespace std;
int main( )
{
vector < );
vector <int>::iterator Iter1, Iter2;
int i;
; i < ; i++ )
{
v1.push_back( i );
}
cout << "最初向量v1中个元素的值为:\n ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")." << endl;
// accumulate函数的第一个功能,求和
int total;
total = accumulate ( v1.begin ( ) , v1.end ( ) , );
cout << "整数从1到20的和为: "
<< total << "." << endl;
// 构造一个前n项和的向量
, partotal;
; Iter1 != v1.end( ) + ; Iter1++ )
{
partotal = accumulate ( v1.begin ( ) , Iter1 , );
v2 [ j ] = partotal;
j++;
}
cout << "前n项和分别为:\n ( " ;
for ( Iter2 = v2.begin( ) ; Iter2 != v2.end( ) ; Iter2++ )
cout << *Iter2 << " ";
cout << ")." << endl << endl;
// accumulate函数的第二个功能,计算连乘积
vector < );
vector <int>::iterator Iter3, Iter4;
int s;
; s < ; s++ )
{
v3.push_back( s );
}
cout << "向量v3的初始值分别为:\n ( " ;
for ( Iter3 = v3.begin( ) ; Iter3 != v3.end( ) ; Iter3++ )
cout << *Iter3 << " ";
cout << ")." << endl;
int ptotal;
ptotal = accumulate ( v3.begin ( ) , v3.end ( ) , , multiplies<int>( ) );
cout << "整数1到10的连乘积为: "
<< ptotal << "." << endl;
// 构造一个前n项积的向量
, ppartotal;
; Iter3 != v3.end( ) + ; Iter3++ ) {
ppartotal = accumulate ( v3.begin ( ) , Iter3 , , multiplies<int>( ) );
v4 [ k ] = ppartotal;
k++;
}
cout << "前n项积分别为:\n ( " ;
for ( Iter4 = v4.begin( ) ; Iter4 != v4.end( ) ; Iter4++ )
cout << *Iter4 << " ";
cout << ")." << endl;
}
瞎想 : 一开始是在想是否能用背包做, 事实证明在S那个数据量下是不可能的, 而且背包的话, 这里要求的是总和尽量小。
#include<bits/stdc++.h>
#define LL long long
using namespace std;
;
LL arr[maxn], change[maxn];
LL n, s;
LL cal(LL k)
{
; i<=n; i++) change[i] = i*k+arr[i];
sort(change+, change++n);
LL ans = accumulate(change+, change++k, 0LL);
return ans;
}
int main(void)
{
scanf("%lld %lld", &n, &s);
; i<=n; i++)
scanf("%lld", &arr[i]);
LL L = , R = n, mid;
while(L<=R){
mid = L + ((R-L)>>);
LL sum = cal(mid);
;
;
}
){
puts("0 0");
;
}
L--;
printf("%lld %lld\n", L, cal(L));
;
}
#417 Div2 Problem C Sagheer and Nubian Market (二分 && std::accumulate)的更多相关文章
- CodeForce-812C Sagheer and Nubian Market(二分)
Sagheer and Nubian Market CodeForces - 812C 题意:n个货物,每个货物基础价格是ai. 当你一共购买k个货物时,每个货物的价格为a[i]+k*i. 每个货物只 ...
- 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 ...
- 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 ...
- #417 Div2 Problem B Sagheer, the Hausmeister (DFS && 枚举)
题目链接:http://codeforces.com/contest/812/problem/B 题意 : 给出一个 n (1 ≤ n ≤ 15)层的教学楼, 每一层楼包含 m (1 ≤ m ≤ 10 ...
- 【二分】Codeforces Round #417 (Div. 2) C. Sagheer and Nubian Market
傻逼二分 #include<cstdio> #include<algorithm> using namespace std; typedef long long ll; ll ...
- CF812C Sagheer and Nubian Market 二分+贪心
模拟赛给他们出T1好了~ code: #include <bits/stdc++.h> #define ll long long #define N 100006 #define setI ...
- 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 ...
- 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 ...
- AC日记——Sagheer and Nubian Market codeforces 812c
C - Sagheer and Nubian Market 思路: 二分: 代码: #include <bits/stdc++.h> using namespace std; #defin ...
随机推荐
- 20191224 Spring官方文档(Core 1.1-1.4)
1. IoC容器 1.1.Spring IoC容器和Bean简介 org.springframework.beans和org.springframework.context包是Spring框架的IoC ...
- 【转】mysql的group_concat函数,默认最大长度是1024
mysql的group_concat函数,默认最大长度是1024 查询sql: show variables like 'group_concat_max_len'; 设置方式: 修改配置文件my.i ...
- 一篇文章看懂Java并发和线程安全(一)
一.前言 长久以来,一直想剖析一下Java线程安全的本质,但是苦于有些微观的点想不明白,便搁置了下来,前段时间慢慢想明白了,便把所有的点串联起来,趁着思路清晰,整理成这样一篇文章. 二.导读 1.为什 ...
- 中值滤波器(平滑空间滤波器)基本原理及Python实现
1. 基本原理 一种典型的非线性滤波器就是中值滤波器,它使用像素的一个领域内的灰度的中值来代替该像素的值.中值滤波器通常是处理椒盐噪声的一种有效的手段. 2. 测试结果 图源自skimage 3. 代 ...
- selenium谷歌火狐插件安装
1.首先ctrl+r进入终端输入(pip install selenium)进行python安装selenium2.打开百度浏览器进行分别输入geckodriver和Chromedriver对火狐和谷 ...
- 如何使用前端分页框架bootstrap paginator
前端分页框架bootstrap paginator用于web前端页面快速实现美观大方的翻页功能.在实现交互良好的页面翻页功能时,往往还需要配合使用后端分页框架pagehelper.pagehelper ...
- vnd.ms-excel.numberformat 导出Ecxel 格式
<td style="vnd.ms-excel.numberformat:@;"><s:property value="accountCode" ...
- 数据库备份及SQL脚本导入
数据库备份及SQL脚本导入 数据导出 su - oracle exp 数据库用户名/数据库密码@ORCL file=20190905.dmp full=y SQL脚本导入 首先导入前查看Oracle用 ...
- 【c#】ADO操作Access的mdb数据库只能读不能修改的解决方法
在使用ACCESS数据库时连接字符串如 string strcon=@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\Access操作\简易 ...
- Stanford CS229 Machine Learning by Andrew Ng
CS229 Machine Learning Stanford Course by Andrew Ng Course material, problem set Matlab code written ...