[Google Codejam] Round 1A 2016 - Rank and File
2016-04-16 21:05
513 查看
[Problem Description]
Argus believes that it is important to keep an eye on all of his soldiers at all times. Since he likes to look at the grid from the upper left, he requires that:
Within every row of the grid, the soldiers' heights must be in strictly increasing order, from left to right.
Within every column of the grid, the soldiers' heights must be in strictly increasing order, from top to bottom.
Although no two soldiers in the same row or column may have the same height, it is possible for multiple soldiers in the grid to have the same height.
Since soldiers sometimes train separately with their row or their column, Argus has asked you to make a report consisting of 2*N lists of the soldiers' heights: one representing each row (in left-to-right order) and column
(in top-to-bottom order). As you surveyed the soldiers, you only had small pieces of paper to write on, so you wrote each list on a separate piece of paper. However, on your way back to your office, you were startled by a loud bugle blast and you dropped all
of the pieces of paper, and the wind blew one away before you could recover it! The other pieces of paper are now in no particular order, and you can't even remember which lists represent rows and which represent columns, since you didn't write that down.
You know that Argus will make you do hundreds of push-ups if you give him an incomplete report. Can you figure out what the missing list is?
in the statement. It is guaranteed that these lists represent all but one of the rows and columns from a valid grid, as described in the statement.
the test case number (starting from 1) and y is a list of N integers in strictly increasing order, representing the missing list.
1 ≤ all heights ≤ 2500.
The integers on each line will be in strictly increasing order.
It is guaranteed that a unique valid answer exists.
In the sample case, the arrangement must be either this:
or this:
In either case, the missing list is
[Problem Analysis]
It is tempting to think it in a rather complicated way. However, the solution is very simple, if you observe a very important property. That is: each soldier
appears exactly twice in your files, once when you record in row and once when you record in column. So, each number must appear even times in the files. If one file is missed, what happened? If you lost the file recording a row, each number will have appeared
once in the columns. These numbers will appear for odd times! What's more, only these lost numbers appear in odd times! Problem becomes easy, since we just need to find the numbers which appears for odd times in the left files. Remember to sort them before
output! The solution in Python is shown below.
[Problem Solution]
T = int(raw_input())
for t in xrange(1, T+1):
N = int(raw_input())
ls = []
for _ in xrange(2*N-1):
l = [int(i) for i in raw_input().split(' ')]
ls.append(l)
nums = []
for l in ls:
nums += l
c = Counter(nums)
res = []
for i in c:
if c[i] % 2 == 1:
res.append(i)
res.sort()
res = [str(i) for i in res]
res = ' '.join(res)
print 'Case #{}: {}'.format(t, res)
[Comment]
If one way is too long and obstructive, just have a rest and think in another way. There is an Chinese poetry illustrating this quite appropriately: "山重水复疑无路,柳暗花明又一村"是也。
[Source]
https://code.google.com/codejam/contest/4304486/dashboard#s=p1
Problem
When Sergeant Argus's army assembles for drilling, they stand in the shape of an N by Nsquare grid, with exactly one soldier in each cell. Each soldier has a certain height.Argus believes that it is important to keep an eye on all of his soldiers at all times. Since he likes to look at the grid from the upper left, he requires that:
Within every row of the grid, the soldiers' heights must be in strictly increasing order, from left to right.
Within every column of the grid, the soldiers' heights must be in strictly increasing order, from top to bottom.
Although no two soldiers in the same row or column may have the same height, it is possible for multiple soldiers in the grid to have the same height.
Since soldiers sometimes train separately with their row or their column, Argus has asked you to make a report consisting of 2*N lists of the soldiers' heights: one representing each row (in left-to-right order) and column
(in top-to-bottom order). As you surveyed the soldiers, you only had small pieces of paper to write on, so you wrote each list on a separate piece of paper. However, on your way back to your office, you were startled by a loud bugle blast and you dropped all
of the pieces of paper, and the wind blew one away before you could recover it! The other pieces of paper are now in no particular order, and you can't even remember which lists represent rows and which represent columns, since you didn't write that down.
You know that Argus will make you do hundreds of push-ups if you give him an incomplete report. Can you figure out what the missing list is?
Input
The first line of the input gives the number of test cases, T. T test cases follow. Each consists of one line with an integer N, followed by 2*N-1 lines of N integers each, representing the lists you have, as describedin the statement. It is guaranteed that these lists represent all but one of the rows and columns from a valid grid, as described in the statement.
Output
For each test case, output one line containingCase #x: y, where
xis
the test case number (starting from 1) and y is a list of N integers in strictly increasing order, representing the missing list.
Limits
1 ≤ T ≤ 50.1 ≤ all heights ≤ 2500.
The integers on each line will be in strictly increasing order.
It is guaranteed that a unique valid answer exists.
Small dataset
2 ≤ N ≤ 10.Large dataset
2 ≤ N ≤ 50.Sample
Input | Output |
1 3 1 2 3 2 3 5 3 5 6 2 3 4 1 2 3 | Case #1: 3 4 6 |
1 2 3 2 3 4 3 5 6
or this:
1 2 3 2 3 5 3 4 6
In either case, the missing list is
3 4 6.
[Problem Analysis]
It is tempting to think it in a rather complicated way. However, the solution is very simple, if you observe a very important property. That is: each soldier
appears exactly twice in your files, once when you record in row and once when you record in column. So, each number must appear even times in the files. If one file is missed, what happened? If you lost the file recording a row, each number will have appeared
once in the columns. These numbers will appear for odd times! What's more, only these lost numbers appear in odd times! Problem becomes easy, since we just need to find the numbers which appears for odd times in the left files. Remember to sort them before
output! The solution in Python is shown below.
[Problem Solution]
T = int(raw_input())
for t in xrange(1, T+1):
N = int(raw_input())
ls = []
for _ in xrange(2*N-1):
l = [int(i) for i in raw_input().split(' ')]
ls.append(l)
nums = []
for l in ls:
nums += l
c = Counter(nums)
res = []
for i in c:
if c[i] % 2 == 1:
res.append(i)
res.sort()
res = [str(i) for i in res]
res = ' '.join(res)
print 'Case #{}: {}'.format(t, res)
[Comment]
If one way is too long and obstructive, just have a rest and think in another way. There is an Chinese poetry illustrating this quite appropriately: "山重水复疑无路,柳暗花明又一村"是也。
[Source]
https://code.google.com/codejam/contest/4304486/dashboard#s=p1
相关文章推荐
- 谷歌正式开始补偿Nexus 6P重启门和电池门用户:最高赔400美元
- 书评:《算法之美( Algorithms to Live By )》
- 动易2006序列号破解算法公布
- 谷歌、雅虎支持中文域名搜索 有助提升搜索引擎优化
- Ruby实现的矩阵连乘算法
- C#插入法排序算法实例分析
- 超大数据量存储常用数据库分表分库算法总结
- C#数据结构与算法揭秘二
- C#冒泡法排序算法实例分析
- 算法练习之从String.indexOf的模拟实现开始
- C#算法之关于大牛生小牛的问题
- C#实现的算24点游戏算法实例分析
- c语言实现的带通配符匹配算法
- 浅析STL中的常用算法
- 算法之排列算法与组合算法详解
- C++实现一维向量旋转算法
- Ruby实现的合并排序算法
- C#折半插入排序算法实现方法
- 基于C++实现的各种内部排序算法汇总
- C++线性时间的排序算法分析