Atcoder (纪念第一次用Perl提交成功)

B - Ranking with Ties
Time Limit: 2 sec / Memory Limit: 1024 MB
Score : 200 points
Problem Statement
N people labeled from 1 to N participated in a certain contest. The score of person i (1≤i≤N) was Pi .
In this contest, the rank of each of the N people is determined by the following procedure:
Prepare a variable r, and initialize r=1. Initially, the ranks of the N people are all undetermined.
Repeat the following operation until the ranks of all N people are determined:
Let x be the maximum score among the people whose ranks are currently undetermined, and let k be the number of people whose score is x. Determine the rank of those k people with score x to be r, and then add k to r.
Print the rank of each of the N people.
Constraints
1≤N≤100
1≤Pi≤100
All input values are integers.
Answer Perl version

sub cal {
  my @arr = @_;
  my %some_hash;
  foreach $a (@arr) {
    $some_hash{$a}++;
  }
  my %ha;
  @sorted_arr = grep{++$ha{$_}<2} @arr;
  @sorted_arr = sort { $a <=> $b } @sorted_arr;
  @sorted_arr = reverse @sorted_arr;
  %s_hash = ();
  $n = 1;
  foreach $a (@sorted_arr){
    $s_hash{$a} = $n;
    $n += $some_hash{$a};
  }
  foreach $a (@arr) {
    print($s_hash{$a});
    print("\n");
  }
}

$n = <>;
chomp $n;
@arr = split ' ', <>;
@arr = map { $_ + 0 } @arr;
cal(@arr);

Answer Ruby version

def cal(arr)
  h = Hash.new(0)
  arr.each do |b|
    h[b] += 1
  end
  m = h.keys.sort.reverse
  h1 = Hash.new
  j = 1
  m.each_with_index do |m1,i|
    h1[m1] = j
    j += h[m1]
  end
  arr.each do |a|
    puts h1[a]
  end
end

n = gets.to_i
arr = gets.split(" ").map {|it| it.to_i}
cal(arr)
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容