B. Vasya and Cornfield
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.
题解
题目大意就是给定 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 ;