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

来源:互联网 发布:js json数组长度 编辑:程序博客网 时间:2024/06/07 04:55

 A题简单水题 找下规律,当时读题比较久,没理解什么意思。

B题,

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 names1 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 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.


题意简单,才看到题没什么想法,后来又把题想复杂了,只是简单贪心,现在连水题都已经做不出来了,平时没用心,以后要努力

code:

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int main(){    int n;    char a1[1005],b1[1005];    int a[1005],b[1005];    while(~scanf("%d",&n))    {        scanf("%s",a1);        for(int i=0; i<n; i++)        {            a[i]=a1[i]-'0';        }        scanf("%s",b1);        for(int i=0; i<n; i++)        {            b[i]=b1[i]-'0';        }        sort(a,a+n);        sort(b,b+n);        int i=0,j=0,ans1=0,ans2=0;        while(i<n&&j<n)        {            if(b[j]>=a[i])            {                i++;                j++;            }            else            {                j++;                ans1++;            }        }        i=j=0;        while(i<n&&j<n)        {            if(a[i]>=b[j])                j++;            else            {                i++;                j++;                ans2++;            }        }        printf("%d\n%d\n",ans1,ans2);    }}

C题 

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 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.

题意:n*m的矩阵,k次查询,找出第l到r行是否有非递减的列,有输出YES,否则NO

预处理,把每行单增的最大行数存起来,把每列单增的最大行数也存起来(优化,不然超时)

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;const int N = 1e5+10;int c[N],r[N];int main(){    int n,m,t;    scanf("%d%d",&n,&m);    int map[n+5][m+5];    for(int i=1; i<=n; i++)    {        for(int j=1; j<=m; j++)        {            scanf("%d",&map[i][j]);        }    }    memset(c,0,sizeof(c));    memset(r,0,sizeof(r));    int k=1,maxx=1;    for(int i=1; i<=n; i++)    {        maxx=1;        for(int j=1; j<=m; j++)        {            k=i;            if(c[j]>=k) k=c[j];            while(map[k+1][j]>=map[k][j]&&k<n)            {                k++;            }            c[j]=k;            maxx=max(k,maxx);        }        r[i]=maxx;    }    scanf("%d",&t);    while(t--)    {        int x,y;        scanf("%d%d",&x,&y);        if(r[x]>=y)            printf("Yes\n");        else            printf("No\n");    }}



D题

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 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 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.

题意:可以删除字符,使n个字符串按字典序的顺序排列,输出删除最少字符后新的字典序排列

要从后往前比较与前一个字符串的大小,大了不变,小了删除前一个字符串后面的字符,   要注意string型字符串和char []字符串赋值的不同,用char []清空时会超时

code:

#include<stdio.h>#include<string.h>#include<iostream>const int N=5e5+5;using namespace std;string s[N];int main(){    int n;    cin>>n;    for(int i=0; i<n; i++)    {        cin>>s[i];    }    int flag=0,k;    string t;    for(int i=n-1; i>0; i--)    {        t="\0";        int len_1=s[i].size();        int len_2=s[i-1].size();        if(s[i]<s[i-1])        {            for(int j=0; j<min(len_1,len_2); j++)            {                if(s[i][j]<s[i-1][j])                {                    break;                }                else                {                    t+=s[i-1][j];//string型字符串赋值时不能是t[j]=s[i-1][j]否则字符串t的结果不对                }            }            s[i-1]=t;        }    }    for(int i=0; i<n; i++)    {        cout<<s[i]<<endl;    }}

0 0
原创粉丝点击