Codeforces 383B. Volcanoes 模拟
2014-11-27 11:34
501 查看
记录每一层可以移动到的区间....非常多的细节...
B. Volcanoes
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Iahub got lost in a very big desert. The desert can be represented as a n × n square matrix, where each cell is a zone of the desert. The cell (i, j) represents
the cell at row i and column j (1 ≤ i, j ≤ n).
Iahub can go from one cell (i, j) only down or right, that is to cells (i + 1, j)or (i, j + 1).
Also, there are m cells that are occupied by volcanoes, which Iahub cannot enter.
Iahub is initially at cell (1, 1) and he needs to travel to cell (n, n).
Knowing that Iahub needs 1 second to travel from one cell to another, find the minimum time in which he can arrive in cell (n, n).
Input
The first line contains two integers n (1 ≤ n ≤ 109) and m (1 ≤ m ≤ 105).
Each of the next m lines contains a pair of integers, x and y (1 ≤ x, y ≤ n),
representing the coordinates of the volcanoes.
Consider matrix rows are numbered from 1 to n from top to bottom, and matrix columns are numbered from 1 to n from
left to right. There is no volcano in cell (1, 1). No two volcanoes occupy the same location.
Output
Print one integer, the minimum time in which Iahub can arrive at cell (n, n). If no solution exists (there is no path to the final cell), print
-1.
Sample test(s)
input
output
input
output
input
output
Note
Consider the first sample. A possible road is: (1, 1) → (1, 2) → (2, 2) → (2, 3) → (3, 3) → (3, 4) → (4, 4).
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
int n,m;
typedef pair<int,int> pII;
vector<pII> vi;
vector<pII> duan[2],temp;
int hy[320000],ny=0;
int MX=-1;
int main()
{
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
vi.push_back(make_pair(a,b));
if(a-1>=1) hy[ny++]=a-1;
hy[ny++]=a;
if(a+1<=n) hy[ny++]=a+1;
}
hy[ny++]=n; hy[ny++]=1;
sort(hy,hy+ny);
ny=unique(hy,hy+ny)-hy;
sort(vi.begin(),vi.end());
int p=0;
int pre=0,now=1;
int left=-1;
if(vi[p].first==1)
{
for(;p<vi.size();p++)
{
if(vi[p].first==1)
{
if(left==-1) left=vi[p].second-1;
else left=min(left,vi[p].second-1);
}
else
{
p--; break;
}
}
if(left<1)
{
puts("-1");
return 0;
}
else duan[0].push_back(make_pair(1,left));
}
else duan[0].push_back(make_pair(1,n));
p=0;
for(int yy=0;yy<ny;yy++)
{
temp.clear();
int one=1,two;
while(vi[p].first==hy[yy])
{
two=vi[p].second-1;
if(two>=one)
{
temp.push_back(make_pair(one,two));
}
one=two+2;
p++;
}
if(one<=n) temp.push_back(make_pair(one,n));
duan[now].clear();
int id=0;
for(int i=0,sz=duan[pre].size(),ts=temp.size();i<sz&&id<ts;i++)
{
int from=duan[pre][i].first;
int to=duan[pre][i].second;
if(to<temp[id].first) continue;
while(id+1<ts&&temp[id].second<from)
id++;
if(to>=temp[id].first&&from<=temp[id].second)
{
duan[now].push_back( make_pair( max(from,temp[id].first)
, temp[id].second) );
if(hy[yy]==n)
{
MX=max(MX,temp[id].second);
}
id++; i--;
}
}
swap(now,pre);
}
if(MX>=n) printf("%d\n",2*n-2);
else printf("-1\n");
return 0;
}
B. Volcanoes
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Iahub got lost in a very big desert. The desert can be represented as a n × n square matrix, where each cell is a zone of the desert. The cell (i, j) represents
the cell at row i and column j (1 ≤ i, j ≤ n).
Iahub can go from one cell (i, j) only down or right, that is to cells (i + 1, j)or (i, j + 1).
Also, there are m cells that are occupied by volcanoes, which Iahub cannot enter.
Iahub is initially at cell (1, 1) and he needs to travel to cell (n, n).
Knowing that Iahub needs 1 second to travel from one cell to another, find the minimum time in which he can arrive in cell (n, n).
Input
The first line contains two integers n (1 ≤ n ≤ 109) and m (1 ≤ m ≤ 105).
Each of the next m lines contains a pair of integers, x and y (1 ≤ x, y ≤ n),
representing the coordinates of the volcanoes.
Consider matrix rows are numbered from 1 to n from top to bottom, and matrix columns are numbered from 1 to n from
left to right. There is no volcano in cell (1, 1). No two volcanoes occupy the same location.
Output
Print one integer, the minimum time in which Iahub can arrive at cell (n, n). If no solution exists (there is no path to the final cell), print
-1.
Sample test(s)
input
4 2 1 3 1 4
output
6
input
7 8
1 62 63 5
3 64 3
5 1
5 2
5 3
output
12
input
2 2 1 2 2 1
output
-1
Note
Consider the first sample. A possible road is: (1, 1) → (1, 2) → (2, 2) → (2, 3) → (3, 3) → (3, 4) → (4, 4).
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
int n,m;
typedef pair<int,int> pII;
vector<pII> vi;
vector<pII> duan[2],temp;
int hy[320000],ny=0;
int MX=-1;
int main()
{
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
vi.push_back(make_pair(a,b));
if(a-1>=1) hy[ny++]=a-1;
hy[ny++]=a;
if(a+1<=n) hy[ny++]=a+1;
}
hy[ny++]=n; hy[ny++]=1;
sort(hy,hy+ny);
ny=unique(hy,hy+ny)-hy;
sort(vi.begin(),vi.end());
int p=0;
int pre=0,now=1;
int left=-1;
if(vi[p].first==1)
{
for(;p<vi.size();p++)
{
if(vi[p].first==1)
{
if(left==-1) left=vi[p].second-1;
else left=min(left,vi[p].second-1);
}
else
{
p--; break;
}
}
if(left<1)
{
puts("-1");
return 0;
}
else duan[0].push_back(make_pair(1,left));
}
else duan[0].push_back(make_pair(1,n));
p=0;
for(int yy=0;yy<ny;yy++)
{
temp.clear();
int one=1,two;
while(vi[p].first==hy[yy])
{
two=vi[p].second-1;
if(two>=one)
{
temp.push_back(make_pair(one,two));
}
one=two+2;
p++;
}
if(one<=n) temp.push_back(make_pair(one,n));
duan[now].clear();
int id=0;
for(int i=0,sz=duan[pre].size(),ts=temp.size();i<sz&&id<ts;i++)
{
int from=duan[pre][i].first;
int to=duan[pre][i].second;
if(to<temp[id].first) continue;
while(id+1<ts&&temp[id].second<from)
id++;
if(to>=temp[id].first&&from<=temp[id].second)
{
duan[now].push_back( make_pair( max(from,temp[id].first)
, temp[id].second) );
if(hy[yy]==n)
{
MX=max(MX,temp[id].second);
}
id++; i--;
}
}
swap(now,pre);
}
if(MX>=n) printf("%d\n",2*n-2);
else printf("-1\n");
return 0;
}
相关文章推荐
- Codeforces 711B 【模拟】
- CodeForces 412E - E-mail Addresses (模拟)
- Codeforces--658C--Bear and Forgotten Tree 3(模拟&&技巧)(好题)
- Codeforces 791A Bear and Big Brother(暴力枚举,模拟)
- Codeforces 527C Glass Carving【思维+模拟】
- CodeForces 357C - Knight Tournament(模拟)
- CodeForces 557A-Ilya and Diplomas【模拟】
- CodeForces - 758C Unfair Poll (模拟+暴力+思维)
- codeforces 954C. Matrix Walk(思维模拟)
- Codeforces 246B-Increase and Decrease【模拟】
- Codeforces 716 B Complete the Word【模拟】
- 树状数组模拟3个元素的排序 Codeforces 12D Ball
- codeforces 792 B. Counting-out Rhyme (模拟链表||约瑟夫环)
- CodeForces 448B 小模拟
- CodeForces - 754B Ilya and tic-tac-toe game(模拟)
- codeforces 676B B. Pyramid of Glasses(模拟)
- Codeforces--629A--Far Relative’s Birthday Cake(暴力模拟)
- CodeForces - 668B Little Artem and Dance(模拟,思路)
- Codeforces - 834B. The Festive Evening - 思维、模拟
- CodeForces - 868B Race Against Time 简单模拟