E - Domino Principle

Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

Vasya is interested in arranging dominoes. He is fed up with common dominoes and he uses the dominoes of different heights. He put ndominoes on the table along one axis, going from left to right. Every domino stands perpendicular to that axis so that the axis passes through the center of its base. The i-th domino has the coordinate xi and the height hi. Now Vasya wants to learn for every domino, how many dominoes will fall if he pushes it to the right. Help him do that.

Consider that a domino falls if it is touched strictly above the base. In other words, the fall of the domino with the initial coordinate x and height h leads to the fall of all dominoes on the segment [x + 1, x + h - 1].

Input

The first line contains integer n (1 ≤ n ≤ 105) which is the number of dominoes. Then follow n lines containing two integers xi and hi( - 108 ≤ xi ≤ 108, 2 ≤ hi ≤ 108) each, which are the coordinate and height of every domino. No two dominoes stand on one point.

Output

Print n space-separated numbers zi — the number of dominoes that will fall if Vasya pushes the i-th domino to the right (including the domino itself).

Sample Input

Input
4
16 5
20 5
10 10
18 2
Output
3 1 4 1 
Input
4
0 10
1 5
9 10
15 10
Output
4 1 2 1 

很久没写博客了。这是一道dp题。
先排序,在从后往前更新,每次计算第i个卡米诺骨牌的能压倒的数量的时候先考虑是否能压倒下一个骨牌,如果能,加上这个骨牌能压倒的骨牌数,在考虑下一个骨牌的压不倒的第一个骨牌,看这个骨牌能否被要求的骨牌压倒,如果能,加上这个骨牌能压倒的骨牌数,在依次往后推。。。
//#include <iostream>
//#include<algorithm>
//using namespace std;
//
//struct dom
//{
// int x,h,num,id;
//}d[100010];
//
//int cmp1(const dom d1,const dom d2)
//{
// if(d1.x<d2.x)
// return 1;
// else
// return 0;
//}
//int cmp2(const dom d1,const dom d2)
//{
// if(d1.id<d2.id)
// return 1;
// else
// return 0;
//}
//int main()
//{
// int n,i;
// cin>>n;
// for(i=0;i<n;i++)
// {
// cin>>d[i].x>>d[i].h;
// d[i].id=i;
// d[i].num=1;
// }
// sort(d,d+n,cmp1);
// int cnt=0,bg=0;
// for(i=0;i<n-1;i++)
// {
// int tmp=d[i].h;
// for(int j=i+1;j<n;j++)
// {
// if(j>i+1)
// tmp=max(tmp-(d[j-1].x-d[j-2].x),d[j-1].h);
// if(tmp>d[j].x-d[j-1].x)
// {
// d[i].num++;
// }
// else
// {
// int t=d[i].num;
// for(int k=i+1;k<j;k++)
// {
// d[k].num=--t;
// }
// i=j-1;
// break;
// }
// }
// }
// sort(d,d+n,cmp2);
// for(i=0;i<n;i++)
// cout<<d[i].num<<' ';
// cout<<endl;
// return 0;
//} #include <iostream>
#include<algorithm>
using namespace std; struct dom
{
int x,h,num,id;
}d[];
int ans[]; int cmp1(const dom d1,const dom d2)
{
if(d1.x<d2.x)
return ;
else
return ;
} int main()
{
int n,i;
cin>>n;
for(i=;i<n;i++)
ans[i]=;
for(i=;i<n;i++)
{
cin>>d[i].x>>d[i].h;
d[i].id=i;
d[i].num=;
}
sort(d,d+n,cmp1);
for(i=n-;i>=;i--)
{
int j=i+;
while(j!=n&&d[j].x<d[i].x+d[i].h)
{
d[i].num+=d[j].num;
j=d[j].num+j;
}
ans[d[i].id]=d[i].num;
}
for(i=;i<n;i++)
cout<<ans[i]<<' ';
cout<<endl;
return ;
}

CodeForces 56E-Domino Principle的更多相关文章

  1. CodeForces - 50A Domino piling (贪心+递归)

    CodeForces - 50A Domino piling (贪心+递归) 题意分析 奇数*偶数=偶数,如果两个都为奇数,最小的奇数-1递归求解,知道两个数都为1,返回0. 代码 #include ...

  2. codeforces A. Domino 解题报告

    题目链接:http://codeforces.com/problemset/problem/353/A 题目意思:通俗地说,就是当上下两半的数的总和不完全是偶数时,通过上下调换某些骨牌来使这两半的数和 ...

  3. codeforces 1269D. Domino for Young (二分图证明/结论题)

    链接:https://codeforces.com/contest/1269/problem/D 题意:给一个不规则的网格,在上面放置多米诺骨牌,多米诺骨牌长度要么是1x2,要么是2x1大小,问最多放 ...

  4. 【CF56E】Domino Principle(线性扫描,伪DP)

    每块多米诺骨牌所在的位置设为x,每块多米诺骨牌高度为h.如果将x位置上的多米诺骨牌向右翻到,它就可以影响[x+1, x+h-1]范围内的所有多米诺骨牌,让他们也翻到,同时这些被翻到的多米诺骨牌还能影响 ...

  5. Codeforces Good Bye 2015 C. New Year and Domino 前缀和

    C. New Year and Domino 题目连接: http://www.codeforces.com/contest/611/problem/C Description They say &q ...

  6. Codeforces 500 E. New Year Domino

    \(>Codeforces \space 500 E. New Year Domino<\) 题目大意 : 数轴上有序排列着 \(n\) 块多米诺骨牌, 第 \(i\) 块骨牌坐标为 \( ...

  7. Codeforces Round #609 (Div. 2) D. Domino for Young

    链接: https://codeforces.com/contest/1269/problem/D 题意: You are given a Young diagram. Given diagram i ...

  8. Codeforces Round #588 (Div. 2) C. Anadi and Domino(思维)

    链接: https://codeforces.com/contest/1230/problem/C 题意: Anadi has a set of dominoes. Every domino has ...

  9. Codeforces 238 div2 B. Domino Effect

    题目链接:http://codeforces.com/contest/405/problem/B 解题报告:一排n个的多米诺骨牌,规定,若从一边推的话多米诺骨牌会一直倒,但是如果从两个方向同时往中间推 ...

随机推荐

  1. NOR FLASH与NAND FLASH的区别

    NOR和NAND是现在市场上两种主要的非易失闪存技术.Intel于1988年首先开发出NOR flash技术,彻底改变了原先由EPROM和EEPROM一统天下的局面.紧接着,1989年,东芝公司发表了 ...

  2. Merge k Sorted Lists [LeetCode]

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. S ...

  3. A New Tetris Game

    时间限制(普通/Java):1000MS/10000MS     运行内存限制:65536KByte 总提交: 40            测试通过: 12 描述 曾经,Lele和他姐姐最喜欢,玩得最 ...

  4. 将windows下的PLSQL转移到Ubuntu上

    1,首先下载安装wine,安装不成功的更新下源即可. 2,Ctal+Alt+T 打开控制台: cd ~/.wine/drive_c mkdir -p oracle/bin mkdir -p oracl ...

  5. POJ 3249 拓扑排序+DP

    貌似是道水题.TLE了几次.把所有的输入输出改成scanf 和 printf ,有吧队列改成了数组模拟.然后就AC 了.2333333.... Description: MR.DOG 在找工作的过程中 ...

  6. 让DIV实现抖动效果!

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  7. 微软WTL模板库完整版安装(VS2010+windows7X64位环境下)分享

    一:简介 想必大家对于微软的MFC应该都比较熟悉.但是WTL可能很多人比较陌生吧.下面我就简单的说说这个库. 首先对这个库的做个简单的介绍吧. WTL 是 Windows Template Libra ...

  8. struts2 ModelDriven 和 Preparable 拦截器

    Struts2 运行流程图-1

  9. lucas 定理学习

    大致意思就是求组合数C(n , m) % p的值, p为一个偶数 可以将组合数的n 和 m都理解为 p 进制的表示 n  = ak*p^k + a(k-1)*p^(k-1) + ... + a1*p ...

  10. C语言基础--while循环

    while循环格式: while (条件表达式) { 语句; ... }   执行说明: while是对给定的条件进行判断, 如果条件满足, 就执行while后面大括号中的内容, 执行完毕之后会再次判 ...