AtCoder Grand Contest 020 (2/6)

maksyuki 发表于 比赛 分类,标签:
0

A Move and Win
A game is played on a strip consisting of N cells consecutively numbered from 1 to N.

Alice has her token on cell A. Borys has his token on a different cell B.

Players take turns, Alice moves first. The moving player must shift his or her token from its current cell X to the neighboring cell on the left, cell X−1, or on the right, cell X+1. Note that it's disallowed to move the token outside the strip or to the cell with the other player's token. In one turn, the token of the moving player must be shifted exactly once.

The player who can't make a move loses, and the other player wins.

Both players want to win. Who wins if they play optimally?

Constraints
2≤N≤100
1≤A<B≤N
All input values are integers.
Input
Input is given from Standard Input in the following format:

N A B
Output
Print Alice if Alice wins, Borys if Borys wins, and Draw if nobody wins.

Sample Input 1
5 2 4
Sample Output 1
Alice
Alice can move her token to cell 3. After that, Borys will be unable to move his token to cell 3, so he will have to move his token to cell 5. Then, Alice moves her token to cell 4. Borys can't make a move and loses.

Sample Input 2
2 1 2
Sample Output 2
Borys
Alice can't make the very first move and loses.

Sample Input 3
58 23 42
Sample Output 3
Borys

 

题目类型:简单博弈

算法分析:简单分析可知只要Alice和Borys之间的距离是偶数则Alice赢,否则Borys赢

 

B Ice Rink Game
An adult game master and N children are playing a game on an ice rink. The game consists of K rounds. In the i-th round, the game master announces:

Form groups consisting of Ai children each!
Then the children who are still in the game form as many groups of Ai children as possible. One child may belong to at most one group. Those who are left without a group leave the game. The others proceed to the next round. Note that it's possible that nobody leaves the game in some round.

In the end, after the K-th round, there are exactly two children left, and they are declared the winners.

You have heard the values of A1, A2, ..., AK. You don't know N, but you want to estimate it.

Find the smallest and the largest possible number of children in the game before the start, or determine that no valid values of N exist.

Constraints
1≤K≤105
2≤Ai≤109
All input values are integers.
Input
Input is given from Standard Input in the following format:

K
A1 A2 … AK
Output
Print two integers representing the smallest and the largest possible value of N, respectively, or a single integer −1 if the described situation is impossible.

Sample Input 1
4
3 4 3 2
Sample Output 1
6 8
For example, if the game starts with 6 children, then it proceeds as follows:

In the first round, 6 children form 2 groups of 3 children, and nobody leaves the game.
In the second round, 6 children form 1 group of 4 children, and 2 children leave the game.
In the third round, 4 children form 1 group of 3 children, and 1 child leaves the game.
In the fourth round, 3 children form 1 group of 2 children, and 1 child leaves the game.
The last 2 children are declared the winners.

Sample Input 2
5
3 4 100 3 2
Sample Output 2
-1
This situation is impossible. In particular, if the game starts with less than 100 children, everyone leaves after the third round.

Sample Input 3
10
2 2 2 2 2 2 2 2 2 2
Sample Output 3
2 3

 

题目类型:简单数学

算法分析:设Li和Ri为经过Ki次操作后剩余的最少和最多的人数(不一定能够整除Ai),Xi和Yi为[Li,Ri]中满足整除Ai的最小和最大值,易知有Xi=ceil(Li/Ai)*Ai(也就是找到一个比Li稍微大一点的能够整除Ai的值),Yi=floor(Ri/Ai)*Ai(找到一个比Ri稍微小一点能够整除Ai的值),然后若Xi>Yi,即表示不存在合理的结果,否则Li-1=Xi,Ri-1=Yi+Ai-1,即给出满足条件的i-1轮的Xi-1和Yi-1的范围

 

Codeforces Educational Round 36(Rated for Div. 2) (3/7)

maksyuki 发表于 比赛 分类,标签:
0

A Garden
Luba thinks about watering her garden. The garden can be represented as a segment of length k. Luba has got n buckets, the i-th bucket allows her to water some continuous subsegment of garden of length exactly ai each hour. Luba can't water any parts of the garden that were already watered, also she can't water the ground outside the garden.

Luba has to choose one of the buckets in order to water the garden as fast as possible (as mentioned above, each hour she will water some continuous subsegment of length ai if she chooses the i-th bucket). Help her to determine the minimum number of hours she has to spend watering the garden. It is guaranteed that Luba can always choose a bucket so it is possible water the garden.

See the examples for better understanding.

Input
The first line of input contains two integer numbers n and k (1 ≤ n, k ≤ 100) — the number of buckets and the length of the garden, respectively.

The second line of input contains n integer numbers ai (1 ≤ ai ≤ 100) — the length of the segment that can be watered by the i-th bucket in one hour.

It is guaranteed that there is at least one bucket such that it is possible to water the garden in integer number of hours using only this bucket.

Output
Print one integer number — the minimum number of hours required to water the garden.

Examples
input
3 6
2 3 5
output
2
input
6 7
1 2 3 4 5 6
output
7
Note
In the first test the best option is to choose the bucket that allows to water the segment of length 3. We can't choose the bucket that allows to water the segment of length 5 because then we can't water the whole garden.

In the second test we can choose only the bucket that allows us to water the segment of length 1.

 

题目类型:简单枚举+贪心

算法分析:从最大容量的桶开始枚举,直到找到能够整除garden长度的容积

 

B Browser
Luba is surfing the Internet. She currently has n opened tabs in her browser, indexed from 1 to n from left to right. The mouse cursor is currently located at the pos-th tab. Luba needs to use the tabs with indices from l to r (inclusive) for her studies, and she wants to close all the tabs that don't belong to this segment as fast as possible.

Each second Luba can either try moving the cursor to the left or to the right (if the cursor is currently at the tab i, then she can move it to the tab max(i - 1, a) or to the tab min(i + 1, b)) or try closing all the tabs to the left or to the right of the cursor (if the cursor is currently at the tab i, she can close all the tabs with indices from segment [a, i - 1] or from segment [i + 1, b]). In the aforementioned expressions a and b denote the minimum and maximum index of an unclosed tab, respectively. For example, if there were 7 tabs initially and tabs 1, 2 and 7 are closed, then a = 3, b = 6.

What is the minimum number of seconds Luba has to spend in order to leave only the tabs with initial indices from l to r inclusive opened?

Input
The only line of input contains four integer numbers n, pos, l, r (1 ≤ n ≤ 100, 1 ≤ pos ≤ n, 1 ≤ l ≤ r ≤ n) — the number of the tabs, the cursor position and the segment which Luba needs to leave opened.

Output
Print one integer equal to the minimum number of seconds required to close all the tabs outside the segment [l, r].

Examples
input
6 3 2 4
output
5
input
6 3 1 3
output
1
input
5 2 1 5
output
0
Note
In the first test Luba can do the following operations: shift the mouse cursor to the tab 2, close all the tabs to the left of it, shift the mouse cursor to the tab 3, then to the tab 4, and then close all the tabs to the right of it.

In the second test she only needs to close all the tabs to the right of the current position of the cursor.

In the third test Luba doesn't need to do anything.

 

题目类型:简单构造+贪心

算法分析:如果pos不在[l,r]之间的话,最优的操作是每次执行左移或右移使得pos到达l或者是r位置,因为del操作可以一次性删除,在还没到达l或者r之前使用del操作不是最优的。然后问题转换成pos在[l,r]之间的情况,此时若l和1,r和n之间还有tab则分类讨论,没有tab则不需要向给定方向移动+del,注意若两边都有tab,则需先向距离边界l,r小的移动

 

C Permute Digits
You are given two positive integer numbers a and b. Permute (change order) of the digits of a to construct maximal number not exceeding b. No number in input and/or output can start with the digit 0.

It is allowed to leave a as it is.

Input
The first line contains integer a (1 ≤ a ≤ 1018). The second line contains integer b (1 ≤ b ≤ 1018). Numbers don't have leading zeroes. It is guaranteed that answer exists.

Output
Print the maximum possible number that is a permutation of digits of a and is not greater than b. The answer can't have any leading zeroes. It is guaranteed that the answer exists.

The number in the output should have exactly the same length as number a. It should be a permutation of digits of a.

Examples
input
123
222
output
213
input
3921
10000
output
9321
input
4940
5000
output
4940

 

题目类型:数位构造+贪心

算法分析:若a位数小于b位数则a直接从高位开始贪心地放最大的数字。若a和b的位数相同,则需判断a每次从高位放置尽可能大的数字时是否能够构造出a。可以使用dfs递减搜索每一位并维护一个tag,若tag为false则表示a的前面高位中已经有小于对应b位的情况,此时放置数字时可以不用考虑b对应位的大小了,否则需要考虑

 

uva1592

maksyuki 发表于 oj 分类,标签: ,
0

Peter studies the theory of relational databases. Table in the relational database consists of values that are arranged in rows and columns.

There are different normal forms that database may adhere to. Normal forms are designed to minimize the redundancy of data in the database. For example, a database table for a library might have a row for each book and columns for book name, book author, and author’s email.

If the same author wrote several books, then this representation is clearly redundant. To formally define this kind of redundancy Peter has introduced his own normal form. A table is in Peter’s Normal Form (PNF) if and only if there is no pair of rows and a pair of columns such that the values in the corresponding columns are the same for both rows.

How to compete in ACM ICPC Peter peter@neerc.ifmo.ru

How to win ACM ICPC Michael michael@neerc.ifmo.ru

Notes from ACM ICPC champion Michael michael@neerc.ifmo.ru

The above table is clearly not in PNF, since values for 2rd and 3rd columns repeat in 2nd and 3rd rows. However, if we introduce unique author identifier and split this table into two tables — one containing book name and author id, and the other containing book id, author name, and author email, then both resulting tables will be in PNF.

How to compete in ACM ICPC 1

How to win ACM ICPC 2

Notes from ACM ICPC champion 2

1 Peter peter@neerc.ifmo.ru

2 Michael michael@neerc.ifmo.ru

Given a table your task is to figure out whether it is in PNF or not.

Input

Input contains several datasets. The first line of each dataset contains two integer numbers n and m (1 ≤ n ≤ 10000, 1 ≤ m ≤ 10), the number of rows and columns in the table. The following n lines contain table rows. Each row has m column values separated by commas. Column values consist of ASCII characters from space (ASCII code 32) to tilde (ASCII code 126) with the exception of comma (ASCII code 44). Values are not empty and have no leading and trailing spaces. Each row has at most 80 characters (including separating commas).

Output

For each dataset, if the table is in PNF write to the output file a single word “YES” (without quotes). If the table is not in PNF, then write three lines. On the first line write a single word “NO” (without quotes). On the second line write two integer row numbers r1 and r2 (1 ≤ r1, r2 ≤ n, r1 ̸= r2), on the third line write two integer column numbers c1 and c2 (1 ≤ c1, c2 ≤ m, c1 ̸= c2), so that values in columns c1 and c2 are the same in rows r1 and r2.

Sample Input

3 3
How to compete in ACM ICPC,Peter,peter@neerc.ifmo.ru
How to win ACM ICPC,Michael,michael@neerc.ifmo.ru
Notes from ACM ICPC champion,Michael,michael@neerc.ifmo.ru
2 3
1,Peter,peter@neerc.ifmo.ru
2,Michael,michael@neerc.ifmo.ru

Sample Output

NO

2 3

2 3

YES

 

题目类型:简单字符处理+二分搜索

算法分析:由于n(10000)比m(10)大得多,所以可以考虑二重枚举m,然后依次枚举每一行,用map记录当前行的两个字符串的行标号并查找当前行的两个字符串是否出现过,若是则直接break后输出结果,复杂度是O(m^2nlogn)。注意两字符串若出现则只会出现一次并且查找后没有break会TLE,由于每次查找的是给定两列的行标,所以每次枚举行时需要清除dict,否则会WA

 

Codeforces Round Hello 2018 (4/8)

maksyuki 发表于 比赛 分类,标签:
0

A Modular Exponentiation
The following problem is well-known: given integers n and m, calculate,
where 2n = 2·2·...·2 (n factors), and denotes the remainder of division of x by y.
You are asked to solve the "reverse" problem. Given integers n and m, calculate.

Input
The first line contains a single integer n (1 ≤ n ≤ 108).
The second line contains a single integer m (1 ≤ m ≤ 108).
Output
Output a single integer — the value of.
Examples
input
4
42
output
10
input
1
58
output
0
input
98765432
23456789
output
23456789
Note
In the first example, the remainder of division of 42 by 24 = 16 is equal to 10.
In the second example, 58 is divisible by 21 = 2 without remainder, and the answer is 0.

 

题目类型:简单模运算

算法分析:当m<2^n时输出m,即当n>=27时,直接输出m,否则输出m%(2^n)

 

B Christmas Spruce
Consider a rooted tree. A rooted tree has one special vertex called the root. All edges are directed from the root. Vertex u is called a child of vertex v and vertex v is called a parent of vertex u if there exists a directed edge from v to u. A vertex is called a leaf if it doesn't have children and has a parent.

Let's call a rooted tree a spruce if its every non-leaf vertex has at least 3 leaf children. You are given a rooted tree, check whether it's a spruce.

The definition of a rooted tree can be found here.

Input
The first line contains one integer n — the number of vertices in the tree (3 ≤ n ≤ 1 000). Each of the next n - 1 lines contains one integer pi (1 ≤ i ≤ n - 1) — the index of the parent of the i + 1-th vertex (1 ≤ pi ≤ i).

Vertex 1 is the root. It's guaranteed that the root has at least 2 children.

Output
Print "Yes" if the tree is a spruce and "No" otherwise.

Examples
input
4
1
1
1
output
Yes
input
7
1
1
1
2
2
2
output
No
input
8
1
1
1
1
3
3
3
output
Yes

 

题目类型:简单枚举

算法分析:输入所有节点构建完树之后,查找是否存在小于3个叶子节点的非叶节点,复杂度O(n^2)

 

C Party Lemonade
A New Year party is not a New Year party without lemonade! As usual, you are expecting a lot of guests, and buying lemonade has already become a pleasant necessity.

Your favorite store sells lemonade in bottles of n different volumes at different costs. A single bottle of type i has volume 2i - 1 liters and costs ci roubles. The number of bottles of each type in the store can be considered infinite.

You want to buy at least L liters of lemonade. How many roubles do you have to spend?

Input
The first line contains two integers n and L (1 ≤ n ≤ 30; 1 ≤ L ≤ 109) — the number of types of bottles in the store and the required amount of lemonade in liters, respectively.

The second line contains n integers c1, c2, ..., cn (1 ≤ ci ≤ 109) — the costs of bottles of different types.

Output
Output a single integer — the smallest number of roubles you have to pay in order to buy at least L liters of lemonade.

Examples
input
4 12
20 30 70 90
output
150
input
4 3
10000 1000 100 10
output
10
input
4 3
10 100 1000 10000
output
30
input
5 787787787
123456789 234567890 345678901 456789012 987654321
output
44981600785557577

Note
In the first example you should buy one 8-liter bottle for 90 roubles and two 2-liter bottles for 30 roubles each. In total you'll get 12 liters of lemonade for just 150 roubles.

In the second example, even though you need only 3 liters, it's cheaper to buy a single 8-liter bottle for 10 roubles.

In the third example it's best to buy three 1-liter bottles for 10 roubles each, getting three liters for 30 roubles.

 

题目类型:贪心+二进制枚举

算法分析:首先若满足2*c[i]<=c[i+1]的话,则选择c[i]至少不会比c[i+1]更差,先使用c[i+1]=min(c[i+1], 2 * c[i])对c[i]排序,以便后续处理,这样c[i]满足关系2*c[i]>=c[i+1],即若第i(i<n)个物品选择超过1个,则由前面可知超出的不如选择第i+1个物品划算,因为这个关系具有传递性,可知i<n时每个物品最多选一个,i=n可以选多个。由于第i个物品的容积是2^i(以0为起始点),所以若组成容积L,其每个二进制表达式的第i位(i<n)即表示第i个物品选还是不选,若组成不小于L的容积V,则易知满足V与L的二进制表示的最高不同位上V的为1,L的为0,并且V之后的低位都为0,因为只要不同的最高位上V为1,则一定有V>L,而低位为0表示不选之后的容积,此时才能保证得到是V比L大的最小花费。比如L=(11010)2,V=(11100)2,V'=(11101)2,易知V>L,且V'>L,但由前面分析可知V的花费要小于V'的。此时可以从高到低枚举L的所有位i,并不断生成第i位上为1,低位均为0的V,并比较两者花费,复杂度O(n)

 

D Too Easy Problems
You are preparing for an exam on scheduling theory. The exam will last for exactly T milliseconds and will consist of n problems. You can either solve problem i in exactly ti milliseconds or ignore it and spend no time. You don't need time to rest after solving a problem, either.

Unfortunately, your teacher considers some of the problems too easy for you. Thus, he assigned an integer ai to every problem i meaning that the problem i can bring you a point to the final score only in case you have solved no more than ai problems overall (including problem i).

Formally, suppose you solve problems p1, p2, ..., pk during the exam. Then, your final score s will be equal to the number of values of j between 1 and k such that k ≤ apj.

You have guessed that the real first problem of the exam is already in front of you. Therefore, you want to choose a set of problems to solve during the exam maximizing your final score in advance. Don't forget that the exam is limited in time, and you must have enough time to solve all chosen problems. If there exist different sets of problems leading to the maximum final score, any of them will do.

Input
The first line contains two integers n and T (1 ≤ n ≤ 2·105; 1 ≤ T ≤ 109) — the number of problems in the exam and the length of the exam in milliseconds, respectively.

Each of the next n lines contains two integers ai and ti (1 ≤ ai ≤ n; 1 ≤ ti ≤ 104). The problems are numbered from 1 to n.

Output
In the first line, output a single integer s — your maximum possible final score.

In the second line, output a single integer k (0 ≤ k ≤ n) — the number of problems you should solve.

In the third line, output k distinct integers p1, p2, ..., pk (1 ≤ pi ≤ n) — the indexes of problems you should solve, in any order.

If there are several optimal sets of problems, you may output any of them.

Examples
input
5 300
3 100
4 150
4 80
2 90
2 300
output
2
3
3 1 4
input
2 100
1 787
2 788
output
0
0

input
2 100
2 42
2 58
output
2
2
1 2
Note
In the first example, you should solve problems 3, 1, and 4. In this case you'll spend 80 + 100 + 90 = 270 milliseconds, falling within the length of the exam, 300 milliseconds (and even leaving yourself 30 milliseconds to have a rest). Problems 3 and 1 will bring you a point each, while problem 4 won't. You'll score two points.

In the second example, the length of the exam is catastrophically not enough to solve even a single problem.

In the third example, you have just enough time to solve both problems in 42 + 58 = 100 milliseconds and hand your solutions to the teacher with a smile.

 

题目类型:贪心+二分枚举

算法分析:贪心选择只会对得分有帮助的problem,先在[0,n]内二分枚举k的值,然后找到满足a[i]大于等于k的problem,若其个数大于等于k并从小到大累加其时间后不超过T,则k是一个合理解,否则不合理,复杂度O(nlog^2n)

 

算法分析:另一个解法类似于尺取法,从k=n开始递减枚举,先将a[i]为k的都压入到集合中,即保证集合中的元素都是大于等于当前k的,若集合个数大于k则优先删除花费时间多的,复杂度O(nlogn)

 

 

Codeforces Round #456(Div.2) (2/5)

maksyuki 发表于 比赛 分类,标签:
0

A Tricky Alchemy
During the winter holidays, the demand for Christmas balls is exceptionally high. Since it's already 2018, the advances in alchemy allow easy and efficient ball creation by utilizing magic crystals.

Grisha needs to obtain some yellow, green and blue balls. It's known that to produce a yellow ball one needs two yellow crystals, green — one yellow and one blue, and for a blue ball, three blue crystals are enough.

Right now there are A yellow and B blue crystals in Grisha's disposal. Find out how many additional crystals he should acquire in order to produce the required number of balls.

Input
The first line features two integers A and B (0 ≤ A, B ≤ 109), denoting the number of yellow and blue crystals respectively at Grisha's disposal.

The next line contains three integers x, y and z (0 ≤ x, y, z ≤ 109) — the respective amounts of yellow, green and blue balls to be obtained.

Output
Print a single integer — the minimum number of crystals that Grisha should acquire in addition.

Examples
input
4 3
2 1 1
output
2
input
3 9
1 1 3
output
1
input
12345678 87654321
43043751 1000000000 53798715
output
2147483648
Note
In the first sample case, Grisha needs five yellow and four blue crystals to create two yellow balls, one green ball, and one blue ball. To do that, Grisha needs to obtain two additional crystals: one yellow and one blue.

 

题目类型:简单数学

算法分析:所需red和blue颜色的最少水晶数量是独立的,直接输出max(0, 2x+y-a)+max(0, 3z+y-b)

 

B New Year's Eve
Since Grisha behaved well last year, at New Year's Eve he was visited by Ded Moroz who brought an enormous bag of gifts with him! The bag contains n sweet candies from the good ol' bakery, each labeled from 1 to n corresponding to its tastiness. No two candies have the same tastiness.

The choice of candies has a direct effect on Grisha's happiness. One can assume that he should take the tastiest ones — but no, the holiday magic turns things upside down. It is the xor-sum of tastinesses that matters, not the ordinary sum!

A xor-sum of a sequence of integers a1, a2, ..., am is defined as the bitwise XOR of all its elements: , here denotes the bitwise XOR operation; more about bitwise XOR can be found here.

Ded Moroz warned Grisha he has more houses to visit, so Grisha can take no more than k candies from the bag. Help Grisha determine the largest xor-sum (largest xor-sum means maximum happiness!) he can obtain.

Input
The sole string contains two integers n and k (1 ≤ k ≤ n ≤ 1018).

Output
Output one number — the largest possible xor-sum.

Examples
input
4 3
output
7
input
6 6
output

7
Note
In the first sample case, one optimal answer is 1, 2 and 4, giving the xor-sum of 7.

In the second sample case, one can, for example, take all six candies and obtain the xor-sum of 7.

 

题目类型:异或运算

算法分析:当k=1时直接输出n,当k>1时,记n的MSB为q(以0为起始点),则总可以通过2^q和2^q - 1的异或运算得到q+1个位均为1的最大值,即2^(q+1)-1

 

uva400

maksyuki 发表于 oj 分类,标签: ,
0

The computer company you work for is introducing a brand new computer line and is developing a new Unix-like operating system to be introduced along with the new computer. Your assignment is to write the formatter for the ls function.

Your program will eventually read input from a pipe (although for now your program will read from the input file). Input to your program will consist of a list of (F) filenames that you will sort (ascending based on the ASCII character values) and format into (C) columns based on the length (L) of the longest filename. Filenames will be between 1 and 60 (inclusive) characters in length and will be formatted into left-justified columns. The rightmost column will be the width of the longest filename and all other columns will be the width of the longest filename plus 2. There will be as many columns as will fit in 60 characters. Your program should use as few rows (R) as possible with rows being filled to capacity from left to right.

Input

The input file will contain an indefinite number of lists of filenames. Each list will begin with a line containing a single integer (1 ≤ N ≤ 100). There will then be N lines each containing one left-justified filename and the entire line’s contents (between 1 and 60 characters) are considered to be part of the filename. Allowable characters are alphanumeric (a to z, A to Z, and 0 to 9) and from the following set {._-} (not including the curly braces). There will be no illegal characters in any of the filenames and no line will be completely empty.

Immediately following the last filename will be the N for the next set or the end of file. You should read and format all sets in the input file.

Output

For each set of filenames you should print a line of exactly 60 dashes (-) followed by the formatted columns of filenames. The sorted filenames 1 to R will be listed down column 1; filenames R + 1 to 2R listed down column 2; etc.

Sample Input

10
tiny
2short4me
very_long_file_name
shorter
size-1
size2
size3
much_longer_name
12345678.123
mid_size_name
12
Weaser
Alfalfa
Stimey
Buckwheat
Porky
Joe
Darla
Cotton
Butch
Froggy
Mrs_Crabapple
P.D.
19
Mr._French
Jody
Buffy
Sissy
Keith
Danny
Lori
Chris
Shirley
Marsha
Jan
Cindy
Carol
Mike
Greg
Peter
Bobby
Alice
Ruben
Sample Output

------------------------------------------------------------
12345678.123                  size-1
2short4me                        size2
mid_size_name                size3
much_longer_name         tiny
shorter                             very_long_file_name
------------------------------------------------------------
Alfalfa             Cotton            Joe                      Porky
Buckwheat     Darla             Mrs_Crabapple    Stimey
Butch              Froggy          P.D.                      Weaser
------------------------------------------------------------
Alice            Chris     Jan      Marsha         Ruben
Bobby         Cindy    Jody    Mike              Shirley
Buffy           Danny   Keith  Mr._French Sissy
Carol           Greg      Lori     Peter

 

题目类型:简单字符处理

算法分析:将string排序后按照尽可能排在一行的贪心策略输出即可

 

uva540

maksyuki 发表于 oj 分类,标签:
0

Queues and Priority Queues are data structures which are known to most computer scientists. The Team Queue, however, is not so well known, though it occurs often in everyday life. At lunch time the queue in front of the Mensa is a team queue, for example.

In a team queue each element belongs to a team. If an element enters the queue, it first searches the queue from head to tail to check if some of its teammates (elements of the same team) are already in the queue. If yes, it enters the queue right behind them. If not, it enters the queue at the tail and becomes the new last element (bad luck). Dequeuing is done like in normal queues: elements are processed from head to tail in the order they appear in the team queue.

Your task is to write a program that simulates such a team queue.

Input

The input file will contain one or more test cases. Each test case begins with the number of teams t (1 ≤ t ≤ 1000). Then t team descriptions follow, each one consisting of the number of elements belonging to the team and the elements themselves. Elements are integers in the range 0..999999. A team may consist of up to 1000 elements.

Finally, a list of commands follows. There are three different kinds of commands:

• ENQUEUE x — enter element x into the team queue

• DEQUEUE — process the first element and remove it from the queue

• STOP — end of test case

The input will be terminated by a value of 0 for t.

Warning: A test case may contain up to 200000 (two hundred thousand) commands, so the implementation of the team queue should be efficient: both enqueing and dequeuing of an element should only take constant time.

Output

For each test case, first print a line saying ‘Scenario #k’, where k is the number of the test case. Then, for each ‘DEQUEUE’ command, print the element which is dequeued on a single line. Print a blank line after each test case, even after the last one.

Sample Input

2
3 101 102 103
3 201 202 203
ENQUEUE 101
ENQUEUE 201
ENQUEUE 102
ENQUEUE 202
ENQUEUE 103
ENQUEUE 203
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
STOP
2
5 259001 259002 259003 259004 259005
6 260001 260002 260003 260004 260005 260006
ENQUEUE 259001
ENQUEUE 260001
ENQUEUE 259002
ENQUEUE 259003
ENQUEUE 259004
ENQUEUE 259005
DEQUEUE
DEQUEUE
ENQUEUE 260002
ENQUEUE 260003
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
STOP
0
Sample Output

Scenario #1
101
102
103
201
202
203
Scenario #2
259001
259002
259003
259004
259005
260001

 

题目类型:简单队列处理

算法分析:维护一个团队队列和每个组的队列,然后按照要求模拟操作即可

 

uva12096

maksyuki 发表于 oj 分类,标签:
0

Background from Wikipedia: “Set theory is a branch of mathematics created principally by the German mathematician Georg Cantor at the end of the 19th century. Initially controversial, set theory has come to play the role of a foundational theory in modern mathematics, in the sense of a theory invoked to justify assumptions made in mathematics concerning the existence of mathematical objects (such as numbers or functions) and their properties. Formal versions of set theory also have a foundational role to play as specifying a theoretical ideal of mathematical rigor in proofs.”

Given this importance of sets, being the basis of mathematics, a set of eccentric theorist set off to construct a supercomputer operating on sets instead of numbers. The initial SetStack Alpha is under construction, and they need you to simulate it in order to verify the operation of the prototype.

The computer operates on a single stack of sets, which is initially empty. After each operation, the cardinality of the topmost set on the stack is output. The cardinality of a set S is denoted |S| and is the number of elements in S. The instruction set of the SetStack Alpha is PUSH, DUP, UNION, INTERSECT, and ADD.

• PUSH will push the empty set {} on the stack.

• DUP will duplicate the topmost set (pop the stack, and then push that set on the stack twice).

• UNION will pop the stack twice and then push the union of the two sets on the stack.

• INTERSECT will pop the stack twice and then push the intersection of the two sets on the stack.

• ADD will pop the stack twice, add the first set to the second one, and then push the resulting set on the stack.

For illustration purposes, assume that the topmost element of the stack is

A = {{}, {{}}}

and that the next one is

B = {{}, {{{}}}}

For these sets, we have |A| = 2 and |B| = 2. Then:

• UNION would result in the set {{}, {{}}, {{{}}}}. The output is 3.

• INTERSECT would result in the set {{}}. The output is 1.

• ADD would result in the set {{}, {{{}}}, {{},{{}}}}. The output is 3.

Input

An integer 0 ≤ T ≤ 5 on the first line gives the cardinality of the set of test cases. The first line of each test case contains the number of operations 0 ≤ N ≤ 2000. Then follow N lines each containing one of the five commands. It is guaranteed that the SetStack computer can execute all the commands in the sequence without ever popping an empty stack.

Output

For each operation specified in the input, there will be one line of output consisting of a single integer. This integer is the cardinality of the topmost element of the stack after the corresponding command has executed. After each test case there will be a line with ‘***’ (three asterisks).

Sample Input

2
9
PUSH
DUP
ADD
PUSH
ADD
DUP
ADD
DUP
UNION
5
PUSH
PUSH
ADD
PUSH
INTERSECT
Sample Output

0
0
1
0
1
1
2
2
2
***
0
0
1
0
0
***

 

题目类型:简单数据结构

算法分析:将每个集合用int类型的ID表示,然后整个栈压入每个集合的ID,然后按照要求模拟操作即可,注意使用set_union等集合操作函数时中的sc每次都要重新定义,因为操作函数是使用inserter将数据插入到迭代器的开始端

 

uva156

maksyuki 发表于 oj 分类,标签:
0

Most crossword puzzle fans are used to anagrams — groups of words with the same letters in different orders — for example OPTS, SPOT, STOP, POTS and POST. Some words however do not have this attribute, no matter how you rearrange their letters, you cannot form another word. Such words are called ananagrams, an example is QUIZ.

Obviously such definitions depend on the domain within which we are working; you might think that ATHENE is an ananagram, whereas any chemist would quickly produce ETHANE. One possible domain would be the entire English language, but this could lead to some problems. One could restrict the domain to, say, Music, in which case SCALE becomes a relative ananagram (LACES is not in the same domain) but NOTE is not since it can produce TONE.

Write a program that will read in the dictionary of a restricted domain and determine the relative ananagrams. Note that single letter words are, ipso facto, relative ananagrams since they cannot be “rearranged” at all. The dictionary will contain no more than 1000 words.

Input

Input will consist of a series of lines. No line will be more than 80 characters long, but may contain any number of words. Words consist of up to 20 upper and/or lower case letters, and will not be broken across lines. Spaces may appear freely around words, and at least one space separates multiple words on the same line. Note that words that contain the same letters but of differing case are considered to be anagrams of each other, thus ‘tIeD’ and ‘EdiT’ are anagrams. The file will be terminated by a line consisting of a single ‘#’.

Output

Output will consist of a series of lines. Each line will consist of a single word that is a relative ananagram in the input dictionary. Words must be output in lexicographic (case-sensitive) order. There will always be at least one relative ananagram.

Sample Input

ladder came tape soon leader acme RIDE lone Dreis peat
ScAlE orb eye Rides dealer NotE derail LaCeS drIed
noel dire Disk mace Rob dries
#
Sample Output

Disk

NotE

derail

drIed

eye

ladder

soon

 

题目类型:简单字符处理

算法分析:将每个string标准化后判断其是否只有一个即可

 

uva10815

maksyuki 发表于 oj 分类,标签:
0

Andy, 8, has a dream - he wants to produce his very own dictionary. This is not an easy task for him, as the number of words that he knows is, well, not quite enough. Instead of thinking up all the words himself, he has a briliant idea. From his bookshelf he would pick one of his favourite story books, from which he would copy out all the distinct words. By arranging the words in alphabetical order, he is done! Of course, it is a really time-consuming job, and this is where a computer program is helpful.

You are asked to write a program that lists all the different words in the input text. In this problem, a word is defined as a consecutive sequence of alphabets, in upper and/or lower case. Words with only one letter are also to be considered. Furthermore, your program must be CaSe InSeNsItIvE. For example, words like “Apple”, “apple” or “APPLE” must be considered the same.

Input

The input file is a text with no more than 5000 lines. An input line has at most 200 characters. Input is terminated by EOF.

Output

Your output should give a list of different words that appears in the input text, one in a line. The words should all be in lower case, sorted in alphabetical order. You can be sure that he number of distinct words in the text does not exceed 5000.

Sample Input

Adventures in Disneyland

Two blondes were going to Disneyland when they came to a fork in the

road. The sign read: "Disneyland Left." So they went home.

Sample Output

a
adventures
blondes
came
disneyland
fork
going
home
in
left
read
road
sign
so
the
they
to
two
went
were
when

 

题目类型:简单字符处理

算法分析:将输入字符串转换成小写后直接放到set中输出即可