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\).综上, ...
随机推荐
- 【题解】【链表】【Leetcode】Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- JavaScript中递归函数用法需要注意的
<script> function sum(num){ if(num<=1){ return 1; }else{ return num*sum(num-1);//return num ...
- win7录屏工具
psr.exe http://jingyan.baidu.com/article/aa6a2c14d330710d4d19c47c.html
- 学习Linux——计算机概论
一直想学习Linux,但计划时不时被耽误,现在开始,决定每天开始学习Linux.学习从最简单的开始,一步步,不能将最简单的东西忽略. 1.计算机硬件的五大单元 计算机分为三部分:输入单元,中央处理器即 ...
- ZOJ 1002 Fire Net
题目大意:有一个4*4的城市,其中一些格子有墙(X表示墙),在剩余的区域放置碉堡.子弹不能穿透墙壁.问最多可以放置几个碉堡,保证它们不会相互误伤. 解法:从左上的顶点开始遍历,如果这个点不是墙,做深度 ...
- 正确答案 全国信息学奥林匹克联赛( ( NOIP2014) 复 赛 模拟题 Day1 长乐一中
[题目描述]小 H 与小 Y 刚刚参加完 UOIP 外卡组的初赛,就迫不及待的跑出考场对答案."吔,我的答案和你都不一样!",小 Y 说道,"我们去找神犇们问答案吧&qu ...
- 使用Metasploit进行端口扫描
Metasploit中不仅能够使用第三方扫描器Nmap等,在其辅助模块中也包含了几款内建的端口扫描器. 查看Metasploit框架提供的端口扫描工具: msf > search portsca ...
- Python文件格式化写入
[root@localhost test]# cat 1.py fd = open('format.txt','w') head = "%10s%10s%10s\n"%('id', ...
- 团队项目开发中,常见的版本控制有svn,git
团队项目开发中,常见的版本控制有svn,git
- ExtJS布局方式(layout)图文详解
Auto默认布局 不给下级组件指定大小和位置 Absolute绝对布局 可使用坐标(x.y)进行布局 Accordion手风琴布局 实现Accordion效果的布局,也可叫可折叠布局.也就是说使用该布 ...