Codeforces Round #401 (Div. 2) A,B,C,D,E

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
4
2
output
1
input
1
1
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次,交换中间一张和左边的一张,中间一张和右边一张,中间一张和左边一张,......最后问选的那张在哪个位置。

代码:

//每交换6次一个循环.......
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,x;
    int a[7][3]={0,1,2,1,0,2,1,2,0,2,1,0,2,0,1,0,2,1,0,1,2};
    cin>>n>>x;
    n=n%6;
    cout<<a[n][x]<<endl;
    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 1 and 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 1, 2, 3 and get no flicks at all, or he can name 2, 3 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
3
123
321
output
0
2
input
2
88
00
output
2
0
Note

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

 题意:

田忌赛马类题。求一个人最多赢多少次和另一个人最少赢多少次。

代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,a[1003],b[1003];
    char s1[1003],s2[1003];
    cin>>n>>s1>>s2;
    for(int i=0;i<n;i++) a[i]=s1[i]-'a';
    for(int i=0;i<n;i++) b[i]=s2[i]-'a';
    int as,ae,bs,be,ans1=0,ans2=0;
    sort(a,a+n);sort(b,b+n);
    as=0;bs=0;ae=n-1;be=n-1;
    while(bs<=be){
        if(b[bs]>=a[as]) {bs++;as++;}
        else if(b[bs]<a[as]) {bs++;ae--;ans1++;}
    }
    as=0;bs=0;ae=n-1;be=n-1;
    while(as<=ae){
        if(a[ae]<b[be]) {ae--;be--;ans2++;}
        else if(a[ae]>b[be]) {ae--;bs++;}
        else{
            if(a[as]<b[bs]) {as++;bs++;ans2++;}
            else {ae--;bs++;}
        }
    }
    cout<<ans1<<endl<<ans2<<endl;
    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 that ai, 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 4
1 2 3 5
3 1 3 2
4 5 2 3
5 5 3 2
4 4 3 4
6
1 1
2 5
4 5
3 5
1 3
1 5
output
Yes
No
Yes
Yes
Yes
No
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.

 题意:

n*m的矩阵,有k次询问,每次询问a b表示从a行到b行中有没有某一列中的数是非递减的。

代码:

//简单dp,可以递推地求出每一行向上找满足非递增次序的最多有几行记为f[i],然后每输入一个
//区间a,b判断这个区间长度是否小于等于f[b]。
#include<bits/stdc++.h>
using namespace std;
int n,m,k,x,y,mp[200005],f[200005],a[100005];
int main()
{
    scanf("%d%d",&n,&m);
    memset(f,0,sizeof(f));
    memset(a,0,sizeof(a));
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            int id=(i-1)*m+j;
            scanf("%d",&mp[id]);
            if(i>1&&mp[id]>=mp[id-m]) f[id]=f[id-m]+1;
            else f[id]=1;
            a[i]=max(a[i],f[id]);
        }
    }
    scanf("%d",&k);
    while(k--){
        scanf("%d%d",&x,&y);
        if(y-x+1>a[y]) printf("No\n");
        else printf("Yes\n");
    }
    return 0;
}
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 without the 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 is minimum 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 bin 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.

 题意:

水。

代码:

#include<bits/stdc++.h>
using namespace std;
int n;
string s[500000];
int main()
{
    cin>>n;
    for(int i=0;i<n;i++) cin>>s[i];
    for(int i=n-2;i>=0;i--){
        string ch="#";
        int len1=s[i+1].size(),len2=s[i].size(),k=0;
        for(int j=1;j<len1&&j<len2;j++){
            if(s[i+1][j]>s[i][j]) {ch=s[i];break;}
            else if(s[i+1][j]<s[i][j]) break;
            ch+=s[i][j];
        }
        s[i]=ch;
    }
    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 if bj ≤ bi.
  • Rings should not fall one into the the other. That means one can place ring j on the ring i only if bj > 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 ≤ 109, bi > 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
3
1 5 1
2 6 2
3 7 3
output
6
input
4
1 2 1
1 3 3
4 6 2
5 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 3, 2, 1.

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 1 on the ring 2 and get the tower of height 4.

 题意:

n个环,环有内经外径和高,问最高能摞多高。

代码:

//贪心。按照外径从大到小排序,使每个环都按照要求进栈一次并且更新最大值。这个方法挺好的。
#include<bits/stdc++.h>
using namespace std;
int n;
struct ring
{
    int R,r,h;
}ri[100005];
bool cmp(ring x,ring y)
{
    if(x.R==y.R) return x.r>y.r;
    return x.R>y.R;
}
int main()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++)
        scanf("%d%d%d",&ri[i].r,&ri[i].R,&ri[i].h);
    sort(ri,ri+n,cmp);
    long long maxn,sum;
    sum=maxn=ri[0].h;
    stack<int>s;
    s.push(0);
    for(int i=1;i<n;i++){
        while(!s.empty()&&ri[i].R<=ri[s.top()].r){
            sum-=ri[s.top()].h;
            s.pop();
        }
        sum+=ri[i].h;
        maxn=max(maxn,sum);
        s.push(i);
    }
    printf("%I64d\n",maxn);
    return 0;
}

 

posted @ 2017-03-12 22:25  luckilzy  阅读(943)  评论(0编辑  收藏  举报