Codeforces Beta Round #93 (Div.2 Div.1)
はい。
http://codeforces.com/contest/127
A. Wasted Time
geometry
ざっくりと大意
・Mr. Scroogeは無駄を数える。
・
方針のようなもの
・問題が解読できない。。。。
B. Canvas Frames
implementation
ざっくりと大意
・Nicholasはn本のstickからcanvasをつくる。
・stickは折ったり長さを変えたりしてはいけない。
・四角形になるよう縦2本はペアで同じ長さ、横2本はペアで同じ長さである必要がある。
方針のようなもの
・特に長方形と正方形は区別しなくていいのかな?
・ソートした後で先頭からペア2組ずつ探せばいいのかな??
#!/usr/bin/env python # -*- coding: UTF-8 -*- n=int(raw_input()) l=map(int, raw_input().split()) l.sort() ans=h=tmp=0 while len(l): if tmp==0: tmp=l[0] l.pop(0) elif tmp==l[0] and h==0: h=1 tmp=0 l.pop(0) elif tmp==l[0] and h==1: h=0 tmp=0 ans+=1 l.pop(0) elif tmp!=l[0]: tmp=l[0] l.pop(0) print ans
先頭からtmpにメモして同じ長さならペア成立を管理して、tmpはないものにさせる。違う長さならペア管理は何もせずメモしなおして次を見に行く。
とやってたら他の人の回答と比べて遅い方法だった模様。
2520189のdaidailanlanさん。
input() a = raw_input().split() m = {} for x in a: m[x] = (m.get(x) or 0) + 1 print sum(x / 2 for x in m.values()) / 2
3076915のlovefly1983さん。
input() s=raw_input().split() print sum([s.count(i)/2 for i in set(s)])/2
なるほど分からんな例。
こっちのがパッと見て分かりやすい例。2391254のfish_ballさん。
import sys n = int(sys.stdin.readline().strip()) a = [int(x) for x in sys.stdin.readline().strip().split()] a.sort() i = 1 cnt = 0 while i < n: if a[i] == a[i-1]: cnt += 1 i += 1 i += 1 print(cnt // 2)
forループで探すと先頭から1つずつになってしまうのをwhileでループにしてペア成立時にiを余分に加算して2つ先を見に行くことで同じ数字が3つ並んでいる時にペアは1つなのに2つカウントしてしまう対策をしているんだと思う。私の書きたかった処理に近いです。