Codeforces Round #283 (Div. 2) A. Minimum Difficulty B. Secret Combination C. Removing Columns

A. Minimum Difficulty
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Mike is trying rock climbing but he is awful at it.

There are n holds on the wall, i-th hold is at height ai off the ground. Besides, let the sequenceai increase, that is,ai < ai + 1 for alli from 1 to n - 1; we will call such sequence atrack. Mike thinks that the track a1, ..., an hasdifficulty . In other words, difficulty equals the maximum distance between two holds that are adjacent in height.

Today Mike decided to cover the track with holds hanging on heights a1, ..., an. To make the problem harder, Mike decided to remove one hold, that is, remove one element of the sequence (for example, if we take the sequence (1, 2, 3, 4, 5) and remove the third element from it, we obtain the sequence(1, 2, 4, 5)). However, as Mike is awful at climbing, he wants the final difficulty (i.e. the maximum difference of heights between adjacent holds after removing the hold) to be as small as possible among all possible options of removing a hold. The first and last holds must stay at their positions.

Help Mike determine the minimum difficulty of the track after removing one hold.

Input

The first line contains a single integer n (3 ≤ n ≤ 100) — the number of holds.

The next line contains n space-separated integersai (1 ≤ ai ≤ 1000), whereai is the height where the hold numberi hangs. The sequence ai is increasing (i.e. each element except for the first one is strictly larger than the previous one).

Output

Print a single number — the minimum difficulty of the track after removing a single hold.

Sample test(s)
Input
3
1 4 6
Output
5
Input
5
1 2 3 4 5
Output
2
Input
5
1 2 3 7 8
Output
4
Note

In the first sample you can remove only the second hold, then the sequence looks like(1, 6), the maximum difference of the neighboring elements equals 5.

In the second test after removing every hold the difficulty equals 2.

In the third test you can obtain sequences (1, 3, 7, 8),(1, 2, 7, 8), (1, 2, 3, 8), for which the difficulty is 4, 5 and 5, respectively. Thus, after removing the second element we obtain the optimal answer — 4.


#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#define N 1000
using namespace std;

int a[N];

int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i=0;i<n;i++)
        scanf("%d",&a[i]);

        int t;
        int mm;
        int ans=1000000000;

        for(int i=1;i<n-1;i++)//枚举每个可以被拿走的
        {
            mm=0;
            for(int j=0;j<n;j++)
            {
                if(j==i)
                {
                    t=a[j+1]-a[j-1];
                }
                else if(j+1==i)
                t=a[j+2]-a[j];

                else
                t=a[j+1]-a[j];

                mm=max(mm,t);

            }
            ans=min(ans,mm);
        }

        cout<<ans<<endl;
    }
    return 0;
}

/*

10
300 315 325 338 350 365 379 391 404 416

*/


B. Secret Combination
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You got a box with a combination lock. The lock has a display showing n digits. There are two buttons on the box, each button changes digits on the display. You have quickly discovered that the first button adds 1 to all the digits (all digits 9 become digits 0), and the second button shifts all the digits on the display one position to the right (the last digit becomes the first one). For example, if the display is currently showing number579, then if we push the first button, the display will show680, and if after that we push the second button, the display will show068.

You know that the lock will open if the display is showing the smallest possible number that can be obtained by pushing the buttons in some order. The leading zeros are ignored while comparing numbers. Now your task is to find the desired number.

Input

The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of digits on the display.

The second line contains n digits — the initial state of the display.

Output

Print a single line containing n digits — the desired state of the display containing the smallest possible number.

Sample test(s)
Input
3
579
Output
024
Input
4
2014
Output
0142

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <functional>
#include <bitset>
#include <cmath>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <deque>
#include <list>
#include <map>
#include <set>
#include <cctype>
#include <cstdlib>
#include <cassert>
#include <ctime>
#include <utility>
#include <limits>
#include <numeric>
#include <iomanip>
#include <sstream>
#include <fstream>

using namespace std;

string s,st,ss,ans;
int n;

void fun1()
{
    for(int i=0;i<n;i++)
    {
        if(s[i]=='9')
        s[i]='0';

        else s[i]+=1;
    }
}

void fun2()
{
    ss=st;
    ss[0]=st[n-1];
    for(int i=1;i<n;i++)
    ss[i]=st[i-1];
}

int main()
{
    while(~scanf("%d",&n))
    {
        cin>>s;
        ans=s;

        for(int i=0;i<10;i++)
        {

            fun1();
            st=s;

            for(int j=0;j<n+1;j++)
            {
                fun2();
                if(ans>ss)
                ans=ss;

                st=ss;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

C. Removing Columns
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given an n × m rectangular table consisting of lower case English letters. In one operation you can completely remove one column from the table. The remaining parts are combined forming a new table. For example, after removing the second column from the table

abcd
edfg
hijk

 

we obtain the table:

acd
efg
hjk

 

A table is called good if its rows are ordered from top to bottom lexicographically, i.e. each row is lexicographically no larger than the following one. Determine the minimum number of operations of removing a column needed to make a given table good.

Input

The first line contains two integers  — n and m (1 ≤ n, m ≤ 100).

Next n lines contain m small English letters each — the characters of the table.

Output

Print a single number — the minimum number of columns that you need to remove in order to make the table good.

Sample test(s)
Input
1 10
codeforces
Output
0
Input
4 4
case
care
test
code
Output
2
Input
5 4
code
forc
esco
defo
rces
Output
4
Note

In the first sample the table is already good.

In the second sample you may remove the first and third column.

In the third sample you have to remove all the columns (note that the table where all rows are empty is considered good by definition).

Let strings s and t have equal length. Then, s is lexicographically larger than t if they are not equal and the character following the largest common prefix of s and t (the prefix may be empty) in s is alphabetically larger than the corresponding character of t.

题目意思是说,尽量少的删除一些列,使得剩余的矩阵每一行从上到下都是非递减的。

#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#define N 1000
using namespace std;

int n,m;
char a[N][N];

int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=0;i<n;i++)
        scanf("%s",a[i]);

        if(n==1)
        {
            cout<<0<<endl;
            continue;
        }

        int f;

        string s[200000];

        for(int j=0;j<m;j++)
        {
             f=0;
            for(int i=1;i<n;i++)
            {
                if(s[i]+a[i][j]<s[i-1]+a[i-1][j])
                {
                    f=1;
                    break;
                }
            }
            if(f==0)
           {
               for(int i=0;i<n;i++)
               s[i]+=a[i][j];
           }
           f=0;
        }

        cout<<(m-s[0].length())<<endl;
    }
    return 0;
}









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值