关于三分的暴力技巧(暴力好)

关于三分的暴力技巧

以下为暴力专场(前方高能

现在以hdu4454为例

1.这道题的一个总结点是暴力对于精度不超过1e-2的东西,我们可以直接暴力求解

2.几何:对于一个圆(整个几何)都需要掌握三角函数的运用,这里有一篇非常不错的文章可以学习

3.圆到矩形的最短距离

关于圆到矩形的最短距离,主要是关于是到

矩形上垂直一点到圆最小(这时x和y都被“包围”);

矩形的四个顶点

具体看代码实现

现在附上超级牛逼的暴力代码(感谢qt神犇

#include<cmath>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdlib>
using namespace std;
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch))
    {
        if(ch=='-') f=-1;
        ch=getchar();
    }
    while(isdigit(ch))
    {
        x=(x<<3)+(x<<1)+ch-'0';
        ch=getchar();
    }
    return x*f;
}
inline void out()
{
    fclose(stdin);
    fclose(stdout);
}
const double pi=acos(-1.0);
struct Point{
    double x,y,r;
}C,P,st;
double xx,yy,x2,y2,miny,maxy,minx,maxx;
inline double dist(Point a,Point b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
inline double Rdist(Point a)
{
    double x=0.0,y=0.0;
    if(a.x<minx) x=minx-a.x;
    else if(a.x>maxx) x=a.x-maxx;
    if(a.y<miny) y=miny-a.y;
    else if(a.y>maxy) y=a.y-maxy;
    return sqrt(x*x+y*y);
}
int main(void)
{
    while(1)
    {
        scanf("%lf%lf",&st.x,&st.y);
        if(st.x==0&&st.y==0) break;
        double ans=1e10;
        scanf("%lf%lf%lf",&C.x,&C.y,&C.r);
        scanf("%lf%lf%lf%lf",&xx,&yy,&x2,&y2);
        minx=min(xx,x2),maxx=max(xx,x2);
        miny=min(yy,y2),maxy=max(yy,y2);
        for(double rad=0;rad<=360;rad+=0.005)
        {
            Point P;
            P.x=C.x+C.r*cos(rad/180*pi);
            P.y=C.y+C.r*sin(rad/180*pi);
            ans=min(ans,dist(P,st)+Rdist(P));
        }
        printf("%.2f\n",ans);
    }
    out();
    return 0;
}

还有正解(所谓的正解都是忽悠老实人的


#include<algorithm>
#include<iostream>
#include<fstream>
#include<sstream>
#include<cstring>
#include<cstdlib>
#include<string>
#include<vector>
#include<cstdio>
#include<queue>
#include<stack>
#include<cmath>
#include<map>
#include<set>
#define FF(i, a, b) for(int i=a; i<b; i++)
#define FD(i, a, b) for(int i=a; i>=b; i--)
#define REP(i, n) for(int i=0; i<n; i++)
#define CLR(a, b) memset(a, b, sizeof(a))
#define LL long long
#define PB push_back
#define eps 1e-10
#define debug puts("**debug**");
using namespace std;
const double PI = acos(-1);

struct Point
{
    double x, y;
    Point(double x=0, double y=0):x(x), y(y){}
};
typedef Point Vector;

Vector operator + (Vector a, Vector b) { return Vector(a.x+b.x, a.y+b.y); }
Vector operator - (Vector a, Vector b) { return Vector(a.x-b.x, a.y-b.y); }
Vector operator * (Vector a, double p) { return Vector(a.x*p, a.y*p); }
Vector operator / (Vector a, double p) { return Vector(a.x/p, a.y/p); }
bool operator < (const Point& a, const Point& b)
{
    return a.x < b.x || (a.x == b.x && a.y < b.y);
}
int dcmp(double x)
{
    if(fabs(x) < eps) return 0; return x < 0 ? -1 : 1;
}
bool operator == (const Point& a, const Point& b)
{
    return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0;
}

double Dot(Vector a, Vector b) { return a.x*b.x + a.y*b.y; }
double Length(Vector a) { return sqrt(Dot(a, a)); }
double Cross(Vector a, Vector b) { return a.x*b.y - a.y*b.x; }
double DistanceToSegment(Point p, Point a, Point b)
{
    if(a == b) return Length(p-a);
    Vector v1 = b-a, v2 = p-a, v3 = p-b;
    if(dcmp(Dot(v1, v2)) < 0) return Length(v2);
    else if(dcmp(Dot(v1, v3)) > 0) return Length(v3);
    else return fabs(Cross(v1, v2)) / Length(v1);
}
struct Circle
{
    Point c;
    double r;
    Circle(){}
    Circle(Point c, double r):c(c), r(r){}
    Point point(double a) //根据圆心角求点坐标
    {
        return Point(c.x+cos(a)*r, c.y+sin(a)*r);
    }
}o;

Point p, p1, p2, p3, p4, s;
double a, b, c, d;

double Calc(double x)
{
    p = o.point(x);
    double d1 = DistanceToSegment(p, p1, p2),
    d2 = DistanceToSegment(p, p2, p3),
    d3 = DistanceToSegment(p, p3, p4),
    d4 = DistanceToSegment(p, p4, p1);
    //点p到矩形最近距离加上s到p距离
    return min(min(d1, d2), min(d3, d4)) + Length(s-p);
}

double solve()
{
    double L, R, m, mm, mv, mmv;
    L = 0; R = PI;
    while (L + eps < R)
    {
        m = (L + R) / 2;
        mm = (m + R) / 2;
        mv = Calc(m);
        mmv = Calc(mm);
        if (mv <= mmv) R = mm; //三分法求最大值时改为mv>=mmv
        else L = m;
    }
    double ret = Calc(L);
    L = PI; R = 2*PI;
    while (L + eps < R)
    {
        m = (L + R) / 2;
        mm = (m + R) / 2;
        mv = Calc(m);
        mmv = Calc(mm);
        if (mv <= mmv) R = mm;
        else L = m;
    }
    return min(ret, Calc(L));
}

int main()
{
    while(scanf("%lf%lf", &s.x, &s.y))
    {
        if(s.x == 0 && s.y == 0) break;
        scanf("%lf%lf%lf", &o.c.x, &o.c.y, &o.r);
        scanf("%lf%lf%lf%lf", &a, &b, &c, &d);
        //确定矩形四个点坐标,左上点开始 逆时针
        double maxx, maxy, minx, miny;
        maxx = max(a, c); maxy = max(b, d);
        minx = min(a, c); miny = min(b, d);
        p1 = Point(minx, maxy);
        p2 = Point(minx, miny);
        p3 = Point(maxx, miny);
        p4 = Point(maxx, maxy);
        double ans = solve();
        printf("%.2f\n", ans);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/starseven/p/12001953.html

时间: 2024-08-02 20:03:08

关于三分的暴力技巧(暴力好)的相关文章

HDU 4509 湫湫系列故事——减肥记II(线段树-区间覆盖 或者 暴力技巧)

http://acm.hdu.edu.cn/showproblem.php?pid=4509 题目大意: 中文意义,应该能懂. 解题思路: 因为题目给的时间是一天24小时,而且还有分钟.为了解题方便,我们将小时换成分钟,那么一天24小时,总共有1440分钟.顾我就可以把一天里的任意HH:MM时间换成分钟.就这样一天的时间就变成[0,1440]区间了. 因为所给的活动最多是5*10^5,如果把活动的时间在线段[0,1440]都修改,那么时间的复杂度最坏是O(5*10^5*1440). (1)方法一

期末考试(正解:三分单峰函数 me:暴力水过)

好久没有水过杂题了! 今天lsc终于刚过了三道考试题来水杂题了! 期末考试 首先一看还是一脸mb(这是正常现象,毕竟我不像一些大神可以一眼出正解)然后我就被颓了标签,知道是三分单峰函数,但是自己实在是太弱了,所以并不会使用三分来水题,就只能使用暴力来做; 我们知道最后的恶心度(题里的不愉快度)是只和最后在那天出成绩有关,所以暴力枚举在哪天出成绩,然后使用前缀和就可以做到枚举1000000就可以出答案,其实就是取min就可以了! 如果A<B,那么显然使用第一种方法更优秀,而且我们可以O(1)算 然

Codeforces Round #486 (Div. 3)988E. Divisibility by 25技巧暴力||更暴力的分类

传送门 题意:给定一个数,可以对其做交换相邻两个数字的操作.问最少要操作几步,使得可以被25整除. 思路:问题可以转化为,要做几次交换,使得末尾两个数为00或25,50,75: 自己一开始就是先for一遍,记录四种可能对于的步数,再对四种可能讨论(有前导0的情况):自己是在数据中,该对了自己的代码, 看了队长和%王宣凯的代码,觉得那才是现场能ac的思路.--暴力交换: #include <iostream> #include <cstdio> #include <algori

Codeforces Round #312 (Div. 2)——C暴力技巧——Amr and Chemistry

Amr loves Chemistry, and specially doing experiments. He is preparing for a new interesting experiment. Amr has n different types of chemicals. Each chemical i has an initial volume of ai liters. For this experiment, Amr has to mix all the chemicals

FZU2077——暴力技巧——The tallest tree

lzs种了n棵树,每棵树每天长高一定的高度.某一天,lzs想知道树长得怎么样了,你能求出那一天最高的树有多高吗? Input 有多组测试数据,每组数据第一行输入两个整数n,m(1<=n,m<=100000),接下来n行,每行两个整数a,b(0<=a,b<=100000),表示第i棵树在第0天的高度以及每天生长的高度.接下来m行,每行一个整数x(0<=x<=100000),表示询问第x天最高的树有多高. Output 对于每个询问输出一行,为当天最高的树的高度. Samp

XDOJ_1079_暴力技巧

http://acm.xidian.edu.cn/problem.php?id=1079 枚举每一个区间不行,就枚举每一对存在的数,然后用最大子串的思路,中间加上判断枚举的数是否存在. #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> using namespace std; int a[100005],n,m,has[15]; int main() { int

Blizzard(暴力技巧+精度问题)

Blizzard Description 在魔兽争霸之冰封王座中有一个叫冰川的地图,里面有魔法传送阵,可以传送到这个此传送阵的对称传送阵位置.假设地图是圆形的,地图中心也就是圆心,在圆上每个点都有一个传送阵,可以传送到与地图圆心中心对称的位置.现在因为贪婪的大魔法师,人族正经历一场浩劫,作为聪明的兽族先知,信奉趁你病要你命的原则,想要趁机占领人族的土地.你能计算最短距离能够使兽族大军尽快到达人族营地? Input 第一行一个整数r(1 <= r <= 1000)代表地图的半径.接下来两行两个整

【USACO】Prime Palindromes(暴力暴力再暴力)

把所有符合条件的数全部记下来,扫一遍就行了,这类数最多2W来个 /* ID: 18906421 LANG: C++ PROG: pprime */ #include<cmath> #include<cstdio> #include<vector> #include<algorithm> using namespace std; const int maxn = 20005; int a,b; int cnt = 0; int prime[maxn]; boo

浅谈共享软件如何不被暴力蹂躏

本文来自泉贸软件工作室 转载请注明 http://www.qmboy.com 共享软件是目前世界上软件业比较热门的话题,国内更是如此.成千上万的程序员以极大的热情投入到这个领域来,都憧憬着用辛勤的劳动获得丰厚的回报:但,实际并非如此,绝大多数的人都铩羽而归.值得注意的是:除了软件设计和技术上的原因外,最大的原因就是共享件被破解(Crack)了-- 面对破解 一个做共享件的作者,面对的是已经形成团伙的众多破解高手,国内的什么CCG.BCG,国外的eGis.King.Core.TNT.DAMN和TM