现在是选举时间!


13

是时候...点票了!

今天,我在整个国家举行地方选举。在此,使用D'Hondt方法确定每个宴会的席位数量。您的目标是实现一个程序或功能,该程序或功能将确定每个方以最少的字节数获得多少席位。

对于这种方法,要分配固定数量的席位,方法是这样的:

  1. 每一方都分配有一个可变数字,该数字从其获得的票数开始。
  2. 然后将第一个席位分配给变量中具有最大价值的政党,然后该政党的那个价值将成为其投票总数除以1+seats,四舍五入后,seats该席位中已有的席位数(因此,首先,他们的选票将被2除,并在获得第二个席位后被3除。
  3. 此后,再次比较当事方的票数。该过程继续进行,直到分配了所有席位。

如果最高数目是两个或多个参与方之间的平局,则它是随机解决的(必须是随机的,它不能只是列表中两个中的第一个)。

输入值

您将收到一个数字N,该数字将指示可用席位的数量,以及各方喜欢的任何格式的投票列表。例:

25
12984,7716,13009,4045,1741,1013

输出量

您应该输出每方获得席位的列表。在上面的示例中,它将类似于

8,5,9,2,1,0

它们的顺序应与输入中各方的顺序相同。

例子

5
3,6,1

outputs: 2,3,0

135
1116259,498124,524707,471681,359705,275007,126435

outputs: 45,20,21,19,14,11,5

奖金

如果以参与方的名字作为输入并在输出中提供他们,则获得-20%的奖励,例如:

25
cio:12984,pcc:7716,irc:13009,icb:4045,cub:1741,bb:1013

outputs

cio:8
pcc:5
irc:9
icb:2
cub:1
bb:0

我觉得我们已经做了类似的事情
edc65

我在搜索中找不到任何类似的内容...但是,如果您找到任何内容,我将对其进行更改或删除问题,没问题!
2015年

@rcrmn您的最后一个示例有问题。也许您的意思是总共153个座位,而不是135个
。– Tyilo

@Tyilo对!我在测试程序中写错了,并且未经仔细检查就复制了答案。现在已修复。谢谢!
rorlork 2015年

1
谢谢@Jakube,这是我用来计算的程序的问题,该程序返回带有标签的座位中有序的输出。丹尼斯,您可以按任何顺序退货,因为标签可以用作识别器。如果您更方便,则可以假定它只有字母。
rorlork 2015年

Answers:


6

果酱,35.2 28.8 28.0 26.4

q2*~,m*mr{~)f/1=~}$<:(f{1$e=1\tp}

这个完整的程序长33个字节,有资格获得奖金。

CJam解释器中在线尝试。

运行示例

$ cjam seats <<< '[["cio"12984]["pcc"7716]["irc"13009]["icb"4045]["cub"1741]["bb"1013]]25'
["cio" 8]
["pcc" 5]
["irc" 9]
["icb" 2]
["cub" 1]
["bb" 0]

怎么运行的

q2*~   e# Repeat the input from STDIN twice. Evaluate.
       e# STACK: Array seats Array seats
,      e# Range: seats -> [0 ... seats-1]
m*     e# Take the cartesian product of the array from the input and the range.
mr     e# Shuffle the array. This makes sure tie breakers are randomized.
{      e# Sort by the following key:
  ~    e#     Dump: [["party" votes] number] -> ["party" votes] number
  f/   e#     Divide each: ["party" votes] number -> ["party"/number votes/number]
  1=   e#     Select: ["party"/number votes/number] -> votes/number
  ~    e#     Bitwise NOT.
}$     e#
<      e# Keep only the elements that correspond to seats.
:(     e# Shift each array.
       e# RESULT: S := [[number] ["party" votes] [number] ["party" votes] ...]
f{     e# For each A := ["party" votes]:
       e#     Push S.
  1$   e#     Push a copy of A.
  e=   e#     Count the occurrences of A in S.
  1\t  e#     Replace the vote count with the number of occurrences.
  p    e#     Print.
}      e#

6

Pyth,36个字节-20%= 28.8

J<_hCo/@eCQhNheN.S*UQUvzvz.e,b/JkhCQ

这有资格获得奖金。

在线尝试:演示测试工具

说明:

                                       implicit: z = 1st input (as string)
                                                 Q = 2nd input (evaluated)

                      vz               evaluate z (#seats)
                     U                 generate the list [0,1,...,seats-1]
                   UQ                  generate the list [0,1,...,len(Q)-1]
                  *                    Cartesian product of these lists
                .S                     shuffle (necessary for break ties randomly)
     o                                 order these tuples N by:
        eCQ                               list of votes
       @   hN                             take the N[0]th vote count
      /      heN                          and divide by N[1]+1
   hC                                  extract the party index of the tuples
  _                                    reverse the list
 <                      vz             take the first #seats elements
J                                      and store them in J

                          .e     hCQ   enumerated map over the names of the parties
                                       (k = index, b = name):
                            ,             generate the pair:
                             b/Jk            name, J.count(k)
                                       print implicitely

J是没有必要的。您可以摆脱它并节省2个字节。
isaacg 2015年

另外,如果交换zQ,然后保存CvzK,则可以保存另一个字节。
isaacg 2015年

@isaacg不,这很重要。由于混洗,表达式可能会产生不同的结果,.e从而使计数混乱。
雅库布2015年

1
实际上,第二个技巧也不起作用,对不起,因为我错过了UQ
isaacg 2015年

2
@isaacg发布它作为答案。
雅库布2015年

5

Javascript,210个字节

v=(a,b,c,d,e,f,g)=>{d=Object.assign({},b),c={};for(g in b)c[g]=0;for(;a--;)e=0,f=Object.keys(d),f.forEach(a=>e=d[a]>e?d[a]:e),f=f.filter(a=>d[a]===e),f=f[~~(Math.random()*f.length)],d[f]=b[f]/-~++c[f];return c}

笔记:

  • 需要支持ES6的现代浏览器/引擎。在Firefox中测试。
  • 使用非常重要的/-~++运算符:)

用法示例:

v(25, {cio:12984,pcc:7716,irc:13009,icb:4045,cub:1741,bb:1013})

1
不必将所有变量声明为参数。
nderscore 2015年

2
是的,但我想避免污染全球范围的可能性
user2951302 2015年

1
JS高尔夫就是要污染球的范围:)
nderscore 2015年

2
我已经将您的方法压缩到168个字节。很抱歉修改变量名称。F=(N,X)=>{for(t=[o={}],[t[o[j]=0,j]=X[j]for(j in X)];N--;t[z=y[new Date%y.length]]=X[z]/-~++o[z])m=0,y=[(m=m<t[j]?t[j]:m,j)for(j in X)],y=y.filter(j=>t[j]==m);return o}
nderscore 2015年

4

Pyth-54个字节

AGZQ=kZK*]0lZVGJhOfqeTh.MZZ.e(kb)Z XJK1 XZJ/@kJ+@KJ1;K

输入格式(标准输入):

[25,[12984,7716,13009,4045,1741,1013]]

输出格式(标准输出):

[8, 5, 9, 2, 1, 0]

使用的变量:

G = total number of seats
K = array of seats currently assigned to each party
k = number of votes for each party
Z = k/(K+1)
J = which party should have the next seat

使用vzQ代替GZ。这样,您将使用保存任务A
雅库布2015年

2

珀尔110

#!perl -pa
$n=pop@F;@x=map
0,@F;/a/,$x[$'/$n]++for(sort{$b-$a}map{map{($'/$_|0).rand.a.$i++}//..$n}@F)[0..$n-1];$_="@x"

输入空间最后与座位数分开。

试试

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.