Codeforces I. Barcelonian Distance(暴力)
题目描述:
In this problem we consider a very simplified model of Barcelona city.
Barcelona can be represented as a plane with streets of kind x=cx=c and y=cy=c for every integer cc (that is, the rectangular grid). However, there is a detail which makes Barcelona different from Manhattan. There is an avenue called Avinguda Diagonal which can be represented as a the set of points (x,y)(x,y) for which ax+by+c=0ax+by+c=0.
One can walk along streets, including the avenue. You are given two integer points AAand BB somewhere in Barcelona. Find the minimal possible distance one needs to travel to get to BB from AA.
Input
The first line contains three integers aa, bb and cc (−109≤a,b,c≤109−109≤a,b,c≤109, at least one of aa and bb is not zero) representing the Diagonal Avenue.
The next line contains four integers x1x1, y1y1, x2x2 and y2y2 (−109≤x1,y1,x2,y2≤109−109≤x1,y1,x2,y2≤109) denoting the points A=(x1,y1)A=(x1,y1) and B=(x2,y2)B=(x2,y2).
Output
Find the minimum possible travel distance between AA and BB. Your answer is considered correct if its absolute or relative error does not exceed 10−610−6.
Formally, let your answer be aa, and the jury's answer be bb. Your answer is accepted if and only if |a−b|max(1,|b|)≤10−6|a−b|max(1,|b|)≤10−6.
Examples
Input
1 1 -3
0 3 3 0
Output
4.2426406871
Input
3 1 -9
0 3 3 -1
Output
6.1622776602
思路:
刚开始,凭感觉做的是通过画图,像是从A点出发直着向上/向下到达直线,从A点出发直着向右/向左到达直线,与从B出发直着向上/向下到达直线,从B出发直着向右/向左到达直线,共四种组合的距离,加上曼哈顿距离,求最小距离
然后,想的是从A点开始,到与直线相交,的每一种情况下,B也同样处理得到距离的最小值,说的不太清楚,如图
对每个A来说,算出每个B路径下的总的距离,取最小。
这里面有几个路径在程序中没包括,就是上图的紫色和黄色,组合在一起的情况,不过这样算会最终超时
小心的是数据用long long,不然计算中会溢出,还有输出格式的设置,想要设置成浮点数(不用科学计数法),设置精度等等
知识点:暴力
代码:
(忽略dd函数,那是会查实的,程序实际调用的是dd1函数)
#include <iostream>
#include <cmath>
#include <iomanip>
#include <climits>
using namespace std;
long long a,b,c;
long long x1,y1;
long long x2,y2;
double dd1(long long x1,long long y1,long long x2,long long y2)
{
double dist1 = ;
double yy1 = (-a*x1-c)/(double)b;
dist1 += abs(y1-yy1);
double yy2 = (-a*x2-c)/(double)b;
dist1 += abs(y2-yy2);
dist1 += sqrt((x1-x2)*(x1-x2)+(yy1-yy2)*(yy1-yy2));
double dist2 = ;
double xx2 = (-b*y2-c)/(double)a;
dist2 += abs(x2-xx2);
dist2 += abs(y1-yy1);
dist2 += sqrt((x1-xx2)*(x1-xx2)+(yy1-y2)*(yy1-y2));
double dist3 = ;
double xx1 = (-b*y1-c)/(double)a;
dist3 += abs(xx1-x1);
dist3 += abs(x2-xx2);
dist3 += sqrt((xx1-xx2)*(xx1-xx2)+(y1-y2)*(y1-y2));
double dist4 = ;
dist4 += abs(xx1-x1);
dist4 += abs(y2-yy2);
dist4 += sqrt((xx1-x2)*(xx1-x2)+(y1-yy2)*(y1-yy2));
double dist = dist1;
dist = min(dist,dist2);
dist = min(dist,dist3);
dist = min(dist,dist4);
return dist;
}
double dd(int x1,int x2,int x3,int x4)
{
double mdist = INT_MAX;
double xx1 = (-b*y1-c)/(double)a;
double xx2 = (-b*y2-c)/(double)a;
int length = abs(x1-(int)xx1);
for(int i = ;i<=length;i++)
{
double dist = ;
int x;
if(a*x1+b*x2+c<)
{
x = x1+i;
}
else
{
x = x1-i;
}
dist += i;
double yy1 = (-a*x-c)/(double)b;
dist += abs(y1-yy1);
int length2 = abs(x2-(int)xx2);
for(int j = ;j<=length2;j++)
{
if(a*x2+b*x2+c<)
{
x = x2+j;
}
else
{
x = x2-j;
}
dist += j;
double yy2 = (-a*x-c)/(double)b;
dist += abs(y2-yy2);
dist += sqrt((x2-x1)*(x2-x1)+(yy1-yy2)*(yy1-yy2));
if(dist<mdist)
{
mdist = dist;
}
} }
double dist = dd1(x1,x2,y1,y2);
if(dist<mdist)
{
mdist = dist;
}
return mdist;
}
int main()
{
cin >> a >> b >> c;
cin >> x1 >> y1 >> x2 >> y2;
double dist = abs(x1-x2)+abs(y1-y2);
if(a!=&&b!=)
{
double ans = dd1(x1,y1,x2,y2);
cout.setf(ios_base::fixed,ios_base::floatfield);
cout << setprecision() << min(dist,ans) << endl;
}
else
{
cout.setf(ios_base::fixed,ios_base::floatfield);
cout << dist << endl;
}
return ;
}
Codeforces I. Barcelonian Distance(暴力)的更多相关文章
- Codeforces 1079D Barcelonian Distance(计算几何)
题目链接:Barcelonian Distance 题意:给定方格坐标,方格坐标上有两个点A,B和一条直线.规定:直线上沿直线走,否则沿方格走.求A到B的最短距离. 题解:通过直线到达的:A.B两点都 ...
- Codeforces Round #522 (Div. 2, based on Technocup 2019 Elimination Round 3) D. Barcelonian Distance 几何代数(简单)
题意:给出一条直线 ax +by+c=0 给出两个整点 (x1,y1) (x2,y2) 只有在x,y坐标至少有一个整点的时 以及 给出的直线才有路径(也就是格子坐标图的线上) 问 两个整点所需要 ...
- codeforces 724B Batch Sort(暴力-列交换一次每行交换一次)
题目链接:http://codeforces.com/problemset/problem/724/B 题目大意: 给出N*M矩阵,对于该矩阵有两种操作: (保证,每行输入的数是 1-m 之间的数且不 ...
- codeforces 897A Scarborough Fair 暴力签到
codeforces 897A Scarborough Fair 题目链接: http://codeforces.com/problemset/problem/897/A 思路: 暴力大法好 代码: ...
- Codeforces A. Playlist(暴力剪枝)
题目描述: Playlist time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- [codeforces 200 A Cinema]暴力,优化
题意大致是这样的:有一个有n行.每行m个格子的矩形,每次往指定格子里填石子,如果指定格子里已经填过了,则找到与其曼哈顿距离最小的格子,然后填进去,有多个的时候依次按x.y从小到大排序然后取最小的.输出 ...
- Codeforces 161 D. Distance in Tree (树dp)
题目链接:http://codeforces.com/problemset/problem/161/D 题意: 给你一棵树,问你有多少对点的距离为k. 思路: dp[i][j]表示离i节点距离为j的点 ...
- Codeforces Gym 100531D Digits 暴力
Problem D. Digits 题目连接: http://codeforces.com/gym/100531/attachments Description Little Petya likes ...
- CodeForces 589B Layer Cake (暴力)
题意:给定 n 个矩形是a*b的,问你把每一块都分成一样的,然后全放一块,高度都是1,体积最大是多少. 析:这个题,当时并没有完全读懂题意,而且也不怎么会做,没想到就是一个暴力,先排序,先从大的开始选 ...
随机推荐
- Linux下tar的安装方式
tar -c: 建立压缩档案-x:解压-t:查看内容-r:向压缩归档文件末尾追加文件-u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用其中一个.下面的 ...
- OCR(Optical Character Recognition)算法总结
https://zhuanlan.zhihu.com/p/84815144 最全OCR资料汇总,awesome-OCR
- java 快速定位线上cpu偏高
1.top -c 加 大写P 查找高进程ID 2.top -Hp 加 大写 P 查找高线程ID 3.printf '%x\n' 线程ID 转成16进制 4.jstack 进程ID | grep 16进 ...
- nginx配置优化提高并发量
1 nginx配置优化提高并发量 worker_processes 2; 这个按照CPU的核数来决定 2 worker_connections 65535; 这个一般设置65535即可 每个进程允许的 ...
- SQL"已更新或者删除的行值要么不能使该行成为唯一行,要么改变了多个行(X行)“解决办法
这种问题大多是由于没有主键(PK)导致同一张表中存在若干条相同的数据.DBMS存储时,只为其存储一条数据,因为DBMS底层做了优化,以减少数据冗余.所以删除或更新一条重复数据就牵一发而动全身. 解决方 ...
- 嵌入式02 STM32 实验09 独立/窗口看门狗
一.独立看门狗和窗口看门狗 看门狗:单片机系统在外界的干扰下会出现程序跑飞的现象导致死循环,或者崩溃,看门狗电路就是为了避免这种情况的发生,看门狗的作用就是在一定的事件内(通过计数器实现)若没有收到喂 ...
- 网络爬虫第五章之Scrapy框架
第一节:Scrapy框架架构 Scrapy框架介绍 写一个爬虫,需要做很多的事情.比如:发送网络请求.数据解析.数据存储.反反爬虫机制(更换ip代理.设置请求头等).异步请求等.这些工作如果每次都要自 ...
- LeetCode 5214. 最长定差子序列(Java)HashMap
题目: 5214. 最长定差子序列 给你一个整数数组 arr 和一个整数 difference,请你找出 arr 中所有相邻元素之间的差等于给定 difference 的等差子序列,并返回其中最长的等 ...
- C语言--简易词法分析器
#include <stdio.h>#include <stdlib.h>#include <string.h>int p,m,syn,n,sum; / ...
- 从实践到原理,带你参透 gRPC
gRPC 在 Go 语言中大放异彩,越来越多的小伙伴在使用,最近也在公司安利了一波,希望这一篇文章能带你一览 gRPC 的巧妙之处,本文篇幅比较长,请做好阅读准备.本文目录如下: 简述 gRPC 是一 ...