#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 ...
随机推荐
- AspNet Core Swagger4.0 生成请求model描述
今天给大家分享 swagger 俩个冷门的小技巧 获取控制器描述 将 IncludeXmlComments 方法第二个参数设置为 true 即可 public static void IncludeX ...
- Java学习开发第一阶段总结
前言: 按照学院的安排我专业应该在下学期学习Java课程,因为对技术的热爱,我选择了在本学期学习Java.俗话说得好“笨鸟先飞”,那我就先学习这门课程了. 第一阶段的学习总结: 在此次阶段任务相对比较 ...
- [转帖]SSH命令总结
SSH命令总结 ssh-keygen ssh-copy-id 等命令自己用过 但是知道的不系统 也知道 转发命令 但是也只是知道一点点... ttps://www.cnblogs.com/chenfa ...
- GITFLOW流程
GITFLOW流程规范 GIT的使用非常的灵活,但是灵活就导致在使用的过程中有各种各样的情况,根据现有项目组的情况,使用GITFLOW流程规范作为项目开发流程规范. 该规范参考地址: 深入理解学习Gi ...
- windows 2008 创建域服务器问题 账户密码不符合要求
windows 2008新建域时,本地administrator账户将成域Administrator账户.无法新建域,因为本地administrator账户密码不符合要求.*解决办法:很多人都会想到在 ...
- PropertyUtilsBean 将bean转成map
public static Map<String,String> beanToMap(Object bean) { Map<String,String> params =Map ...
- QtSpim使用Tips
QtSpim使用记录 垃圾QtSpim,输入中文会死机 MIPS的中文资料奇缺,如果有问题建议google参考英文资料,许多外国大学的网站上有对MIPS各方面的详细介绍 QtSpim是MIPS处理器的 ...
- IntelliJ IDEA 2017 提示“Unmapped Spring configuration files found.Please configure Spring facet.”解决办法
当把自己的一个项目导入IDEA之后,Event Log提示“Unmapped Spring configuration files found.Please configure Spring face ...
- iconv_open - 分配一个字符集转换的描述符
总览 (SYNOPSIS) #include <iconv.h> iconv_t iconv_open (const char* tocode, const char* fromcode) ...
- http知识总结
layout: '''http' title: 知识整理' date: 2019-06-09 17:07:20 tags: --- 简介 超文本传输协议(HTTP)是用于传输诸如HTML的超媒体文 ...