Codeforces Round #283 (Div. 2) 题解

 
        A. Minimum Difficulty
 

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 sequence ai increase, that is, ai < ai + 1 for all ifrom 1 to n - 1; we will call such sequence a track. Mike thinks that the track a1, ..., an has difficulty Codeforces Round #283 (Div. 2) 题解. 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 integers ai (1 ≤ ai ≤ 1000), where ai is the height where the hold number i 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


意思:一个非递增的数组,定义这个数组的难度为Codeforces Round #283 (Div. 2) 题解
   现在要从这个数组中拿掉一个数(必须拿掉),问拿掉哪个数使得这个数组的难度最小。


思路:记录这个数组(数组本身不用记录)两两的距离,然后找出a[i]+a[i+1]的最小值,就是去掉中间这个数了,再用这个a[i]+a[i+1]和数组原来的难度相比,大的那个就是ans了。

Codeforces Round #283 (Div. 2) 题解
 1 #include<cstdio>

 2 const int maxn=104;

 3 int a[maxn];

 4 int main()

 5 {

 6     int n;

 7     while(scanf("%d",&n)!=EOF)

 8     {

 9         int pre,p;

10         int tot=1;

11         scanf("%d",&pre);

12         for(int i=2;i<=n;i++)

13         {

14             scanf("%d",&p);

15             a[tot++]=p-pre;

16             pre=p;

17         }

18         int ans=100000;

19         for(int i=1;i<tot-1;i++)

20         {

21             if(a[i]+a[i+1]<ans)

22                 ans=a[i]+a[i+1];

23         }

24         for(int i=1;i<tot;i++)

25         {

26             if(a[i]>ans)

27                 ans=a[i];

28         }

29         printf("%d\n",ans);

30     }

31     return 0;

32 }
15ms

 



         C. Removing Columns

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

题意:给出一个n*m的矩阵,上面放着小写字符。
要求:删除其中的一些列,使得矩阵从上到下,每一行组成的字符串的字典序由小到大(可以相等)

思路:
从第1列->第m列:考虑是否要去掉,去掉则把字符标记为‘A'。

Codeforces Round #283 (Div. 2) 题解
 1 #include<cstdio>

 2 const int maxn=110;

 3 char a[maxn][maxn];

 4 int main()

 5 {

 6     int n,m;

 7     while(scanf("%d%d",&n,&m)!=EOF)

 8     {

 9         char str[maxn];

10         for(int i=1;i<=n;i++)

11         {

12             scanf("%s",&str);

13             for(int j=1;j<=m;j++)

14             {

15                 a[i][j]=str[j-1];

16             }

17         }

18         if(n==1)

19         {

20             printf("0\n");

21             continue;

22         }

23         int ans=0;

24         for(int j=1;j<=m;j++)

25         {

26             for(int i=2;i<=n;i++)

27             {

28                 int u=a[i][j];

29                 int v=a[i-1][j];

30                 if(u<v)

31                 {

32                     int w=1;

33                     while(a[i][w]==a[i-1][w]||a[i][w]=='A')

34                         w++;

35                     if(w==j)

36                     {

37                         ans++;

38                         //刚开始这里写成t=n,当然无限循环了,然后输入数据后

39                         //一直没有输出结果,我甚至开始怀疑人生了

40                         //检查了一个小时,呵呵

41                         for(int t=1;t<=n;t++)

42                         {

43                             a[t][j]='A';

44                         }

45                         break;

46                     }

47                 }

48             }

49         }

50         printf("%d\n",ans);

51     }

52     return 0;

53 }
15ms
 
     

 

 
     
          B. Secret Combination
 
     

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 number 579, then if we push the first button, the display will show 680, and if after that we push the second button, the display will show 068.

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

题意:有一个串数字,现在你可以有2种操作:
1.把这串数字的每个数字加1 (随便你做多少次)
2.把这串数字的最后一个数字放到最前面,其余的后移一位(随便多少次都可以)
问:这串数字经过你的操作后,可能出现的最小数字是多少。

思路:刚开始看到这道题,我觉得是很难的,一想,其实很简单。
数字最小,则操作后,第一位一定是0
先操作1,则出现0的位置一共有n<=1000种可能,然后操作2,把0放到最前面。
当然,也可以先不断调整首位的数字,再把第一位的数字变为0.(我的代码也是这样)


Codeforces Round #283 (Div. 2) 题解
 1 #include<cstdio>

 2 const int maxn=1010;

 3 char a[maxn];       //输入的数字串

 4 int b[maxn];        //首位为0时的数字串

 5 int ans[maxn];     //存储当前的最小值

 6 int n;

 7 void update()

 8 {

 9     bool flag=false;

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

11     {

12         if(b[i]>ans[i])        //说明b>ans了,后面的不用再比较了,开始没有这一步,wa了

13             break;

14         if(b[i]<ans[i])

15         {

16             flag=true;

17             break;

18         }

19     }

20     if(flag)

21     {

22         for(int i=0;i<n;i++)

23             ans[i]=b[i];

24     }

25 }

26 int main()

27 {

28     while(scanf("%d",&n)!=EOF)

29     {

30         scanf("%s",&a);

31         for(int i=0;i<n;i++)     //初始化ans为99999999......

32             ans[i]=9;

33         for(int i=0;i<n;i++)     //n次

34         {

35                                     //m表示首位加多少个1变成0

36             int m=10-(a[0]-'0');   //-'0'和+'0'是不一样的

37             for(int j=0;j<n;j++)

38             {

39                 int u=a[j]-'0';

40                 b[j]=(u+m)%10;     //%10

41             }

42             update();              //更新ans

43             char p=a[n-1];      

44             for(int j=n-1;j>0;j--)   //执行操作2,调整首位的数字

45                 a[j]=a[j-1];         //注意,这里要从后往前,不能从前往后

46             a[0]=p;

47         }

48         for(int i=0;i<n;i++)

49             printf("%d",ans[i]);

50         printf("\n");

51     }

52     return 0;

53 }
15ms
 
     

 






你可能感兴趣的:(codeforces)