[Hackerrank] Filling jars

Source: Internet
Author: User

Animesh hasNEmpty candy jars, numbered from 1N, With infinite capacity. He performsMOperations. Each operation is described by 3 IntegersA, BAndK. Here,AAndBAre index of the jars, andKIs the number of candies to be added inside each jar whose index liesAAndB(BOTH sponsive). Can you tell the average number of candies afterMOperations?

Input Format
The first line contains two integersNAndMSeparated by a single space.
MLines follow. Each ofMLines contain three IntegersA, BAndKSeparated by single space.

Output Format
A single line containing the average number of candies internal SSNJars,Rounded downTo the nearest integer.

Note
Rounded downMeans finding the greatest integer which is less than or equal to given number. Eg,13.65And13.23Is rounded down13, While12.98Is rounded down12.

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

 

Question:

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.