fzu 2163

扫码查看
Problem 2163 多米诺骨牌

Accept: 17    Submit: 50
Time Limit: 1000 mSec    Memory Limit : 32768 KB

fzu 2163-LMLPHP Problem Description

Vasya很喜欢排多米诺骨牌。他已经厌倦了普通的多米诺骨牌,所以他用不同高度的多米诺骨牌。他从左边到右边,把n个多米诺骨牌沿一个轴放在桌子上。每一个多米诺骨牌垂直于该轴,使该轴穿过其底部的中心。第i个多米诺骨牌具有坐标xi与高度hi。现在Vasya想要知道,对于每一个多米诺骨牌如果他推倒的话,右侧会有多少个多米诺骨牌也会倒下。

想想看,一个多米诺倒下,如果它严格的触动右侧的多米诺骨牌,被触碰的也会倒下。换句话说,如果多米诺骨牌(初始坐标x和高度h)倒下,会导致所有在[ X + 1,x + H - 1]范围内的多米诺骨牌倒下。

fzu 2163-LMLPHP Input

输入有多组测试数据,处理到文件结尾。

每组测试数据第一行包含整数n(1≤N≤10^5),这是多米诺骨牌的数量。然后n行,每行包含两个整数xi与hi(-10^8≤xi≤10^8 ,2 ≤hi≤108),xi表示多米诺骨牌的坐标和hi表示多米诺骨牌的高度。没有两个多米诺骨牌在同一个坐标点上。

fzu 2163-LMLPHP Output

对于每组数据输出一行,包含n个空格分隔的数Zi - 表示倒下的多米诺骨牌数量,如果Vasya推第i个多米诺骨牌(包括多米诺骨牌本身)。

fzu 2163-LMLPHP Sample Input

4
16 5
20 5
10 10 18 2
3
6 7
2 9
-6 10

fzu 2163-LMLPHP Sample Output

3 1 4 1
1 2 3
 
 单调栈,如此神奇,呵呵...
 这类题做少了
 #include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<stack>
using namespace std;
typedef __int64 LL; struct node
{
LL x;
int h;
int id;
int i;
}f[];
int ans[]; bool cmp(node n1,node n2)
{
return n1.x<n2.x;
}
void solve(int n)
{
int i;
node k;
stack<node>Q;
Q.push(f[]); for(i=;i<=n;i++){
while( !Q.empty() && (Q.top().x+Q.top().h-) <f[i].x)
{
k=Q.top();
Q.pop();
ans[k.i]=f[i].id-k.id;
}
Q.push(f[i]);
}
}
int main()
{
int n,i;
while(scanf("%d",&n)>)
{
for(i=;i<=n;i++)
{
scanf("%I64d%d",&f[i].x,&f[i].h);
f[i].i=i;
}
n++;
f[n].x=(<<);
sort(f+,f++n,cmp);
for(i=;i<=n;i++)
{
f[i].id=i;
}
solve(n);
for(i=;i<n;i++)
{
if(i!=)printf(" ");
printf("%d",ans[i]);
}
printf("\n");
}
return ;
}
05-11 10:53
查看更多