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\).综上, ...
随机推荐
- PCV 学习笔记-ch1 主成分分析实现
模块名称:pca.py PCA原理与紧致技巧原理待补... #-*-coding:UTF-8-*- ''' Created on 2015年3月2日 @author: Ayumi Phoenix ch ...
- FHS目录配置下,常见的几个问题及解答
请说明/bin与/usr/bin目录所放置的执行文件有何不同之处? /bin主要放置在开机时,以及进入单人维护模式后还能够被使用的指令,至于/usr/bin则是大部分软件提供的指令放置处. 请说明/b ...
- 转:struts标签之select详解
<html:select>生成HTML<select>元素 <html:option>:生成HTML<option>元素 <html:option ...
- Oracle数据库Linux下的导出EXP
先转一篇 ================================我是分割线================================ 时间:2013-06-22 13:48来源:未知 ...
- leetcode 114 Flatten Binary Tree to Linked List ----- java
Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ...
- 笨小猴 2008年NOIP全国联赛提高组
题目描述 Description 笨小猴的词汇量很小,所以每次做英语选择题的时候都很头疼.但是他找到了一种方法,经试验证明,用这种方法去选择选项的时候选对的几率非常大! 这种方法的具体描述如下:假设m ...
- Jewelry Exhibition(最小点覆盖集)
Jewelry Exhibition 时间限制: 1 Sec 内存限制: 64 MB提交: 3 解决: 3[提交][状态][讨论版] 题目描述 To guard the art jewelry e ...
- 利用CSS的@font-face属性 在网页中嵌入字体
字体使用是网页设计中不可或缺的一部分.网页是文字的载体,我们希望在网页中使用某一特定字体,但是该字体并非主流操作系统的内置字体,这样用户在浏览页面的时候就有可能看不到真实的设计. 美工设计师最常做的办 ...
- 关于MSP430中断机制
中断很大程度上体现了一款单片机的性能,从这一点将MSP430在中断方面做得很不错,主要是提供了非常丰富的中断源,基本的有IO中断,定时器中断和一些接口中断(SPI,UART,I2C)等等. 现 ...
- JS页面间传值
一:JavaScript静态页面值传递之URL篇 能过URL进行传值.把要传递的信息接在URL上. 例子: 参数传出页面Post.htm—> <input type="tex ...