summaryrefslogtreecommitdiff
path: root/2011/qual/d/d.py
blob: dafa0dd7bc0b93c8eda2ca22777bc5421cab2e92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
f = open('2.in', 'r')
o = open('2.out', 'w')

T = int(f.readline().strip())

for t in xrange(T):
    _n = f.readline().strip()
    el = map(int, f.readline().strip().split(' '))
    print el
    m = len([el[i] for i in range(len(el)) if el[i] != i+1 and el[el[i]-1] != i+1])
    p = len([el[i] for i in range(len(el)) if el[i] != i+1 and el[el[i]-1] == i+1])
    
    print m, p
    
    m = m-1 if m>0 else 0
    
    steps = p + (2 * m)
    
    res = steps
    
    s = "Case #%d: %s\n" % (t+1, res)
    print s
    o.write(s)

f.close()
o.close()