Codeforces Round #512 B.Vasya and Cornfield

本篇博客详细解析了Codeforces上一道关于判断点是否在由四个顶点定义的特殊矩形内的算法题。通过使用叉积计算,判断m个草蜢的位置是否位于特定矩形边界或内部,提供了完整的C++代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

B. Vasya and Cornfield

time limit per test1 second memory limit per test:256 megabytes input:standard input output:standard output
Vasya owns a cornfield which can be defined with two integers n and d.The cornfield can be represented as rectangle with vertices having Cartesian coordinates (0,d),(d,0),(n,n−d) and (n−d,n).
![An example of a cornfield with n=7 and d=2](https://codeforces.com/predownloaded/d4/77/d4774c8e0335ef834f224f203b990e3154fc535a.png)
Vasya also knows that there are m grasshoppers near the field (maybe even inside it). The i-th grasshopper is at the point (xi,yi). Vasya does not like when grasshoppers eat his corn, so for each grasshopper he wants to know whether its position is inside the cornfield (including the border) or outside. Help Vasya! For each grasshopper determine if it is inside the field (including the border).

Input
The first line contains two integers n and d (1≤d<n≤100).
The second line contains a single integer m (1≤m≤100) — the number of grasshoppers.
The i-th of the next m lines contains two integers xi and yi (0≤xi,yi≤n) — position of the i-th grasshopper.

Output
Print m lines. The i-th line should contain “YES” if the position of the i-th grasshopper lies inside or on the border of the cornfield. Otherwise the i-th line should contain “NO”.
You can print each letter in any case (upper or lower).

Examples

input

7 2
4
2 4
4 1
6 3
4 5

output

YES
NO
NO
YES

input

8 7
4
4 4
2 8
8 1
6 1

output

YES
NO
YES
YES

Note

The cornfield from the first example is pictured above. Grasshoppers with indices 1 (coordinates (2,4)) and 4 (coordinates (4,5)) are inside the cornfield.
The cornfield from the second example is pictured below. Grasshoppers with indices 1 (coordinates (4,4)), 3
(coordinates (8,1)) and 4 (coordinates (6,1)) are inside the cornfield.

![](https://codeforces.com/predownloaded/30/b5/30b5e2a9e0f28c1052b75c86da5045de2d090d00.png)

题解

题目大意就是给定 n 和 d ,在(0,d),(d,0),(n,n−d) 和 (n−d,n)这四个点构成的矩形里,给出 m 次询问,再给出点的坐标,询问此点是否在这个矩形内。可以通过叉积来计算点是否在矩形内(之后补充,再次挖坑…)。代码如下:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std ;

typedef struct{
    double x ;
    double y ;
}point ;

double xmulti( point p1 , point p2 , point p0 ){
    return ((p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y)) ;
}

int main(){
    int n , d ;
    while ( cin >> n >> d ){
        int m ;
        cin >> m ;
        point p1 , p2 , p3 , p4 ;
        p1.x = 0 ;
        p1.y = d ;
        p2.x = d ;
        p2.y = 0 ;
        p3.x = n ;
        p3.y = n - d ;
        p4.x = n - d ;
        p4.y = n ;
        for ( int i = 0 ; i < m ; i ++ ){
            point p0 ;
            cin >> p0.x >> p0.y ;
            if ( xmulti( p1 , p2 , p0 ) * xmulti( p3 , p4 , p0 ) >= 0
              && xmulti( p2 , p3 , p0 ) * xmulti( p4 , p1 , p0 ) >= 0 ){
                cout << "YES" << endl ;
            }else{
                cout << "NO" << endl ;
            }
        }
    }
    return 0 ;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值