优先队列+模拟-Fox and Number Game

codeforces-Fox and Number Game

题目地址:http://codeforces.com/contest/389/problem/A
Time Limit: 1000ms
Fox Ciel is playing a game with numbers now.
Ciel has n positive integers: x1, x2, ..., xn. She can do the following operation as many times as needed: select two different indexes i and j such that xi > xj hold, and then apply assignment xi = xi - xj. The goal is to make the sum of all numbers as small
as possible.
Please help Ciel to find this minimal sum.
Input
The first line contains an integer n (2 ≤ n ≤ 100). Then the second line contains n integers: x1, x2, ..., xn (1 ≤ xi ≤ 100).
Output
Output a single integer — the required minimal sum.
Sample Input
3
2 4 6
5
45 12 27 30 18
4
1 1 2 2
2
1 2

Sample Output

6

15

4

2

大意:每个测试用例是一个数组。找出xi和xj,(xi>xj),做运算令xi=xi-xj。可以做若干组这样的运算,使得最后的数组和最小。输出此和。

分析:用优先队列,每次找出最大的和次大的,处理后再加入此队列。注意多个相同的xi这种情况!

时间: 2024-10-05 16:25:32

优先队列+模拟-Fox and Number Game的相关文章

【哈夫曼编码】HDU2527-Safe Or Unsafe

Safe Or Unsafe Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1474    Accepted Submission(s): 582 Problem Description Javac++ 一天在看计算机的书籍的时候,看到了一个有趣的东西!每一串字符都可以被编码成一些数字来储存信息,但是不同的编码方式得到的储存空间是不一

codeforces B. Design Tutorial: Learn from Life

题意:有一个电梯,每一个人都想乘电梯到达自己想要到达的楼层!从a层到b层的时间是|a-b|, 乘客上下电梯的时间忽略不计!问最少需要多少的时间....     这是一道神题啊,自己的思路不知不觉的就按照注解的思路走了,想着用优先队列模拟一下,可能还是没有模拟好吧,一直哇!但是同学的 优先队列模拟过了! 没想到是greedy算法简单的几行就解决了! #include<iostream> #include<cmath> #include<cstdio> #include&l

UVa 550 Multiplying by Rotation:模拟乘法

550 - Multiplying by Rotation Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=100&page=show_problem&problem=491 Warning: Not all numbers in this problem are decimal numbers! Multiplic

模拟qq的复制聊天记录到发消息框

接上-----"模拟qq的消息接收"    qq 的消息发送界面提供了聊天记录,并且你可以通过鼠标轻松地.重复地把聊天记录复制到发消息框里,下面就是我提供的web页里实现的方法(注:此页面的来源为天乐的picq,小白只是添加了一个函数和自己的asp代码,实现了复制的功能!)下面是显示聊天记录页的主要代码:    <table border="0" width="100%" cellspacing="0" cellpadd

UVa 10706:Number Sequence

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1647 原题: A single positive integer i is given. Write a program to find the digit located in the position i in the sequence of n

UVa 10205 Stack &#039;em Up (模拟)

10205 - Stack 'em Up Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1146 A standard playing card deck contains 52 cards, 13 values in each of four suits.

UVa 12266 Stock Prices (优先队列)

12266 - Stock Prices Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=3418 In this problem we deal with the calculation of stock prices. You need to know the following things about stock p

算法题:POJ 1068 Parencodings 栈模拟

Let S = s1 s2...s2n be a well-formed string of parentheses. S can be encoded in two different ways: q By an integer sequence P = p1 p2...pn where pi is the number of left parentheses before the ith right parenthesis in S (P-sequence). q By an integer

JS+CSS实现仿触屏手机拨号盘界面及功能模拟完整实例

  本文实例讲述了JS+CSS实现仿触屏手机拨号盘界面及功能模拟的方法.分享给大家供大家参考.具体如下: 首先来看一下运行效果图: 具体实现代码如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 6