天天看点

Educational Codeforces Round 33

A. Chess For Three time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard output Alex, Bob and Carl will soon participate in a team chess tournament. Since they are all in the same team, they have decided to practise really hard before the tournament. But it’s a bit difficult for them because chess is a game for two players, not three. So they play with each other according to following rules: Alex and Bob play the first game, and Carl is spectating; When the game ends, the one who lost the game becomes the spectator in the next game, and the one who was spectating plays against the winner. Alex, Bob and Carl play in such a way that there are no draws. Today they have played n games, and for each of these games they remember who was the winner. They decided to make up a log of games describing who won each game. But now they doubt if the information in the log is correct, and they want to know if the situation described in the log they made up was possible (that is, no game is won by someone who is spectating if Alex, Bob and Carl play according to the rules). Help them to check it! Input The first line contains one integer n (1 ≤ n ≤ 100) — the number of games Alex, Bob and Carl played. Then n lines follow, describing the game log. i-th line contains one integer ai (1 ≤ ai ≤ 3) which is equal to 1 if Alex won i-th game, to 2 if Bob won i-th game and 3 if Carl won i-th game. Output Print YES if the situation described in the log was possible. Otherwise print NO. Examples input 3 1 2 output YES NO Note In the first example the possible situation is: Alex wins, Carl starts playing instead of Bob; Alex wins, Bob replaces Carl; Bob wins. The situation in the second example is impossible because Bob loses the first game, so he cannot win the second one.

用一个flag数组来记录是哪两个在比赛,如果胜利的人在flag数组标记的正在比赛的两人输出NO跳出,否则将失败和旁观的人flag数组的状态改变,直到最后都没跳出的输出YES。

B. Beautiful Divisors time limit per test2 seconds Recently Luba learned about a special kind of numbers that she calls beautiful numbers. The number is called beautiful iff its binary representation consists of k + 1 consecutive ones, and then k consecutive zeroes. Some examples of beautiful numbers: 12(110); 1102(610); 11110002(12010); 1111100002(49610). More formally, the number is beautiful iff there exists some positive integer k such that the number is equal to (2k − 1) ∗ (2k − 1). Luba has got an integer number n, and she wants to find its greatest beautiful divisor. Help her to find it! The only line of input contains one number n (1 ≤ n ≤105) — the number Luba has got. Output one number — the greatest beautiful divisor of Luba’s number. It is obvious that the answer always exists. 992 496

其实在1−105范围内满足(2k − 1) ∗ (2k − 1)式子的只有八个{1,6,28,120,496,2016,8128,32640}直接打表,找n最大的除数输出即可。

C. Rumor Vova promised himself that he would never play computer games… But recently Firestorm — a well-known game developing company — published their newest game, World of Farcraft, and it became really popular. Of course, Vova started playing it. Now he tries to solve a quest. The task is to come to a settlement named Overcity and spread a rumor in it. Vova knows that there are n characters in Overcity. Some characters are friends to each other, and they share information they got. Also Vova knows that he can bribe each character so he or she starts spreading the rumor; i-th character wants ci gold in exchange for spreading the rumor. When a character hears the rumor, he tells it to all his friends, and they start spreading the rumor to their friends (for free), and so on. The quest is finished when all n characters know the rumor. What is the minimum amount of gold Vova needs to spend in order to finish the quest? Take a look at the notes if you think you haven’t understood the problem completely. The first line contains two integer numbers n and m (1 ≤ n ≤ 105, 0 ≤ m ≤ 105) — the number of characters in Overcity and the number of pairs of friends. The second line contains n integer numbers ci (0 ≤ ci ≤ 109) — the amount of gold i-th character asks to start spreading the rumor. Then m lines follow, each containing a pair of numbers (xi, yi) which represent that characters xi and yi are friends (1 ≤ xi, yi ≤ n, xi ≠ yi). It is guaranteed that each pair is listed at most once. Print one number — the minimum amount of gold Vova has to spend in order to finish the quest. 5 2 2 5 3 4 8 1 4 4 5 10 10 0 1 2 3 4 5 6 7 8 9 10 55 10 5 1 6 2 7 3 8 4 9 5 10 1 2 3 4 5 6 7 8 9 10 15 In the first example the best decision is to bribe the first character (he will spread the rumor to fourth character, and the fourth one will spread it to fifth). Also Vova has to bribe the second and the third characters, so they know the rumor. In the second example Vova has to bribe everyone. In the third example the optimal decision is to bribe the first, the third, the fifth, the seventh and the ninth characters.

裸的并查集题目,直接向花钱少的合并,最后将所有父亲的钱加起来。

D. Credit Card Recenlty Luba got a credit card and started to use it. Let’s consider n consecutive days Luba uses the card. She starts with 0 money on her account. In the evening of i-th day a transaction ai occurs. If ai > 0, then ai bourles are deposited to Luba’s account. If ai < 0, then ai bourles are withdrawn. And if ai = 0, then the amount of money on Luba’s account is checked. In the morning of any of n days Luba can go to the bank and deposit any positive integer amount of burles to her account. But there is a limitation: the amount of money on the account can never exceed d. It can happen that the amount of money goes greater than d by some transaction in the evening. In this case answer will be «-1». Luba must not exceed this limit, and also she wants that every day her account is checked (the days when ai = 0) the amount of money on her account is non-negative. It takes a lot of time to go to the bank, so Luba wants to know the minimum number of days she needs to deposit some money to her account (if it is possible to meet all the requirements). Help her! The first line contains two integers n, d (1 ≤ n ≤ 105, 1 ≤ d ≤ 109) —the number of days and the money limitation. The second line contains n integer numbers a1, a2, … an ( - 104 ≤ ai ≤ 104), where ai represents the transaction in i-th day. Print -1 if Luba cannot deposit the money to her account in such a way that the requirements are met. Otherwise print the minimum number of days Luba has to deposit money. 5 10 -1 5 0 -5 3 -10 0 20 -1 -5 0 10 -11 0
上一篇: 六度空间

继续阅读