CodeForces 56E-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
4
16 5
20 5
10 10
18 2
3 1 4 1
4
0 10
1 5
9 10
15 10
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的更多相关文章
- CodeForces - 50A Domino piling (贪心+递归)
CodeForces - 50A Domino piling (贪心+递归) 题意分析 奇数*偶数=偶数,如果两个都为奇数,最小的奇数-1递归求解,知道两个数都为1,返回0. 代码 #include ...
- codeforces A. Domino 解题报告
题目链接:http://codeforces.com/problemset/problem/353/A 题目意思:通俗地说,就是当上下两半的数的总和不完全是偶数时,通过上下调换某些骨牌来使这两半的数和 ...
- codeforces 1269D. Domino for Young (二分图证明/结论题)
链接:https://codeforces.com/contest/1269/problem/D 题意:给一个不规则的网格,在上面放置多米诺骨牌,多米诺骨牌长度要么是1x2,要么是2x1大小,问最多放 ...
- 【CF56E】Domino Principle(线性扫描,伪DP)
每块多米诺骨牌所在的位置设为x,每块多米诺骨牌高度为h.如果将x位置上的多米诺骨牌向右翻到,它就可以影响[x+1, x+h-1]范围内的所有多米诺骨牌,让他们也翻到,同时这些被翻到的多米诺骨牌还能影响 ...
- 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 ...
- Codeforces 500 E. New Year Domino
\(>Codeforces \space 500 E. New Year Domino<\) 题目大意 : 数轴上有序排列着 \(n\) 块多米诺骨牌, 第 \(i\) 块骨牌坐标为 \( ...
- 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 ...
- 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 ...
- Codeforces 238 div2 B. Domino Effect
题目链接:http://codeforces.com/contest/405/problem/B 解题报告:一排n个的多米诺骨牌,规定,若从一边推的话多米诺骨牌会一直倒,但是如果从两个方向同时往中间推 ...
随机推荐
- Xcode 8 打包上线 iTunes Connect 找不到构建版本
Xcode 8 打包上线 iTunes Connect 找不到构建版本 最近苹果推出新的mac操作系统(macOS Sierra 10.12),大家可能都已经升级了,作为一个开发者,小编肯定是第一时间 ...
- Hadoop运维操作
1. 处理hadoop的namenode宕机 处理措施: 进入hadoop的bin目录,重启namenode服务 操作命令: cd path/to/hadoop/bin ./hadoop-d ...
- JAVA读取EXCEL文件异常Unable to recognize OLE stream
异常: jxl.read.biff.BiffException: Unable to recognize OLE stream at jxl.read.biff.CompoundFile.<in ...
- 转 velocity 模板使用总结
Velocity是一个基于java的模板引擎.它允许任何人仅仅简单的使用模板语言来引用由java代码定义的对象. 当Velocity应用于web开发时,界面设计人员可以和java程序开发人员同步开发一 ...
- 如何解决linQ“序列不包含任何元素”的问题?
描述:该问题出现在校对BT种子数据的时候遇到的bug,原因是使用linq查找元素的时候 B是A的一个子集, B在A中一定存在,这种情况下就不会抛出异常情况,反之B的一部分不属于A就会异常应为B中的一个 ...
- linux系统字符集
Linux中中文乱码问题通常是由于字符集与windows不兼容所引起,windows的中文字符集是双字节的GBK编码linux采用的是3字节的utf-8编码,所以在windows下用工具连接linux ...
- mysql启动错误
1.启动时,显示ERROR tail localhost.localdomain.err 错误日志 2.新增目录,启动成功
- 关于linux服务器的批量维护、批量升级、
由于版权费用原因,众多中小服务器采用linux服务器进行功能处理.在进行批量升级.更新业务程序时.可以采用如下批量方案: 1.打包成rpm包,并配置yum源服务器,以支持个人的yum服务器进行处理.一 ...
- iOS --- 通过openURL实现APP之间跳转并传递数据
在IOS中,实现一个应用启动另外一个应用,使用UIApplication的openURL:方法就可实现,这里以test跳到test02为例.(需要先创建这两个工程) 注册自定义URL协议(在test中 ...
- WordPress的神器
导读 Markdown 是一种简单的标记语言,旨在帮助你花费更小的代价来格式化纯文本文档.在 WordPress 下你可以使用 HTML 或者可视化编辑器来格式化你的文档,但是使用 markdown ...