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"的更多相关文章

  1. 【HackerRank】Missing Numbers

    Numeros, The Artist, had two lists A and B, such that, B was a permutation of A. Numeros was very pr ...

  2. 【HackerRank】Closest Numbers

    Sorting is often useful as the first step in many different tasks. The most common task is to make f ...

  3. Codeforces Round #114 (Div. 1) C. Wizards and Numbers 博弈论

    C. Wizards and Numbers 题目连接: http://codeforces.com/problemset/problem/167/C Description In some coun ...

  4. Asia Hong Kong Regional Contest 2016

    A. Colourful Graph 可以在$2n$步之内实现交换任意两个点的颜色,然后就可以构造出方案. #include <bits/stdc++.h> using namespace ...

  5. Codechef April Challenge 2019 游记

    Codechef April Challenge 2019 游记 Subtree Removal 题目大意: 一棵\(n(n\le10^5)\)个结点的有根树,每个结点有一个权值\(w_i(|w_i\ ...

  6. Codechef April Challenge 2019 Division 2

    Maximum Remaining 题意:给n个数,取出两个数$a_{i}$,$a_{j}$,求$a_{i}\% a_{j}$取模的最大值 直接排个序,第二大(严格的第二大)模第一大就是答案了. #i ...

  7. 【Code Chef】April Challenge 2019

    Subtree Removal 很显然不可能选择砍掉一对有祖先关系的子树.令$f_i$表示$i$子树的答案,如果$i$不被砍,那就是$a_i + \sum\limits_j f_j$:如果$i$被砍, ...

  8. 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 ...

  9. CodeChef April Challenge 2019题解

    传送门 \(Maximum\ Remaining\) 对于两个数\(a,b\),如果\(a=b\)没贡献,所以不妨假设\(a<b\),有\(a\%b=a\),而\(b\%a<a\).综上, ...

随机推荐

  1. 深入理解JavaScript闭包

    Closure 闭包的定义1: <JavaScript高级程序设计>定义闭包:闭包是指有权访问另一个函数作用域中的变量的函数. 创建闭包的常见方式,就是在一个函数内部创建另一个函数. 然而 ...

  2. 优先队列(Priority Queue)

    优先队列(Priority Queue) A priority queue must at least support the following operations: insert_with_pr ...

  3. USB 描述符

    标准的USB设备有5种USB描述符:设备描述符,配置描述符,字符串描述符,接口描述符,端点描述符. // Standard Device Descriptor typedef struct { u8 ...

  4. 解决f.lux总是弹框定位

    解决f.lux总是弹框定位,直接导入成功定位的注册表文件即可. 以下保存为f.lux.reg 双击导入即可. Windows Registry Editor Version 5.00 [HKEY_CU ...

  5. Java--常用类

    1.方法中参数传递:值传递 基本数类型:具体的实际值 引用数据类型:对象的地址值 2.继承:关键字:extends单继承继承父类的非私有成员多重继承 2.权限的修饰符:private 只能在当前类的内 ...

  6. 无密码通过ssh执行rsync

    默认情况下,在执行rsync命令时通常需要我们输入密码.但有时我们并不希望如此,那么如何实现无密码执行rsync呢? 1. 测试通过ssh可以执行rsync(需要密码) 执行rsync,确保你帐户的密 ...

  7. openjudge 大师兄,师傅被妖怪抓走啦

    描述 孙悟空听到沙僧大喊一句:“大师兄,师傅被妖怪抓走啦!”于是孙悟空直追白骨精而去.孙悟空在一条长度为L的森林小路上飞奔,上面有L+1个整点,依次为0,1,2……L.白骨精会使用一种大范围的攻击法术 ...

  8. jquery中prop()方法和attr()方法的区别

    最近在用jquery的时候遇到一个问题,那就是attr()方法,发现这个方法有时候使用会有一些说不出原因的问题.翻翻自己之前笔记发现,还有个函数prop(). 这两个函数都可以用来获取属性. jque ...

  9. hdu2955 Robberies  01背包+概率

    link:http://acm.hdu.edu.cn/showproblem.php?pid=2955 首先,这个题目的背包容量不能是概率.1.精度不清楚.2.把概率相加有什么意义呢?所以,转换一下, ...

  10. C#部分---利用arraylist集合做滚动抽奖;

    输入多个手机号码,放到集合中,进行三秒钟的滚动抽奖:随机显示号码,清空,再显示: 1.收集号码: 2.每隔三秒进行抽奖,及作弊代码,哈哈哈: 3.System.Threading.Thread.Sle ...