落絮飞雁的个人网站

顺流而下,把梦做完

HDOJ1052:Tian Ji — The Horse Racing–简单贪心

Here is a famous story in Chinese history.

“That was about 2300 years ago. General Tian Ji was a high official in the country Qi. He likes to play horse racing with the king and others.”

“Both of Tian and the king have three horses in different classes, namely, regular, plus, and super. The rule is to have three rounds in a match; each of the horses must be used in one round. The winner of a single round takes two hundred silver dollars from the loser.”

“Being the most powerful man in the country, the king has so nice horses that in each class his horse is better than Tian’s. As a result, each time the king takes six hundred silver dollars from Tian.”

“Tian Ji was not happy about that, until he met Sun Bin, one of the most famous generals in Chinese history. Using a little trick due to Sun, Tian Ji brought home two hundred silver dollars and such a grace in the next match.”

“It was a rather simple trick. Using his regular class horse race against the super class from the king, they will certainly lose that round. But then his plus beat the king’s regular, and his super beat the king’s plus. What a simple trick. And how do you think of Tian Ji, the high ranked official in China?”

Were Tian Ji lives in nowadays, he will certainly laugh at himself. Even more, were he sitting in the ACM contest right now, he may discover that the horse racing problem can be simply viewed as finding the maximum matching in a bipartite graph. Draw Tian’s horses on one side, and the king’s horses on the other. Whenever one of Tian’s horses can beat one from the king, we draw an edge between them, meaning we wish to establish this pair. Then, the problem of winning as many rounds as possible is just to find the maximum matching in this graph. If there are ties, the problem becomes more complicated, he needs to assign weights 0, 1, or -1 to all the possible edges, and find a maximum weighted perfect matching…

However, the horse racing problem is a very special case of bipartite matching. The graph is decided by the speed of the horses — a vertex of higher speed always beat a vertex of lower speed. In this case, the weighted bipartite matching algorithm is a too advanced tool to deal with the problem.

In this problem, you are asked to write a program to solve this special case of matching problem.

Input
The input consists of up to 50 test cases. Each case starts with a positive integer n (n <= 1000) on the first line, which is the number of horses on each side. The next n integers on the second line are the speeds of Tian’s horses. Then the next n integers on the third line are the speeds of the king’s horses. The input ends with a line that has a single 0 after the last test case. Output For each input case, output a line containing a single number, which is the maximum money Tian Ji will get, in silver dollars. Sample Input 3 92 83 71 95 87 74 2 20 20 20 20 2 20 19 22 18 0 Sample Output 200 0 0 贪心问题的简单应用.首先将田忌的马和齐王的马做个排序.然后分类讨论

  • 田忌最快的马比齐王最快的快–直接跑赢;
  • 田忌最快的马比齐王最快的慢–用田忌最慢的马去跟齐王的马跑;
  • 田忌最快的马跟齐王最快的一样快–再比较田忌最慢的马是否比齐王的最慢的马快,如果是就直接跑赢,如果不是就用田忌最慢的马去跟齐王最快的马跑.
  • 放上代码:

    HDOJ1009:FatMouse' Trade——简单贪心

    Problem Description
    FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean.
    The warehouse has N rooms. The i-th room contains J[i] pounds of JavaBeans and requires F[i] pounds of cat food. FatMouse does not have to trade for all the JavaBeans in the room, instead, he may get J[i]* a% pounds of JavaBeans if he pays F[i]* a% pounds of cat food. Here a is a real number. Now he is assigning this homework to you: tell him the maximum amount of JavaBeans he can obtain.

    Input
    The input consists of multiple test cases. Each test case begins with a line containing two non-negative integers M and N. Then N lines follow, each contains two non-negative integers J[i] and F[i] respectively. The last test case is followed by two -1’s. All integers are not greater than 1000.

    Output
    For each test case, print in a single line a real number accurate up to 3 decimal places, which is the maximum amount of JavaBeans that FatMouse can obtain.

    Sample Input
    5 3
    7 2
    4 3
    5 2
    20 3
    25 18
    24 15
    15 10
    -1 -1

    Sample Output
    13.333
    31.500

    简单贪心问题。先利用结构体对J和F进行排序。然后依次累加即可。

    POJ3617:Best Cow Line——字典序最小问题

    Description

    FJ is about to take his N (1 ≤ N ≤ 2,000) cows to the annual”Farmer of the Year” competition. In this contest every farmer arranges his cows in a line and herds them past the judges.

    The contest organizers adopted a new registration scheme this year: simply register the initial letter of every cow in the order they will appear (i.e., If FJ takes Bessie, Sylvia, and Dora in that order he just registers BSD). After the registration phase ends, every group is judged in increasing lexicographic order according to the string of the initials of the cows’ names.

    FJ is very busy this year and has to hurry back to his farm, so he wants to be judged as early as possible. He decides to rearrange his cows, who have already lined up, before registering them.

    FJ marks a location for a new line of the competing cows. He then proceeds to marshal the cows from the old line to the new one by repeatedly sending either the first or last cow in the (remainder of the) original line to the end of the new line. When he’s finished, FJ takes his cows for registration in this new order.

    Given the initial order of his cows, determine the least lexicographic string of initials he can make this way.

    Input

    * Line 1: A single integer: N
    * Lines 2..N+1: Line i+1 contains a single initial (‘A’..’Z’) of the cow in the ith position in the original line

    Output

    The least lexicographic string he can make. Every line (except perhaps the last one) contains the initials of 80 cows (‘A’..’Z’) in the new line.

    Sample Input

    6
    A
    C
    D
    B
    C
    B
    Sample Output

    ABCBCD


    题目大意是给定一个长度为N的字符串S,要求构造一个字符串T。T的默认状态为空。然后随机将S的头部(或尾部)删除一个字符,加到T的尾部。

    要求构造一个字典序尽可能小的字符串T。

    思路:

    1. 按照字典序比较S和反转后的字符串S’
    2. 如果S较小,就从S开头取出字符,放到T的末尾
    3. 如果S’较小,就从S末位取出字符,放到T的末尾
    4. 如果相同,则比较(S+1)和(S’-1)两字符的大小;相同则继续。

    字典序+贪心问题。

    出现PE问题:每行最多80个字符

     

     

    区间调度问题

    题目描述:

    有n项工作,每项工作分别在si时间开始,ti时间结束。对于每项工作,你都可以选择参加或不参加,但选择了参加某项工作就必须至始至终参加全程参与,即参与工作的时间段不能有重叠(即使开始的瞬间和结束的瞬间重叠也是不允许的)。

    你的目标是参与尽可能多的工作,那么你最多能参与多少项工作呢?

    限制条件:

    1<=n<=100000

    1<=si<=ti,=10^9


    这个问题首先想到的就是贪心算法。但设计算法时容易出现错误。

    比较容易想到的算法如:

    • 在可选工作中,每次选取最先开始的工作
    • 在可选工作中,每次选取用时最短的工作
    • 在可选工作中,每次选取与最少可选工作有重叠的工作
    • 在可选工作中,每次选取结束时间最早的工作

    而很显然,前三项算法都不具有普适性(例如:一项开头早,持续时间长的工作和两项开头晚的工作)。

    关于最后一种算法的解释:

    结束时间早,对应的可选工作就越多。当然也可以用归纳法来证明。

    代码实现:

     

    HDOJ2037:今年暑假不AC

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 27554 Accepted Submission(s): 14558
    Problem Description
    “今年暑假不AC?”
    “是的。”
    “那你干什么呢?”
    “看世界杯呀,笨蛋!”
    “@#$%^&*%…”

    确实如此,世界杯来了,球迷的节日也来了,估计很多ACMer也会抛开电脑,奔向电视了。
    作为球迷,一定想看尽量多的完整的比赛,当然,作为新时代的好青年,你一定还会看一些其它的节目,比如新闻联播(永远不要忘记关心国家大事)、非常6+7、超级女生,以及王小丫的《开心辞典》等等,假设你已经知道了所有你喜欢看的电视节目的转播时间表,你会合理安排吗?(目标是能看尽量多的完整节目)

    Input
    输入数据包含多个测试实例,每个测试实例的第一行只有一个整数n(n<=100),表示你喜欢看的节目的总数,然后是n行数据,每行包括两个数据Ti_s,Ti_e (1<=i<=n),分别表示第i个节目的开始和结束时间,为了简化问题,每个时间都用一个正整数表示。n=0表示输入结束,不做处理。

    Output
    对于每个测试实例,输出能完整看到的电视节目的个数,每个测试实例的输出占一行。

    Sample Input
    12
    1 3
    3 4
    0 7
    3 8
    15 19
    15 20
    10 15
    8 18
    6 12
    5 10
    4 14
    2 9
    0

    Sample Output
    5

    动态规划+贪心,一开始想的太简单了。根本没有考虑到贪心。
    放上一开始的Wa代码:

    放上一段比较简单的代码吧,根据节目时间长短来排序。然后依次观看时间最短的节目。(WA代码)

    放上已经AC的C++代码。不是我写的。

    ACM常用算法

    转载自知乎,原作者:王晓。原文链接:http://www.zhihu.com/question/19719698/answer/13045301

     

    • 感谢邀请。
    • 网络上流传的答案有很多,估计提问者也曾经去网上搜过。所以根据自己微薄的经验提点看法。
    • 我ACM初期是训练编码能力,以水题为主(就是没有任何算法,自己靠动脑筋能够实现的),这种题目特点是麻烦,但是不难,30-50道题目就可以了。
    • 然后可以接触一下基础的算法,我感觉搜索方向的比较不错,可以解决很多问题,深搜,广搜,然后各种剪枝能力的锻炼。
    • 搜索感觉不错了就可以去看看贪心,图论,和动态规划方向的了。图论有最短路径,最小生成树,网络流,拓扑排序等等很多,动态规划先去书上看经典例子,最长公共子序列等。各种变形的题目。
    • 数学是ACM中极具杀伤力的武器,我一向很羡慕数学好的队友,精力有限自己数学方面的算法只能说入门。这方面经典的数论,组合数学方面的比较多,计算几何是很重要的,经典模型要熟悉,最近点对,二维三维,凸包以及各种应用。
    • 数据结构方面的就比较多了,基础的堆,栈,队列,并查集,二叉查找树,红黑树,trie树,hash表等等。
    • 用C++参赛的话STL要熟悉,有时候很有帮助,里面的queue,list,map,stack等。
    • ACM到后来算法就成了工具,不断的靠自己意淫一个新的解法来解决问题是最开心的事情了。我们学校ACM一直是一届带一届的,老师只提供经济上的援助,上面的内容是我在大三当队长时教给大一的新队员的入门内容,再深的就靠每个人自己发掘了。