Solving with Ruby, Perl, Java and Python AtCoder ABC 131 D Array Sorting

Introduction

This theme

AtCoder Beginner Contest 131 D - Megalomania Difficulty: 594

This theme, sorting of arrays Ruby It's an array sort, so I think it's easy to implement. ~~ I chose a lighter question because it's my first Python. ~~

ruby.rb


n = gets.to_i
a = Array.new(n){gets.split.map(&:to_i)}
a.sort_by!{|x| x[1]}
ans = 0
n.times do |i|
  ans += a[i][0]
  if ans > a[i][1]
    puts "No"
    exit
  end
end
puts "Yes"

sort.rb


a.sort_by!{|x| x[1]}

~~ Sorting is done by multiple conditions, but for learning purposes, only one can be sorted as a solution. ~~ ** Addition ** Some corrections have been made from the comments received. Python It is almost a sutra copy because it is solved with *** Python *** for the first time.

python.py


n = int(input())
a = list(list(map(int, input().split())) for _ in range(n))
a.sort(key=lambda x: x[1])
ans = 0
for i in range(0, n):
    ans += a[i][0]
    if ans > a[i][1]:
        print("No")
        exit()
print("Yes")

Coding is easy because the VSCode extension is working.

range.py


for i in range(0, n):
for i in range(0, n - 1):

I tried to turn it with i at for, but I wrote n -1 and got WA.

Next time, I will try to sort by multiple conditions. Perl

perl.pl


chomp (my $n = <STDIN>);
my @a;
for my $i (0..$n-1) {
  my @in = split / /, <STDIN>;
  ($a[$i][0], $a[$i][1]) = @in;
}
@a = sort {$$a[1]<=>$$b[1] || $$b[0]<=>$$a[0]} @a;

my $ans = 0;
for my $i (0..$n-1) {
  $ans += $a[$i][0];
  if ($ans>$a[$i][1]) {
    print "No\n";
    exit;
  }
}
print "Yes\n";

sort.pl


@a = sort {$$a[1]<=>$$b[1] || $$b[0]<=>$$a[0]} @a;

java.java


import java.util.*;

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.next());
        List<Work> work = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            int time = Integer.parseInt(sc.next());
            int limit = Integer.parseInt(sc.next());
            work.add(new Work(time, limit));
        }
        sc.close();
        work.sort(Comparator.comparingInt(x -> x.limit));
        int ans = 0;
        for (int i = 0; i < n; i++) {
            ans += work.get(i).time;
            if (ans > work.get(i).limit) {
                System.out.println("No");
                return;
            }
        }
        System.out.println("Yes");
    }

    static class Work {
        int time;
        int limit;

        Work(int time, int limit) {
            this.time = time;
            this.limit = limit;
        }
    }
}

sort.java


        work.sort(Comparator.comparingInt(x -> x.limit));

** Addition ** As I was told in the comments, I changed the sort comparison part to code that uses the Comparator introduced in java8. Safer and slightly faster.

Ruby Python Perl Java
Code length 189 Byte 231 Byte 315 Byte 972 Byte
Execution time 484 ms 995 ms 1237 ms 883 ms
memory 22520 KB 53728 KB 66788 KB 72900 KB

Summary

Recommended Posts

Solving with Ruby, Perl, Java and Python AtCoder ABC 131 D Array Sorting
Solving with Ruby, Perl, Java and Python AtCoder ABC 165 D Floor function
Solving with Ruby, Perl, Java, and Python AtCoder ABC 065 C factorial
Solving with Ruby and Python AtCoder ABC151 D Breadth-first search
Solving with Ruby, Perl, Java and Python AtCoder ATC 002 A
Solving with Ruby, Perl, Java and Python AtCoder ATC 002 B
Solving with Ruby and Python AtCoder ABC138 D Adjacency list
Solving in Ruby, Python and Java AtCoder ABC141 D Priority Queuing
Solving with Ruby, Python and networkx AtCoder ABC168 D Adjacency list
Solving with Ruby, Perl, Java, and Python AtCoder AGC 033 A Breadth-first search
Solving with Ruby, Perl, Java, and Python AtCoder ARC 098 C Cumulative sum
Solving with Ruby, Perl, Java and Python AtCoder CADDi 2018 C Prime Factorization
Solve with Ruby, Perl, Java and Python AtCoder ABC 047 C Regular Expression
Solve with Ruby and Python AtCoder ABC133 D Cumulative sum
Solving with Ruby and Python AtCoder AISING2020 D Iterative Squares
Solving with Ruby and Python AtCoder ABC011 C Dynamic programming
Solving with Ruby and Python AtCoder ABC153 E Dynamic programming
Solving with Ruby, Perl, Java and Python AtCoder diverta 2019 Programming Contest C String Manipulation
Solving with Ruby, Python and numpy AtCoder ABC054 B Matrix operation
Solving with Ruby and Python AtCoder ABC057 C Prime Factorization Bit Search
Solving in Ruby, Perl, Java, and Python AtCoder ARC 066 C Iterative Squares Hash
Solving with Ruby and Python AtCoder ARC 059 C Least Squares
Solving with Ruby and Python AtCoder ARC067 C Prime Factorization
Solve AtCoder ABC168 with python (A ~ D)
Solve with Ruby, Python and Java AtCoder ARC104 B Cumulative sum
Solving with Ruby and Python AtCoder Tenka1 Programmer Contest C Cumulative sum
Solving with Ruby AtCoder ABC110 C String Manipulation
Solving with Ruby and Python AtCoder CODE FESTIVAL 2016 qual C B Priority queue
AtCoder ABC 182 Python (A ~ D)
Solve AtCoder ABC 186 with Python
AtCoder ABC168 A case expression solved in Ruby and Python
Solve ABC166 A ~ D with Python
Ruby Python Java Case insensitive sorting
Solved AtCoder ABC 114 C-755 with Python3
List split and join strings with split and join (Perl / PowerShell / Java / Kotlin / Python)
Python> link> 2D array initialization and assignment
[AtCoder] Solve ABC1 ~ 100 A problem with Python
Encrypt with Ruby (Rails) and decrypt with Python
Easy web scraping with Python and Ruby
AtCoder ABC172 C Cumulative Sum Binary Search Solved by Ruby and Python
[AtCoder] Solve A problem of ABC101 ~ 169 with Python
MessagePack-Try to link Java and Python with RPC
Correspondence summary of array operation of ruby and python
AtCoder ABC 174 Python
[AtCoder explanation] Control the A, B, (C), D problems of ABC165 with Python!
[AtCoder explanation] Control the A, B, C, D problems of ABC183 with Python!
AtCoder ABC187 Python
AtCoder ABC188 Python
Solving the Lorenz 96 model with Julia and Python
Challenge AtCoder (ABC) 164 with Python! A ~ C problem
[AtCoder explanation] Control the A, B, C, D problems of ABC181 with Python!
AtCoder ABC 175 Python
2014 Web Application Framework Trends (PHP / Java / Ruby / Python / Perl)
Benchmark for C, Java and Python with prime factorization
Algorithm learned with Python 18th: Sorting (stack and queue)
Try to operate DB with Python and visualize with d3
Investigate Java and python data exchange with Apache Arrow
Comparison of CoffeeScript with JavaScript, Python and Ruby grammar
Version control of Node, Ruby and Python with anyenv
Ruby, Python and map
Python and Ruby split