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\).综上, ...
随机推荐
- 二 J2EE 概述
一 WEB 应用 1. WEB 应用工作方式:B/S 模式 (浏览器/服务器模式) 2. WEB 应用结构组成: a. WEB 服务器:是安装在 WEB 服务器计算机上的一个软件包,负责接收用户请求并 ...
- SFTP 上传文件夹
使用sftp上传文件夹时若使用如下命令并不work: put /media/Research/GWAS_Class/* Desktop/ 此时,需要添加一个参数 -r, 另外在目标文件夹下面建立一个同 ...
- 第四部分:python性能技巧
4.1 查询操作为主时,选择字典结构比list结构效率更高 4.2 取list的交集.并集.差集时,可借助set数据结构如listintersection = list(set(lista)& ...
- Codeforces Round #365 (Div. 2) B 前缀和
B. Mishka and trip time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- 使用 rqt_console 和 roslaunch---8
使用 rqt_console 和 roslaunch Description: 本教程介绍如何使用rqt_console和rqt_logger_level进行调试,以及如何使用roslaunch同时运 ...
- Codeforces Round #120 (Div. 2)
A. Vasya and the Bus 根据\(n,m\)是否为0分类讨论下. B. Surrounded 判断两圆是否有交点,否则构造的圆与两圆相切. C. STL 看代码比较清楚. void t ...
- 救援行动(save) (BFS)
时间限制: 1 Sec 内存限制: 64 MB提交: 42 解决: 9[提交][状态][讨论版] 题目描述 Angel被人抓住关在一个迷宫了!迷宫的长.宽均不超过200,迷宫中有不可以越过的墙以及 ...
- mysql数据库主从复制部署笔记
主从复制是mysql中数据库实时同步的一个常用做法了,今天我来给各位介绍一下关于mysql数据库主从复制部署一个过程,希望此例子对各位同学参考参考. 数据库主从复制原理: 数据库的主从复制就是从mas ...
- Java——函数
------- <a href="http://www.itheima.com" target="blank">android培训</a ...
- CentOS 6 DEPRECATED
Created by YEISON CAMARGO, last modified by John Boteler on 2016.02.02 Go to start of metadata Abo ...