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个的多米诺骨牌,规定,若从一边推的话多米诺骨牌会一直倒,但是如果从两个方向同时往中间推 ...
随机推荐
- [BI基础] 一些不得不了解的概念
0.Hadoop hadoop主要是用来对海量数据进行存储和计算的. 它本身是一个分布式系统,核心由分布式文件系统hdfs,和分布式计算框架mapreduce组成,在存储和计算时能够发挥出集群中每台机 ...
- WCF中Service Configuration Editor的使用方法
1.在App.config文件上右击,选择Edit WCF Configuration.... 或者打开Program Files\Microsoft Visual Studio 8\Common7\ ...
- hdu----(1528)Card Game Cheater(最大匹配/贪心)
Card Game Cheater Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- hduoj-----(1068)Girls and Boys(二分匹配)
Girls and Boys Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- C++中颜色的设置
1.改变整个控制台的颜色用 system("color 0A"); 其中color后面的0是背景色代号,A是前景色代号.各颜色代码如下: 0=黑色 1=蓝色 2=绿色 3=湖蓝色 ...
- while, do-while ,switch···case语句的学习与运用
1.while语句:当···的时候 格式:初始条件 while(循环条件) { 循环体; 状态改变; } 相当于 ...
- TCP协议基础
IP协议是Internet上使用的一个关键协议,它的全称是Internet Protocol,即Internet协议,通常简称IP协议.通过使用IP协议,使Internet·成为一个允许连接不同类型 ...
- 各种常用函数 (SQL)
数学函数 1.绝对值 S:select abs(-1) value O:select abs(-1) value from dual 2.取整(大) S:select ceiling(-1.001 ...
- TopCoder SRM 582 ColorfulBuilding
DP 思路是三维,但是时间肯定会超时,需要根据其特殊性质加两个标记数组,优化成二维. 刚开始想了N久N久,没感觉,还是动手画了一下才有用呀,意淫再久,不如动手呀. 代码: #include<i ...
- 制作Linux下程序安装包——使用脚本打包bin、run等安装包
制作简单的安装包的时候可以简单的用cat命令连接两个文件,然后头部是脚本文件,执行的时候把下面的文件分解出来就行了.一般这个后部分的文件是个压缩 包,那样,就能够打包很多文件了,在脚本中解压出来即可. ...