Codeforces Contest 1075 problem C The Tower is Going Home

On a chessboard with a width of 109 and a height of 109, the rows are numbered from bottom to top from 1 to 109, and the columns are numbered from left to right from 1 to 109. Therefore, for each cell of the chessboard you can assign the coordinates (x,y), where x is the column number and y is the row number.

Every day there are fights between black and white pieces on this board. Today, the black ones won, but at what price? Only the rook survived, and it was driven into the lower left corner — a cell with coordinates (1,1). But it is still happy, because the victory has been won and it’s time to celebrate it! In order to do this, the rook needs to go home, namely — on the upper side of the field (that is, in any cell that is in the row with number 109).

Everything would have been fine, but the treacherous white figures put spells on some places of the field before the end of the game. There are two types of spells:

Vertical. Each of these is defined by one number x. Such spells create an infinite blocking line between the columns x and x+1.
Horizontal. Each of these is defined by three numbers x1, x2, y. Such spells create a blocking segment that passes through the top side of the cells, which are in the row y and in columns from x1 to x2 inclusive. The peculiarity of these spells is that it is impossible for a certain pair of such spells to have a common point. Note that horizontal spells can have common points with vertical spells.
在这里插入图片描述
An example of a chessboard.
Let’s recall that the rook is a chess piece that in one move can move to any point that is in the same row or column with its initial position. In our task, the rook can move from the cell (r0,c0) into the cell (r1,c1) only under the condition that r1=r0 or c1=c0 and there is no blocking lines or blocking segments between these cells (For better understanding, look at the samples).

Fortunately, the rook can remove spells, but for this it has to put tremendous efforts, therefore, it wants to remove the minimum possible number of spells in such way, that after this it can return home. Find this number!

Input
The first line contains two integers n and m (0≤n,m≤105) — the number of vertical and horizontal spells.

Each of the following n lines contains one integer x (1≤x<109) — the description of the vertical spell. It will create a blocking line between the columns of x and x+1.

Each of the following m lines contains three integers x1, x2 and y (1≤x1≤x2≤109, 1≤y<109) — the numbers that describe the horizontal spell. It will create a blocking segment that passes through the top sides of the cells that are in the row with the number y, in columns from x1 to x2 inclusive.

It is guaranteed that all spells are different, as well as the fact that for each pair of horizontal spells it is true that the segments that describe them do not have common points.

Output
In a single line print one integer — the minimum number of spells the rook needs to remove so it can get from the cell (1,1) to at least one cell in the row with the number 109
Examples
inputCopy
2 3
6
8
1 5 6
1 9 4
2 4 2
outputCopy
1
inputCopy
1 3
4
1 5 3
1 9 4
4 6 6
outputCopy
1
inputCopy
0 2
1 1000000000 4
1 1000000000 2
outputCopy
2
inputCopy
0 0
outputCopy
0
inputCopy
2 3
4
6
1 4 3
1 5 2
1 6 5
outputCopy
2

题意:

有一个点要从1,1出发,到为1e9的任何一个地方,给你n个第一种障碍物:在x轴第x位,跨越1到1e9的y轴,
x1 x2 y,在y轴的第y位,跨越x1到x2,问你最少需要删掉多少障碍物。

题解:

这道题就是问你有多少围绕着1,1的封闭区域,但两个共享一条边的只算一个,因为去掉一条边就可以过去了,,那么我们只需要找到横着的和竖着的最少交叉多少就可以了。

#include<bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const int N=1e5+5;
int a[N],vis[N];
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    sort(a+1,a+1+n);
    int ans=0;
    int x1,x2,y;
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d%d",&x1,&x2,&y);
        if(x1==1)
        {
            if(x2==inf)
            {
                ans++;
                continue;
            }
            int pos=upper_bound(a+1,a+1+n,x2)-a-1;
            while(vis[pos])
                pos--;
            if(pos)
               ans++,vis[pos]=1;
        }
    }
    printf("%d\n",ans);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值