【HackerRank】 Filling Jars
Animesh has N empty candy jars, numbered from 1 to N, with infinite capacity. He performs M operations. Each operation is described by 3 integers a, b and k. Here, a and b are index of the jars, and k is the number of candies to be added inside each jar whose index lies between a and b (both inclusive). Can you tell the average number of candies after M operations?
Input Format
The first line contains two integers N and M separated by a single space.
M lines follow. Each of the M lines contain three integers a, b and k separated by single space.
Output Format
A single line containing the average number of candies across N jars, rounded down to the nearest integer.
Note
Rounded down means finding the greatest integer which is less than or equal to given number. Eg, 13.65 and 13.23 is rounded down to 13, while 12.98 is rounded down to 12.
Constraints
3 <= N <= 107
1 <= M <= 105
1 <= a <= b <= N
import java.io.*;
import java.util.*; public class Solution { public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
Long result = new Long(0);
for(int i = 0; i < m; i++){
Long a = in.nextLong();
Long b = in.nextLong();
Long k = in.nextLong();
result += k*(b-a+1);
}
System.out.println(result/n);
} }
0 <= k <= 106
题解:
【HackerRank】 Filling Jars的更多相关文章
- 【HackerRank】How Many Substrings?
https://www.hackerrank.com/challenges/how-many-substrings/problem 题解 似乎是被毒瘤澜澜放弃做T3的一道题(因为ASDFZ有很多人做过 ...
- 【HackerRank】Running Time of Quicksort
题目链接:Running Time of Quicksort Challenge In practice, how much faster is Quicksort (in-place) than I ...
- 【hackerrank】Week of Code 30
Candy Replenishing Robot Find the Minimum Number 直接模拟 Melodious password dfs输出方案 Poles 题意:有多个仓库,只能从后 ...
- 【hackerrank】Week of Code 26
在jxzz上发现的一个做题网站,每周都有训练题,题目质量……前三题比较水,后面好神啊,而且类型差不多,这周似乎是计数专题…… Army Game 然后给出n*m,问需要多少个小红点能全部占领 解法:乘 ...
- 【HackerRank】Median
题目链接:Median 做了整整一天T_T 尝试了各种方法: 首先看了解答,可以用multiset,但是发现java不支持: 然后想起来用堆,这个基本思想其实很巧妙的,就是维护一个最大堆和最小堆,最大 ...
- 【HackerRank】Coin on the Table
题目链接:Coin on the Table 一开始想用DFS做的,做了好久都超时. 看了题解才明白要用动态规划. 设置一个三维数组dp,其中dp[i][j][k]表示在时间k到达(i,j)所需要做的 ...
- 【HackerRank】Pairs
题目链接:Pairs 完全就是Two Sum问题的变形!Two Sum问题是要求数组中和正好等于K的两个数,这个是求数组中两个数的差正好等于K的两个数.总结其实就是“骑驴找马”的问题:即当前遍历ar[ ...
- 【HackerRank】Cut the tree
题目链接:Cut the tree 题解:题目要求求一条边,去掉这条边后得到的两棵树的节点和差的绝对值最小. 暴力求解会超时. 如果我们可以求出以每个节点为根的子树的节点之和,那么当我们去掉一条边(a ...
- 【HackerRank】Missing Numbers
Numeros, The Artist, had two lists A and B, such that, B was a permutation of A. Numeros was very pr ...
随机推荐
- map--C++ STL 学习
map–C++ STL 学习 Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力. 说下map内 ...
- Elasticsearch5.X IN Windows 10 系列文章(3)
系统类型:windows10 64位家庭版 ElasticSearch版本: 5.5.1 (最新稳定版为5.5.2),由于用到IK中文分词插件,最新版本没有5.5.2 ,所以使用5.5.1 日期:20 ...
- CentOS 7 换yum源
备份原来的源 sudo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bk 下载阿里源 $ cd /et ...
- javascript拼接html代码
转自开源中国社区:http://www.oschina.net/code/snippet_94055_21640经常做jsp开发的朋友可能遇到一个情况,显示列表数据不是table,而是div或者其他很 ...
- MEF基础概念学习笔记
MEF,是微软.net框架下的一个框架类库.可以使你的程序低耦合的加载扩展.在开发插件,或者开发一些需要灵活扩展的功能的时候经常用到.例如微软给出的计算器的例子.当你开发计算器的时候,初始功能只提供了 ...
- Laravel创建Route
<?php /* |-------------------------------------------------------------------------- | Routes Fil ...
- urllib -- ProxyHandler处理器(代理设置)
import urllib.requestimport randomimport ssl proxy_list = [ {"https" : "196.61.27.58: ...
- nyoj677 谍战
本题能够说是最小割入门级题目. 假设能想到是最小割问题.那么建图思路便是水到渠成的事了. 加入一个源点S和汇点T: 把S与每一个间谍相连.容量为无穷大: 把城市N(即飞机场的位置)与汇点T相连.容量为 ...
- 160824、ionic添加地图站点
1.基本的地图显示 <!DOCTYPE html> <html ng-app="myApp"> <head> <meta charset= ...
- Django基础流程
软件环境: Pycharm 2018.1 Python 3.6 Django 2.0.3 1.新建项目 直接使用Pycharm的菜单来创建项目,命名为mysite. mysite mysite __i ...