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.
Output
Print a single number — the minimum difficulty of the track after removing a single hold.
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
*/
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.
Output
Print a single line containing n digits — the desired state of the display containing the smallest possible number.
#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;
}
题目意思是说,尽量少的删除一些列,使得剩余的矩阵每一行从上到下都是非递减的。
#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;
}