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

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


題意簡單,纔看到題沒什麼想法,後來又把題想複雜了,只是簡單貪心,現在連水題都已經做不出來了,平時沒用心,以後要努力

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 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次查詢,找出第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;
    }
}

發佈了70 篇原創文章 · 獲贊 25 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章