Codeforces Round #401 (Div. 2) A---E

来源:互联网 发布:可以制作手机壁纸软件 编辑:程序博客网 时间:2024/05/19 23:00
A. Shell Game
time limit per test
0.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Bomboslav likes to look out of the window in his room and watch lads outside playing famous shell game. The game is played by two persons: operator and player. Operator takes three similar opaque shells and places a ball beneath one of them. Then he shuffles the shells by swapping some pairs and the player has to guess the current position of the ball.

Bomboslav noticed that guys are not very inventive, so the operator always swaps the left shell with the middle one during odd moves (first, third, fifth, etc.) and always swaps the middle shell with the right one during even moves (second, fourth, etc.).

Let's number shells from 0 to 2 from left to right. Thus the left shell is assigned number 0, the middle shell is 1 and the right shell is 2. Bomboslav has missed the moment when the ball was placed beneath the shell, but he knows that exactly n movements were made by the operator and the ball was under shell x at the end. Now he wonders, what was the initial position of the ball?

Input

The first line of the input contains an integer n (1 ≤ n ≤ 2·109) — the number of movements made by the operator.

The second line contains a single integer x (0 ≤ x ≤ 2) — the index of the shell where the ball was found after n movements.

Output

Print one integer from 0 to 2 — the index of the shell where the ball was initially placed.

Examples
input
42
output
1
input
11
output
0
Note

In the first sample, the ball was initially placed beneath the middle shell and the operator completed four movements.

  1. During the first move operator swapped the left shell and the middle shell. The ball is now under the left shell.
  2. During the second move operator swapped the middle shell and the right one. The ball is still under the left shell.
  3. During the third move operator swapped the left shell and the middle shell again. The ball is again in the middle.
  4. Finally, the operators swapped the middle shell and the right shell. The ball is now beneath the right shell.                                              


题意:  给你三个位置0 1 2 奇数部时左面和中间交换   偶数部时右面和中间交换,告诉你经过n步后球的位置x求其原来的位置
思路:     找规律发现6个一周期  n%6 取余数 然后在倒着往前模拟即可
#include<bits/stdc++.h>using namespace std;int a[20];int main(){int n,x;scanf("%d%d",&n,&x);a[x]=1;n=n%6;for(int i=n;i>=1;i--){ if(i%2==1)swap(a[0],a[1]);elseswap(a[1],a[2]);}for(int i=0;i<3;i++){if(a[i]==1){printf("%d",i);break;}}return 0;}

B. Game of Credit Cards
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

After the fourth season Sherlock and Moriary have realized the whole foolishness of the battle between them and decided to continue their competitions in peaceful game of Credit Cards.

Rules of this game are simple: each player bring his favourite n-digit credit card. Then both players name the digits written on their cards one by one. If two digits are not equal, then the player, whose digit is smaller gets a flick (knock in the forehead usually made with a forefinger) from the other player. For example, if n = 3, Sherlock's card is 123 and Moriarty's card has number 321, first Sherlock names 1and Moriarty names 3 so Sherlock gets a flick. Then they both digit 2 so no one gets a flick. Finally, Sherlock names 3, while Moriarty names 1 and gets a flick.

Of course, Sherlock will play honestly naming digits one by one in the order they are given, while Moriary, as a true villain, plans to cheat. He is going to name his digits in some other order (however, he is not going to change the overall number of occurences of each digit). For example, in case above Moriarty could name 123 and get no flicks at all, or he can name 23 and 1 to give Sherlock two flicks.

Your goal is to find out the minimum possible number of flicks Moriarty will get (no one likes flicks) and the maximum possible number of flicks Sherlock can get from Moriarty. Note, that these two goals are different and the optimal result may be obtained by using different strategies.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 1000) — the number of digits in the cards Sherlock and Moriarty are going to use.

The second line contains n digits — Sherlock's credit card number.

The third line contains n digits — Moriarty's credit card number.

Output

First print the minimum possible number of flicks Moriarty will get. Then print the maximum possible number of flicks that Sherlock can get from Moriarty.

Examples
input
3123321
output
02
input
28800
output
20
Note

First sample is elaborated in the problem statement. In the second sample, there is no way Moriarty can avoid getting two flicks.

 题意:A和B玩游戏,他们分别在纸上写下n个数字(0-9)。然后对比,如果A[i]>B[i] ,A打B一下,如果A比B小则B打A一下,

现在B可以作弊,他知道了A的n个数。问你B最少被打几下,A最多被打几下。

思路: 这个题其实就是个贪心,我们对M的号码进行升序排序,因为M要被打的次数最少,那么他就要尽可能的让自己和S达成平局或者赢S即可

所以对于S每出一个数,M就要拿他拥有的大于等于S中最小的数去和S比较如果没有,就被打一次.

S最多被打几下 也就是M要赢得最多,那么对于S每出一个数M都要找他拥有的大于S最小的数去赢S,

这个题可把我坑苦了,醉了将近半个小时。。。这次的cf这么简单,,,其实是我想复杂了,一开始是按照田忌赛马的思路去考虑的这道题目,

可是最后感觉和田忌赛马有相似的地方但是不一样,这道题我们只需要考虑的是单方面的,就是如果要输的最少 怎么去输的最少,如果要赢得多

怎么去赢得多,可是对于田忌赛马要同时考虑输赢 平局三种情况 那考虑的就多了......


#include<bits/stdc++.h>using namespace std;const int maxn=1111;int book[maxn];string s;int a[maxn],b[maxn];int main(){int n;cin>>n;cin>>s;for(int i=0;i<s.length();i++)a[i]=s[i]-'0';cin>>s;for(int i=0;i<s.length();i++)b[i]=s[i]-'0';sort(b,b+n);memset(book,0,sizeof(book));int ans=n;for(int i=0;i<n;i++){for(int j=0;j<n;j++){if(!book[j]&&b[j]>=a[i])//因为排序了 所以找到的一定是大于等于s最小的数{book[j]=1;ans--;//这里求被打的次数最小 是不输一次就--break;}}}printf("%d\n",ans);ans=0;memset(book,0,sizeof(book));for(int i=0;i<n;i++){for(int j=0;j<n;j++){if(!book[j]&&b[j]>a[i])//和上面同理{book[j]=1;ans++;break;}}}printf("%d\n",ans);return 0;}


C. Alyona and Spreadsheet
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

During the lesson small girl Alyona works with one famous spreadsheet computer program and learns how to edit tables.

Now she has a table filled with integers. The table consists of n rows and m columns. By ai, j we will denote the integer located at the i-th row and the j-th column. We say that the table is sorted in non-decreasing order in the column j if ai, j ≤ ai + 1, j for all i from 1 to n - 1.

Teacher gave Alyona k tasks. For each of the tasks two integers l and r are given and Alyona has to answer the following question: if one keeps the rows from l to r inclusive and deletes all others, will the table be sorted in non-decreasing order in at least one column? Formally, does there exist such j thatai, j ≤ ai + 1, j for all i from l to r - 1 inclusive.

Alyona is too small to deal with this task and asks you to help!

Input

The first line of the input contains two positive integers n and m (1 ≤ n·m ≤ 100 000) — the number of rows and the number of columns in the table respectively. Note that your are given a constraint that bound the product of these two integers, i.e. the number of elements in the table.

Each of the following n lines contains m integers. The j-th integers in the i of these lines stands for ai, j(1 ≤ ai, j ≤ 109).

The next line of the input contains an integer k (1 ≤ k ≤ 100 000) — the number of task that teacher gave to Alyona.

The i-th of the next k lines contains two integers li and ri (1 ≤ li ≤ ri ≤ n).

Output

Print "Yes" to the i-th line of the output if the table consisting of rows from li to ri inclusive is sorted in non-decreasing order in at least one column. Otherwise, print "No".

Example
input
5 41 2 3 53 1 3 24 5 2 35 5 3 24 4 3 461 12 54 53 51 31 5
output
YesNoYesYesYesNo
Note

In the sample, the whole table is not sorted in any column. However, rows 1–3 are sorted in column 1, while rows 4–5 are sorted in column 3.




见:

http://blog.csdn.net/howardemily/article/details/57123310



D. Cloud of Hashtags
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Vasya is an administrator of a public page of organization "Mouse and keyboard" and his everyday duty is to publish news from the world of competitive programming. For each news he also creates a list of hashtags to make searching for a particular topic more comfortable. For the purpose of this problem we define hashtag as a string consisting of lowercase English letters and exactly one symbol '#' located at the beginning of the string. The length of the hashtag is defined as the number of symbols in it withoutthe symbol '#'.

The head administrator of the page told Vasya that hashtags should go in lexicographical order (take a look at the notes section for the definition).

Vasya is lazy so he doesn't want to actually change the order of hashtags in already published news. Instead, he decided to delete some suffixes (consecutive characters at the end of the string) of some of the hashtags. He is allowed to delete any number of characters, even the whole string except for the symbol '#'. Vasya wants to pick such a way to delete suffixes that the total number of deleted symbols isminimum possible. If there are several optimal solutions, he is fine with any of them.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 500 000) — the number of hashtags being edited now.

Each of the next n lines contains exactly one hashtag of positive length.

It is guaranteed that the total length of all hashtags (i.e. the total length of the string except for characters '#') won't exceed 500 000.

Output

Print the resulting hashtags in any of the optimal solutions.

Examples
input
3#book#bigtown#big
output
#b#big#big
input
3#book#cool#cold
output
#book#co#cold
input
4#car#cart#art#at
output
###art#at
input
3#apple#apple#fruit
output
#apple#apple#fruit
Note

Word a1, a2, ..., am of length m is lexicographically not greater than word b1, b2, ..., bk of length k, if one of two conditions hold:

  • at first position i, such that ai ≠ bi, the character ai goes earlier in the alphabet than character bi, i.e.a has smaller character than b in the first position where they differ;
  • if there is no such position i and m ≤ k, i.e. the first word is a prefix of the second or two words are equal.

The sequence of words is said to be sorted in lexicographical order if each word (except the last one) is lexicographically not greater than the next word.

For the words consisting of lowercase English letters the lexicographical order coincides with the alphabet word order in the dictionary.

According to the above definition, if a hashtag consisting of one character '#' it is lexicographically not greater than any other valid hashtag. That's why in the third sample we can't keep first two hashtags unchanged and shorten the other two.

 题意:有n的标签,现在要你不改变顺序的前提下使其变为字典序依次从小到大。

思路:水题啊。。。。由于B题傻逼了耽误太多时间 这个题再有两分钟就调完了。。。。orz、、

这道题也可以算是一个贪心吧,我们从最后一个字符串往前遍历然后比较大小删除字符;用string函数很简单

#include<bits/stdc++.h>using namespace std;const int maxn=1e7+10;string s[maxn];int main(){int n;scanf("%d",&n);for(int i=0;i<n;i++)cin>>s[i];int flag,item=-1;for(int i=n-2;i>=0;i--){flag=1;for(int j=1;j<max(s[i].length(),s[i+1].length());j++){flag=0;if(s[i+1][j]>s[i][j])break;if(s[i+1][j]<s[i][j]){if(s[i].length()>s[i+1].length())s[i].erase(s[i+1].length(),s[i].length()-1);if(s[i][j]>s[i+1][j]){s[i].erase(j,s[i].length()-1);break;}}}}for(int i=0;i<n;i++){cout<<s[i]<<endl;}return 0;}

E. Hanoi Factory
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Of course you have heard the famous task about Hanoi Towers, but did you know that there is a special factory producing the rings for this wonderful game? Once upon a time, the ruler of the ancient Egypt ordered the workers of Hanoi Factory to create as high tower as possible. They were not ready to serve such a strange order so they had to create this new tower using already produced rings.

There are n rings in factory's stock. The i-th ring has inner radius ai, outer radius bi and height hi. The goal is to select some subset of rings and arrange them such that the following conditions are satisfied:

  • Outer radiuses form a non-increasing sequence, i.e. one can put the j-th ring on the i-th ring only ifbj ≤ bi.
  • Rings should not fall one into the the other. That means one can place ring j on the ring i only ifbj > ai.
  • The total height of all rings used should be maximum possible.
Input

The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of rings in factory's stock.

The i-th of the next n lines contains three integers aibi and hi (1 ≤ ai, bi, hi ≤ 109bi > ai) — inner radius, outer radius and the height of the i-th ring respectively.

Output

Print one integer — the maximum height of the tower that can be obtained.

Examples
input
31 5 12 6 23 7 3
output
6
input
41 2 11 3 34 6 25 7 1
output
4
Note

In the first sample, the optimal solution is to take all the rings and put them on each other in order 321.

In the second sample, one can put the ring 3 on the ring 4 and get the tower of height 3, or put the ring 1on the ring 2 and get the tower of height 4.



见:

http://blog.csdn.net/howardemily/article/details/57101259

0 0
原创粉丝点击