Skip to content

Instantly share code, notes, and snippets.

@hyunjun
Created December 18, 2014 00:56
Show Gist options
  • Save hyunjun/afcf77b19906a45586b7 to your computer and use it in GitHub Desktop.
Save hyunjun/afcf77b19906a45586b7 to your computer and use it in GitHub Desktop.
[hacker rank] Filling Jars
#include <stdio.h>
int main() {
int num_of_jars = 0, num_of_ops = 0;
fscanf(stdin, "%d %d", &num_of_jars, &num_of_ops);
int i = 0;
long jar_from = 0, jar_to = 0, num_of_candies = 0;
long long total_num_of_candies = 0;
for ( i = 0; i < num_of_ops; ++i ) {
fscanf(stdin, "%ld %ld %ld", &jar_from, &jar_to, &num_of_candies);
total_num_of_candies += (jar_to - jar_from + 1) * num_of_candies;
}
printf("%lld\n", total_num_of_candies / num_of_jars);
return 0;
}
import sys
def average_num_of_candies(num_of_jars, data):
candies = [0] * num_of_jars
for jar_from, jar_to, num_of_candies in data:
for i in range(jar_from - 1, jar_to):
candies[i] += num_of_candies
return sum(candies) / num_of_jars
if __name__ == '__main__':
num_of_jars, num_of_ops = map(int, raw_input().split())
data = []
for line in sys.stdin.readlines():
jar_from, jar_to, num_of_candies = map(int, line.split())
data.append((jar_from, jar_to, num_of_candies))
print average_num_of_candies(num_of_jars, data)
import sys
def average_num_of_candies(num_of_jars, data):
candies = [0] * num_of_jars
for jar_from, jar_to, num_of_candies in data:
candies[jar_from - 1:jar_to] = map(lambda x: x + num_of_candies, candies[jar_from - 1:jar_to])
return sum(candies) / num_of_jars
if __name__ == '__main__':
num_of_jars, num_of_ops = map(int, raw_input().split())
data = []
for line in sys.stdin.readlines():
jar_from, jar_to, num_of_candies = map(int, line.split())
data.append((jar_from, jar_to, num_of_candies))
print average_num_of_candies(num_of_jars, data)
import sys
if __name__ == '__main__':
num_of_jars, num_of_ops = map(int, raw_input().split())
total_num_of_candies, data = 0, []
for line in sys.stdin.readlines():
jar_from, jar_to, num_of_candies = map(int, line.split())
total_num_of_candies += num_of_candies * (jar_to - jar_from + 1)
print total_num_of_candies / num_of_jars
import sys
if __name__ == '__main__':
num_of_jars, num_of_ops = map(int, input().split())
total_num_of_candies, data = 0, []
for line in sys.stdin.readlines():
jar_from, jar_to, num_of_candies = map(int, line.split())
total_num_of_candies += num_of_candies * (jar_to - jar_from + 1)
print(int(total_num_of_candies / num_of_jars))
mport java.math.BigInteger;
import java.util.Scanner;
public class Solution {
public static void main(final String[] args) {
Scanner sc = new Scanner(System.in);
String[] input = sc.nextLine().split(" ");
final int numOfJars = Integer.parseInt(input[0]);
final int numOfOps = Integer.parseInt(input[1]);
BigInteger totalNumOfCandies = new BigInteger("0");
for ( int i = 0; i < numOfOps; ++i ) {
input = sc.nextLine().split(" ");
BigInteger jarFrom = new BigInteger(input[0]);
BigInteger jarTo = new BigInteger(input[1]);
BigInteger numOfCandies = new BigInteger(input[2]);
totalNumOfCandies = totalNumOfCandies.add(jarTo.subtract(jarFrom).add(BigInteger.ONE).multiply(numOfCandies));
}
System.out.println(totalNumOfCandies.divide(BigInteger.valueOf(numOfJars)));
}
}
object Solution {
def main(args: Array[String]) {
val inps = io.Source.stdin.getLines.toArray
val numOfJars = inps(0).split(" ")(0).toInt
val numOfOps = inps(0).split(" ")(1).toInt
var totalNumOfCandies = BigInt(0)
for ( l <- inps.drop(1) ) {
val data = l.split(" ").toArray.map((s: String) => BigInt(s))
totalNumOfCandies += (data(1) - data(0) + 1) * data(2)
}
println(totalNumOfCandies / numOfJars)
}
}
5 3
1 2 100
2 5 100
3 4 100
10000000 3
1000000 2000000 1000000
2000000 5000000 1000000
3000000 4000000 1000000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment