codeforces 401

补的题

A:

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.

meaning:有3个杯子,其中一个杯子藏着东西,这3个杯子变化N次,变化次为单数的时候,杯子1和2交换,变换次数为偶次的时候,杯子2和3交换,给出变化N次和最终东西所在地方,问刚开始东西放在哪个地方。

thinking:水题,显而易见变化6次后的杯子跟原来的摆放是相同的,那么n=n%6,然后再变化即可

the reason of faliure:1、超时,直接O(n)会超时,应该找到一个循环。

代码:

#include<iostream>
#include <string.h>
using namespace std;
int gg[3];
int main(){
    int n,m,t1,t2,t3;
    cin >> n;
    cin >> t3;
    memset(gg,0,sizeof(gg));
    gg[t3]=1;
    int i;
    n=n%6;
    for(i=n;i>0;i--){
        if(i%2==0){
            swap(gg[2],gg[1]);
        }else{
            swap(gg[0],gg[1]);
        }
    }
    if(gg[0]){
        cout << 0 << endl;
    }else if(gg[1]){
        cout << 1 <<endl;
    }else if(gg[2]){
    cout << 2 <<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 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.

meaning:有2个人在玩游戏,每个人刚开始有N张个位数字的牌,然后亮牌,当A的数字大于B的时候,B获得一个flick,当B大于A时,A获得一个flick,数字相当时则2人都不获得flick,A的出牌顺序是固定的,问B如何出牌使得它获得的flick最小和A获得的flick最大,这是2个独立的问题。

thinking:水题,直接贪心。对于B获得flick最小:找最小那张大于等于A的的牌即可,然后输出n-满足的数量,然后标记这张牌用过。

对于A获得flick最大:找最小的大于A的牌即可,然后输出满足的数量,然后标记这张牌用过。

the reason of failure:一遍过。

代码:

#include <iostream>
#include <string.h>
using namespace std;
int q1[1050];
int q2[1050];
bool walked1[1050];
bool walked2[1050];
int main()
{
  //  freopen("in.txt","r",stdin);
    memset(q1,0,sizeof(q1));
    memset(q2,0,sizeof(q2));
    memset(walked1,0,sizeof(walked1));
    memset(walked2,0,sizeof(walked2));
    int n,m,i,j,k,l,f1,f2,t1,t2;
    int min2,min1;
    cin >> n;
    f1=0;
    f2=0;
    char c1,c2;
    for(i=1;i<=n;i++){
     //   scanf("%c",&c1);
        cin >>c1;
        q1[i]=c1-48;
    //cout << "c1=" <<c1 <<endl;
    //cout << i <<"===" << q1[i] << endl;
    }
    for(i=1;i<=n;i++){
      //  scanf("%c",&c2);
        cin >>c2;
        q2[i]=c2-48;
    //cout << i <<"===" << q2[i] << endl;
    }
    for(i=1;i<=n;i++){
        min1=99;
        min2=99;
        t1=0;t2=0;
        for(j=1;j<=n;j++){
            if(walked1[j]==0&&q2[j]>q1[i]&&min1>q2[j]){ //大于是获得
                    min1=q2[j];
            t1=j;
        }
            if(walked2[j]==0&&q2[j]>=q1[i]&&min2>=q2[j]){
                    min2=q2[j];
            t2=j;
            }
    }
   // cout << "q1 " << q1[i] << " q2 " << q2[i] << endl;
    //cout << "i=" << i << " "<< t1 << " "<< t2 <<endl;
    if(t1!=0){
        walked1[t1]=1;
        f1++;
    }
    if(t2!=0){
        walked2[t2]=1;
        f2++;
    }
    }
    cout << n-f2 <<endl;
    cout << f1 <<endl;
    return 0;
}

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.

meaning:给一个由数字组成的矩阵,问从第i到第j列,是否存在非递减序列,任意一列都行。

thinking:用一个f[x]=y,表示x能到y的最长范围,然后预处理全部,这样对于输入的值就可以直接O(1)判断。

the reason of failuire:1、9到9相同的数字也是输出YES。

2、超时,应该预处理全部,而不是如5 9  从5开始找到1是否有f[x]大于等于9

代码:

#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
//bool sort1[100500];
//int map1[100000][100000];
int f[100500];
int st1[100500];
int f2[100500];
int main(){
  // freopen("in.txt","r",stdin);
    int n,m,r,c,t1,t2,t3,f1,f3,i,j;
    cin >> r>> c;
    //memset(map1,0,sizeof(map1));
    memset(f,0,sizeof(f));
    memset(st1,0,sizeof(st1));  //st1为值
    for(i=1;i<=c;i++)
        f2[i]=1; // f2为从哪里开始
    for(i=1;i<=r;i++){
    for(j=1;j<=c;j++){
        scanf("%d",&f1);
        if(f1>=st1[j]){
            f[f2[j]]=max(f[f2[j]],i);
            st1[j]=f1;
        }else{
        st1[j]=f1;
        f2[j]=i;
        }
    }
}
    int max1=0;
    for(i=1;i<=r;i++){ //2、最右边的给赋值,表明最右边能到哪里
        max1=max(max1,f[i]);
        f[i]=max1;
    }
    //for(i=1;i<=r;i++)
     //   cout << i << "----" << f[i] <<endl;
  cin >>m;
  int g1,g2;
  for(i=1;i<=m;i++){
    scanf("%d %d",&t1,&t2);
    g1=0;
    if(t1==t2){  //1、考虑相等的情况,特殊值另外出来,之前想到,写的时候又忘记了
        printf("Yes\n");
       continue;
    }
    if(f[t1]>=t2)
        printf("Yes\n");
    else
        printf("No\n");
  }
    return 0;
}

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

meaning:就是将这些字符串按哈希排序,从小到大,如果下一行比上一行的小,就从最后一行开始删除,直到这行小于下一行

thinking:数组也是可以比较大小的,比较方式是从第一个数开始毕竟,如果第一个数相等则比较下一个,对于abc和abca是后则毕竟大。

string qq 是未分配内存,不能使用memset,而应该使用qq.clear();qq.size()用于得到string类型的长度。qq.resize(a),如果变小,那么多余那些就全部删除掉,如果是变大,可以resize(10,'b'),用b补上,对string尾部加入数可以qq.push_back(),或者qq.insert(从哪开始,字符串,长度)

the reason of failure:1、超时,for删除太慢,后面用了resize

代码

#include <iostream>
#include <string.h>
#include <vector>
using namespace std; //可以用不定长数组来解决。
int ff[500500];
string qq[500500];
char q1[500500];
int main(){
    //freopen("in.txt","r",stdin);
    int l1;
    int n,g1;
    int i,j,k;
    cin >> n;
    memset(ff,0,sizeof(ff));
    for(i=1;i<=n;i++){
    cin >> qq[i];}
    for(i=n-1;i>0;i--){
        if(qq[i]>qq[i+1]){
                g1=0;
            while(qq[i][g1]<=qq[i+1][g1]){
             g1++;
            }
            qq[i].resize(g1);
            ff[i]=g1;
        }
    }
    for(i=1;i<=n;i++){
        cout << qq[i] <<endl;
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值