CodeForces 993A Two Squares(数学 几何)
https://codeforces.com/problemset/problem/993/A
题意:
给你两个矩形,第一行是一个正面表示的矩形,第二个是一个旋转四十五度角的矩形,问这两个矩形是否相交
思路:
刚开始的想法:
因为题目数据范围很小,所以很容易想到的是暴力枚举每个矩形中的每个点,若有点既在第一个矩形又在第二个矩形内,则这两个矩形相交
不过既然是数学题,就最好不要暴力了
要知道,如果两个正方形相交,那么其中一个正方形的四个角至少有一个处于另一个正方形内,或者一个正方形的中心处于另一个正方形内。
如果是正方形②的角在正方形①内的话,就很容易就可以判断,但是,如果是正方形①的角在正方形②内的话,就需要求出正方形②的边,然后根据点在直线的上下方关系来判断。
代码如下:
#include "iostream"
#include "algorithm"
using namespace std;
int main()
{
double nu,nd,nl,nr,mu,md,ml,mr,mx,my,x,y;//u,d,l,r,分别记录最上,最下,最左,最右的值,mx,my记录的是正方形②中心的坐标
nu=nr=mu=mr=-;//由于范围是-100~100,所以赋初值
nd=nl=md=ml=;
for(int i=;i<;i++){
cin>>x>>y;
nd=min(nd,y);
nu=max(nu,y);
nl=min(nl,x);
nr=max(nr,x);
}
for(int i=;i<;i++){
cin>>x>>y;
md=min(md,y);
mu=max(mu,y);
ml=min(ml,x);
mr=max(mr,x);
}
mx=(ml+mr)/;
my=(mu+md)/;
//正方形②的角在正方形①中的情况,分别判断四个角,有一个在里面就成立
if(mx>=nl&&mx<=nr&&md>=nd&&md<=nu||mx>=nl&&mx<=nr&&mu>=nd&&mu<=nu||ml>=nl&&ml<=nr&&my>=nd&&my<=nu||mr>=nl&&mr<=nr&&my>=nd&&my<=nu)
cout<<"YES"<<endl;
//正方形①的角在正方形②中的情况,分别判断四个角,有一个在里面就成立,其中,诸如nu+nr>=ml+my的式子是判断点在直线的上方还是下方
else if(nu+nr>=ml+my&&nu<=nr-mx+mu&&nu>=nr-mx+md&&nu+nr<=mr+my||nd+nr>=ml+my&&nd<=nr-mx+mu&&nd>=nr-mx+md&&nd+nr<=mr+my)
cout<<"YES"<<endl;
else if(nu+nl>=ml+my&&nu<=nl-mx+mu&&nu>=nl-mx+md&&nu+nl<=mr+my||nd+nl>=ml+my&&nd<=nl-mx+mu&&nd>=nl-mx+md&&nd+nl<=mr+my)
cout<<"YES"<<endl;
//一个正方形中心在另一个中的情况
else if(mx>=nl&&mx<=nr&&my>=nd&&my<=nu)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
return ;
}
这种想法不难, 就是有点麻烦。
另外在看别人的想法时看到一个比较有意思的解法:
from:https://blog.csdn.net/qq_40858062/article/details/80720092
因为正方形一个是正的,一个成45度角,第二个四边形完全在第一个上下左右就肯定不相交,要看的就是类似图中的情况,其实只要看中间这个小四边形周长和那个45度角四边形上下边界的差的大小关系就好了了,多画几张图可以看出来.....
照着他的思路敲了一遍,代码如下:
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <sstream>
const int INF=0x3f3f3f3f;
typedef long long LL;
const int mod=1e9+;
//const double PI=acos(-1);
#define Bug cout<<"---------------------"<<endl
const int maxn=1e5+;
using namespace std; struct point
{
int x;
int y;
}; bool cmp(point a,point b)
{
if(a.x!=b.x)
return a.x<b.x;
else
return a.y<b.y;
} int main()
{
point a[];
point b[];
for(int i=;i<;i++)
scanf("%d %d",&a[i].x,&a[i].y);
for(int i=;i<;i++)
scanf("%d %d",&b[i].x,&b[i].y);
sort(a,a+,cmp);
sort(b,b+,cmp);
int flag=;
if(b[].x<=a[].x&&b[].x>=a[].x&&b[].y<=a[].y&&b[].y>=a[].y)
{
int p=min(abs(a[].x-b[].x),abs(a[].x-b[].x));
int q=min(fabs(a[].y-b[].y),fabs(a[].y-b[].y));
if((p+q)*>=b[].y-b[].y)
flag=;
}
if(flag)
printf("YES\n");
else
printf("NO\n");
return ;
}
CodeForces 993A Two Squares(数学 几何)的更多相关文章
- Codeforces 993A. Two Squares(暴力求解)
解题思路(暴力解法) 平行于x轴的正方形和与x轴成45度倾斜的正方形相交的点中必定有整数点.即若两正方形相交,必定存在整数i,j,使(i,j)同时属于两个正方形. 我们把两个正方形中的整数点都找出来, ...
- hdu 1577 WisKey的眼神 (数学几何)
WisKey的眼神 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- hdu 1115 Lifting the Stone (数学几何)
Lifting the Stone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- Codeforces Round #332 (Div. 2)D. Spongebob and Squares 数学
D. Spongebob and Squares Spongebob is already tired trying to reason his weird actions and calcula ...
- ACM: FZU 2110 Star - 数学几何 - 水题
FZU 2110 Star Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Pr ...
- CodeForces 534C Polycarpus' Dice (数学)
题意:第一行给两个数,n 和 A,n 表示有n 个骰子,A表示 n 个骰子掷出的数的和.第二行给出n个数,表示第n个骰子所能掷出的最大的数,这些骰子都有问题, 可能或多或少的掷不出几个数,输出n个骰子 ...
- codeforces 687B - Remainders Game 数学相关(互质中国剩余定理)
题意:给你x%ci=bi(x未知),是否能确定x%k的值(k已知) ——数学相关知识: 首先:我们知道一些事情,对于k,假设有ci%k==0,那么一定能确定x%k的值,比如k=5和ci=20,知道x% ...
- CodeForces 342C Cupboard and Balloons (几何问题)
题意:给定一个 r 和 h,r 是上面那个半球的半径,h 是下面那个圆柱的高度,然后有一些半径为 r/2的气球,问你最多能放几个. 析:根据题意,很容易知道,先从下面往上放,两个两个的放,放到不能放的 ...
- Codeforces 1099 B. Squares and Segments-思维(Codeforces Round #530 (Div. 2))
B. Squares and Segments time limit per test 1 second memory limit per test 256 megabytes input stand ...
随机推荐
- AVCodecContext 结构体
typedef struct AVCodecContext { int bit_rate; int frame_number; //扩展数据,如mov 格式中audio trak 中aac 格式中es ...
- JSP页面获取其他页面传递的参数
jstl表达式获取方式: ${param.pid} el表达式获取方式: ${requestScope.attr} el表达式获取方式: ${attr} ---------------------- ...
- 第7章,c语言控制语句:分支和跳转
7.1 if语句 通用形式:if(expression) statment 7.2 if else语句 通用形式:if(expression) startment else startment2 7. ...
- Unity3D 协程的介绍和使用
我是快乐的搬运工 http://blog.csdn.net/u011397120/article/details/61236055 ---------------------------------- ...
- UML-设计模式-缓存策略
继续前一章<本地服务容错> 问题:考虑加载内存ProductCatalog缓存和基于LocalProducts文件缓存的可选方案: 一种是惰性初始化(lazy init):当实际读取外部产 ...
- IDEA控制台输出中文乱码日志文件正常
控制台中文输出乱码但输出的日志文件正常 idea.exe.vmoptions与idea64.exe.vmoptions已经配置 -Dfile.encoding=UTF-8 logback.xml中也配 ...
- 2019.1的IDEA的Pulgins无法使用解决
第一步 第二步
- cf 1241 E. Paint the Tree(DP)
题意: 有一颗树,n个点,边有边权. 有无限多种颜色,每个点可以同时染上k种颜色,如果一条边的两个端点 拥有至少一种相同的颜色,那么说这条边是“饱和的”. 问:所有“饱和边”的权值和最大为多少,只需要 ...
- (4)关于Alpha通道问题
其实,我还是不理解,我还是先把我目前懂得和觉得有用的东西先存下来 =================================================================== ...
- 吴裕雄--天生自然 JAVASCRIPT开发学习: 正则表达式
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...