HackerRank "Playing with numbers"
This is 'Difficult' - I worked out it within 45mins, and unlocked HackerRank Algorithm Level 80 yeah!
So the idea is straight forward:
1. sort the input array and calculate partial_sum()
2. find the negative\positive boundary with the accumulated given offset
Note: in C++ you have to use 'long long' type all the way. I will switch to Python later..
#include <cmath>
#include <cstdio>
#include <vector>
#include <queue>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
using namespace std; int main()
{
int N, Q; // Get Array
cin >> N;
vector<long long> v(N);
for(int i = ; i < N; i ++)
cin >> v[i]; // Some processing
sort(v.begin(), v.end());
vector<long long> pre(N);
partial_sum(v.begin(), v.end(), pre.begin()); // Go Query
long long off = ;
cin >> Q;
while(Q--)
{
long long tmp; cin >> tmp;
off += tmp; auto it = lower_bound(v.begin(), v.end(), -off);
long long cnt_neg = it - v.begin();
long long cnt_pos = N - cnt_neg; long long sum_neg = llabs(off * cnt_neg + pre[cnt_neg - ]);
long long sum_pos = llabs(off * cnt_pos + pre.back() - pre[cnt_neg - ]); cout << (sum_neg + sum_pos) << endl;
}
return ;
}
HackerRank "Playing with numbers"的更多相关文章
- 【HackerRank】Missing Numbers
Numeros, The Artist, had two lists A and B, such that, B was a permutation of A. Numeros was very pr ...
- 【HackerRank】Closest Numbers
Sorting is often useful as the first step in many different tasks. The most common task is to make f ...
- Codeforces Round #114 (Div. 1) C. Wizards and Numbers 博弈论
C. Wizards and Numbers 题目连接: http://codeforces.com/problemset/problem/167/C Description In some coun ...
- Asia Hong Kong Regional Contest 2016
A. Colourful Graph 可以在$2n$步之内实现交换任意两个点的颜色,然后就可以构造出方案. #include <bits/stdc++.h> using namespace ...
- Codechef April Challenge 2019 游记
Codechef April Challenge 2019 游记 Subtree Removal 题目大意: 一棵\(n(n\le10^5)\)个结点的有根树,每个结点有一个权值\(w_i(|w_i\ ...
- Codechef April Challenge 2019 Division 2
Maximum Remaining 题意:给n个数,取出两个数$a_{i}$,$a_{j}$,求$a_{i}\% a_{j}$取模的最大值 直接排个序,第二大(严格的第二大)模第一大就是答案了. #i ...
- 【Code Chef】April Challenge 2019
Subtree Removal 很显然不可能选择砍掉一对有祖先关系的子树.令$f_i$表示$i$子树的答案,如果$i$不被砍,那就是$a_i + \sum\limits_j f_j$:如果$i$被砍, ...
- Mastering Creativity:A brief guide on how to overcome creative blocks
MASTERING CREATIVITY, 1st EditionThis guide is free and you are welcome to share it withothers.From ...
- CodeChef April Challenge 2019题解
传送门 \(Maximum\ Remaining\) 对于两个数\(a,b\),如果\(a=b\)没贡献,所以不妨假设\(a<b\),有\(a\%b=a\),而\(b\%a<a\).综上, ...
随机推荐
- CoreOS 835.12.0 稳定版安装
导读 CoreOS是一个基于Docker的轻量级容器化Linux发行版,为Docker而生,CoreOS作为Docker生态圈中的重要一员,日益得到各大云服务商的重视,发展风头正劲. CoreOS宣称 ...
- 268. Missing Number
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- php部分---函数、四类常用函数、例子(下拉菜单添加内容);
1.简单函数 四要素:返回类型,函数名,参数列表,函数体 function Show() { echo "hello"; } Show(); 2.有返回值的函数 function ...
- hdu1058丑数(优先队列、暴力打表)
hdu1058 题意:当一个数只有2.3.5.7这四种质因数时(也可以一种都没有或只有其中几种),这个数就是丑数,输出第 n 个丑数是多少: 其实并没有发现hdu把这道题放在 dp 专题里的意图,我的 ...
- MySQL中日期与字符串相互转换,并进行日期比较查询
技术交流群:233513714 1.日期无需转换查询(日期在数据库中的类型为字符串) select * from day where dateTime > '2016-03-15' 2.使用da ...
- 简单工厂模式(Simple Factory Pattern)
简单工厂模式是属于创建型模式,又叫做静态工厂方法(Static Factory Method)模式,但不属于23种GOF设计模式之一.简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实例.简单工厂 ...
- 论文笔记之:From Facial Parts Responses to Face Detection: A Deep Learning Approach
From Facial Parts Responses to Face Detection: A Deep Learning Approach ICCV 2015 从以上两张图就可以感受到本文所提方法 ...
- 学习笔记之 初试Linux遇到的问题 2015-10-13
1. 安装.deb文件,用sudo gdebi XXX.deb sudo apt-get install xxx 2. 需要配置系统路径: LD_LIBRARY_PATH=.../lib:LD_LIB ...
- fs event_socket
mod_event_socket Skip to end of metadata Created by John Boteler, last modified by Niek Vlesse ...
- C#中调用C++的dll的参数为指针类型的导出函数(包括二级指针的情况)
严格来说这篇文章算不上C++范围的,不过还是挂了点边,还是在自己的blog中记录一下吧. C++中使用指针是家常便饭了,也非常的好用,这也是我之所以喜欢C++的原因之一.但是在C#中就强调托管的概念了 ...