<cf>Walking in the Rain
2012-07-17 23:04
281 查看
B. Walking in the Rain
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
In Berland the opposition is going to arrange mass walking on the boulevard. The boulevard consists ofn tiles that are lain in a row and
are numbered from 1 to n from
right to left. The opposition should start walking on the tile number 1 and the finish on the tile number n.
During the walk it is allowed to move from right to left between adjacent tiles in a row, and jump over a tile. More formally, if you are standing on the tile number i (i < n - 1),
you can reach the tiles number i + 1 or the tile number i + 2 from
it (if you stand on the tile number n - 1, you can only reach tile number n).
We can assume that all the opposition movements occur instantaneously.
In order to thwart an opposition rally, the Berland bloody regime organized the rain. The tiles on the boulevard are of poor quality and they are rapidly destroyed in the rain. We know that the i-th
tile is destroyed after ai days
of rain (on day ai tile
isn't destroyed yet, and on day ai + 1 it
is already destroyed). Of course, no one is allowed to walk on the destroyed tiles! So the walk of the opposition is considered thwarted, if either the tile number 1 is
broken, or the tile number n is broken, or it is impossible to reach the tile number n from
the tile number 1 if we can walk on undestroyed tiles.
The opposition wants to gather more supporters for their walk. Therefore, the more time they have to pack, the better. Help the opposition to calculate how much time they still have and tell us for how many days the walk from the tile number 1 to
the tile number n will be possible.
Input
The first line contains integer n (1 ≤ n ≤ 103)
— the boulevard's length in tiles.
The second line contains n space-separated integers ai —
the number of days after which the i-th tile gets destroyed (1 ≤ ai ≤ 103).
Output
Print a single number — the sought number of days.
Sample test(s)
input
output
input
output
Note
In the first sample the second tile gets destroyed after day three, and the only path left is 1 → 3 → 4. After day five there is a two-tile gap between
the first and the last tile, you can't jump over it.
In the second sample path 1 → 3 → 5 is available up to day five, inclusive. On day six the last tile is destroyed and the walk is thwarted.
AC Code:
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
In Berland the opposition is going to arrange mass walking on the boulevard. The boulevard consists ofn tiles that are lain in a row and
are numbered from 1 to n from
right to left. The opposition should start walking on the tile number 1 and the finish on the tile number n.
During the walk it is allowed to move from right to left between adjacent tiles in a row, and jump over a tile. More formally, if you are standing on the tile number i (i < n - 1),
you can reach the tiles number i + 1 or the tile number i + 2 from
it (if you stand on the tile number n - 1, you can only reach tile number n).
We can assume that all the opposition movements occur instantaneously.
In order to thwart an opposition rally, the Berland bloody regime organized the rain. The tiles on the boulevard are of poor quality and they are rapidly destroyed in the rain. We know that the i-th
tile is destroyed after ai days
of rain (on day ai tile
isn't destroyed yet, and on day ai + 1 it
is already destroyed). Of course, no one is allowed to walk on the destroyed tiles! So the walk of the opposition is considered thwarted, if either the tile number 1 is
broken, or the tile number n is broken, or it is impossible to reach the tile number n from
the tile number 1 if we can walk on undestroyed tiles.
The opposition wants to gather more supporters for their walk. Therefore, the more time they have to pack, the better. Help the opposition to calculate how much time they still have and tell us for how many days the walk from the tile number 1 to
the tile number n will be possible.
Input
The first line contains integer n (1 ≤ n ≤ 103)
— the boulevard's length in tiles.
The second line contains n space-separated integers ai —
the number of days after which the i-th tile gets destroyed (1 ≤ ai ≤ 103).
Output
Print a single number — the sought number of days.
Sample test(s)
input
4 10 3 5 10
output
5
input
510 2 8 3 5
output
5
Note
In the first sample the second tile gets destroyed after day three, and the only path left is 1 → 3 → 4. After day five there is a two-tile gap between
the first and the last tile, you can't jump over it.
In the second sample path 1 → 3 → 5 is available up to day five, inclusive. On day six the last tile is destroyed and the walk is thwarted.
AC Code:
#include <iostream> #include <algorithm> using namespace std; struct Days_Destroyed { int i;//tile的序号 int day;//要使 i-th tile 被毁的日数 }a[1001]; bool dest[1001]; //记录是否被毁,默认为假,即木有被毁 int cmp(const void *a,const void *b) { struct Days_Destroyed* aa=(Days_Destroyed*)a; struct Days_Destroyed* bb=(Days_Destroyed*)b; return aa->day-bb->day; } int main() { int n,MaxDays,j; while(cin>>n) { MaxDays=1; for(j=1;j<=n;j++) { cin>>a[j].day; a[j].i=j; dest[j]=false; } qsort(a+1,n,sizeof(a[0]),cmp);//快速排序 for(j=1;j<=n;j++) { //如果是第一或最后一个tile被毁,又或相邻两个tile之一被毁,当前tile就不能再被毁 if(a[j].i==1 || a[j].i==n || dest[a[j].i-1] || dest[a[j].i+1]) { MaxDays=a[j].day;//注意不能直接break,要先执行这一步 break; } else { MaxDays=a[j].day; dest[a[j].i]=true;//标记为已被破坏 } } cout<<MaxDays<<endl; } return 0; }
相关文章推荐
- <cf>Walking in the Rain
- <转> 解决异常:IllegalStateException: Fragment <ThisFragment> is not currently in the FragmentManager
- ACdream 1079 Walking in the Rain (线性dp)
- <转>python version 2.7 required,which was not found in the registry
- <Chapter 2>Fast Index Creation in the InnoDB Storage Engine
- The method setClass(Context, Class<?>) in the type Intent is not applicable for the问题
- Unable to update the EntitySet 'T_JsAPI' because it has a DefiningQuery and no <InsertFunction> element exists in the <ModificationFunctionMapping> element to support the current operation.
- lyrics of <The message> in Grandmaster Flash and the Furious Five song
- MySQL错误:Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column To disable safe mode, toggle the option in Preferences -> SQL easonjim
- <The C++ Programming Language> 读书笔记
- (POJ1426)Find The Multiple <BFS 大数模>
- Data Types in the Kernel <LDD3 学习笔记>
- <Programming_in_Lua> 笔记(未完)
- 解决“could not find gem 'turn <>=0 ' in any of the gem sources listed in your gemfile”错误
- <Programming Ruby 1.9 The Pragmatic Programmer>读书笔记
- The method setClass(Context, Class<?>) in the type Intent is not applicable for the arguments (GameV
- it has a DefiningQuery and no <InsertFunction> element exists in the <ModificationFunctionMapping> element
- <Effective Mordern C++>Item 6:UseTheExplicitlyTypedInitializerIdiomWhenAutoDeducesUndesiredTypes
- [英语歌曲]Sunshine In The Rain
- <五>基于Fourinone实现分布式锁指南和demo