AcWing 105. 七夕祭
原题链接
困难
作者:
XianZS.com
,
2024-09-25 22:17:04
,
所有人可见
,
阅读 2
import copy
n,m,t=map(int,input().split())
rs=[0 for _ in range(n+1)]
cs=[0 for _ in range(m+1)]
for _ in range(t):
x,y=map(int,input().split())
rs[x]+=1
cs[y]+=1
def make(nums,number) -> int:
mid=sum(nums)//number
als=[0 for _ in range(number+1)]
for x in range(1,number+1):
nums[x]-=mid
for x in range(1,number+1):
als[x]=als[x-1]+nums[x]
als.pop(0)`
als.sort()
ans=0
for x in range(number):
ans+=abs(als[x]-als[number>>1])
return ans
def main() -> int:
# pass
if t%n & t%m: # 无法操作
print("impossible")
elif t%n==0 and t%m==0: # 都操作
print("both {}".format(make(rs,n)+make(cs,m)))
elif t%m==0: # 列操作
print("column {}".format(make(cs,m)))
else: # 行操作
print("row {}".format(make(rs,n)))
return 0
main()