`
Simone_chou
  • 浏览: 184625 次
  • 性别: Icon_minigender_2
  • 来自: 广州
社区版块
存档分类
最新评论

Domino Principle(排序)

    博客分类:
  • CF
 
阅读更多
E. Domino Principle
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 test(s)
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 

     

       题意:

       给出 n(1 ~ 100000),代表有 n 张牌,后给出 n 张牌的 x 坐标和 h 高度,问每张牌向右倒的话,一共最多能倒多少张牌。

 

       思路:

       排序。记录每张牌能到达的最远的坐标 next,输出顺序 idex,x 坐标 num,最大能到达的牌数 to。先对 num 由小到大 sort 一遍,后从最后一张牌开始往后倒,因为最后一张牌倒的话只有 1 张,之后判断的时候,判断能不能使下一张牌倒下,如果能则这张牌倒下的牌数加上下一张牌的倒排数,同时跳到这张牌能到达的最远牌数(即 num [ i ].to += num [ i + 1 ].to 且 to += no [ i + 1 ].to )。最后输出的时候按 idex 由小到达再 sort 一遍后,一次输出 to 即可。

        比赛的时候心态很重要。对于边界处理真的是非常烦躁,明明会做的题一直卡在代码的实现上,有时候又因为太过紧张而漏洞百出,忘这忘那呢,思路又混乱,必须提高思维的练习才行。

 

        AC:

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int MAX = 100005;

typedef struct {
    int idex, num, next, to;
}node;

node no[MAX];

bool cmp1 (node a, node b) { return a.num < b.num; }
bool cmp2 (node a, node b) { return a.idex < b.idex; }

int main() {

    int n;
    scanf("%d", &n);

    for (int i = 0; i < n; ++i) {
        int ans;
        scanf("%d%d", &no[i].num, &ans);
        no[i].next = no[i].num + ans - 1;
        no[i].idex = i;
    }

    sort(no, no + n, cmp1);
    no[n - 1].to = 1;

    for (int i = n - 2; i >= 0; --i) {
        int t = i + 1;
        no[i].to = 1;
        while (t < n && no[t].num <= no[i].next) {
            no[i].to += no[t].to;
            t += no[t].to;
        }
    }

    sort(no, no + n, cmp2);
    for (int i = 0; i < n; ++i) {
        printf("%d", no[i].to);
        i == n - 1 ? printf("\n") : printf(" ");
    }

    return 0;
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics