如何去寻找解题方案

如何去寻找解题方案

声明

本文是 How to Find a Solution 的翻译转述篇。原文作者为Dumitru。

简介

topcoder的很多问题都可以通过读题直接解答。原因在于很多题目在某种程度上都具有相似的模式。熟练的coder多数都掌握了从题目中发现相应的模式。这种模式可以是某种算法数据结构,也可以是某种抽象建模。
本文的目的就是让大家都能具有这种观察出题目内在模式的能力。

各种解题方法

简单题目

多数的SRM 250points的题目都是属于此类型。在这种类型题中,答题者主要需要把 题目的步骤描述转化为代码 。此种类型题目不考察算法,考察的为解题的速度和正确性。

简单模拟

BusinessTasks – SRM 236 Div 1:
N tasks are written down in the form of a circular list, so the first task is adjacent to the last one. A number n is also given. Starting with the first task, move clockwise (from element 1 in the list to element 2 in the list and so on), counting from 1 to n. When your count reaches n, remove that task from the list and start counting from the next available task. Repeat this procedure until one task remains. Return it.

以下是官方tutorial:

With n as large as ten million, you have to be a little careful about timing out if you perform a naive simulation of the process. However, you can use a bit of simple math to make it run instantaneously. If there are k things, and you want to count to n, starting from j, you will simply end up at (j+n)%k. Using this, its just a matter of removing the elements as specified, and running the simulation.

注意这里n如果特别大的话,仅仅是简单的模拟并不能完成,所以用简单数学来发现一些规律。

以下是我的python解法:

class BusinessTasks:
    def getTask(self, tasklist, n):
        cur=0
        l=list(tasklist)
        while len(l)>1:
            del l[(cur+n-1)%len(l)]
            cur=(cur+n-1)%(len(l)+1)
        return l[0]

if __name__ == "__main__":
    p= BusinessTasks()
    print p.getTask(("a","b","c","d"),2)

简单查找

TallPeople – SRM 208 Div 1:
A group of people stands before you arranged in rows and columns. Looking from above, they form an R by C rectangle of people. Your job is to return 2 specific heights – the first is computed by finding the shortest person in each row, and then finding the tallest person among them (the "tallest-of-the-shortest"); and the second is computed by finding the tallest person in each column, and then finding the shortest person among them (the "shortest-of-the-tallest").

以下是我的python解法:

class TallPeople:
    def __init__(self):
        pass
    def getPeople(self, people):
        heights=[[int(ele) for ele in row.split()] for row in people]
        a= max([min(row) for row in heights])
        b= min([max(row) for row in zip(*heights)])
        return (a,b)

if __name__ == "__main__":
    pass

你可能感兴趣的:(如何去寻找解题方案)