【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

复制代码
 1 import java.io.*;
 2 import java.util.*;
 3 
 4 
 5 public class Solution {
 6 
 7     public static void main(String[] args) {
 8         Scanner in = new Scanner(System.in);
 9         int n = in.nextInt();
10         int m = in.nextInt();
11         Long result = new Long(0);
12         for(int i = 0; i < m; i++){
13             Long a = in.nextLong();
14             Long b = in.nextLong();
15             Long k = in.nextLong();
16             result += k*(b-a+1);
17         }
18         System.out.println(result/n);
19     }
20     
21     
22     
23 }
复制代码

 


0 <= k <= 106


 

题解:

 

posted @   SunshineAtNoon  阅读(518)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示