Codeforces-1019D:Large Triangle(思维+二分)

D. Large Triangle
time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output

There is a strange peculiarity: if you connect the cities of Rostov, Taganrog and Shakhty, peculiarly, you get a triangle
«Unbelievable But True»
Students from many different parts of Russia and abroad come to Summer Informatics School. You marked the hometowns of the SIS participants on a map.

Now you decided to prepare an interesting infographic based on this map. The first thing you chose to do is to find three cities on this map, such that they form a triangle with area S
.

Input
The first line of input contains two integers n n and S S (3n2000,1S21018) ( 3 ≤ n ≤ 2000 , 1 ≤ S ≤ 2 ⋅ 10 18 ) — the number of cities on the map and the area of the triangle to be found.

The next n n lines contain descriptions of the cities, one per line. Each city is described by its integer coordinates xi,yi(109xi,yi109) x i , y i ( − 10 9 ≤ x i , y i ≤ 10 9 ) .

It is guaranteed that all cities are located at distinct points. It is also guaranteed that no three cities lie on the same line.

Output
If the solution doesn’t exist — print «No».

Otherwise, print «Yes», followed by three pairs of coordinates (x,y) ( x , y ) — the locations of the three cities, which form the triangle of area S.

Examples
input
3 7
0 0
3 0
0 4
output
No
input
4 3
0 0
2 0
1 2
1 3
output
Yes
0 0
1 3
2 0

思路:BZOJ 3707 题目的变形,弄懂BZOJ那个题目后,CF这个题在那之上加个二分也就做出来了。

我说说我对于BZOJ 那个题的理解。

做法是先求出任意2点间的线段,按斜率从小到大排序。
并且把原序列的点按x坐标上升排序。

这时候对于第一条线段 pipj p i p j (即斜率最小的那条线段)来说,没有任何点位于它覆盖的区间内,不然这条线段一定不是斜率最小的(可以画图理解下)。

同时因为点经过排序,那么第一条线段的2个端点在序列中是相邻的,即 j=i+1 j = i + 1

那么点 pi1 p i − 1 和点 pj+1 p j + 1 一定是到这条线段的垂直距离最短的2个点。

为什么呢?

假设下标为点 pi2 p i − 2 距离这条线段的垂直距离最短。
因为 xi2<xi1 x i − 2 < x i − 1 ,同时点 pi2 p i − 2 距离这条线段更“近”,那么线段 pi2pi1 p i − 2 p i − 1 的斜率一定比线段 pipi+1 p i p i + 1 的小(建议画图理解),那么与之前说的 pipi+1 p i p i + 1 斜率最小不成立。

同理点 pj p j .

然后,我们按照斜率枚举线段时,各点到线段的垂直距离会改变,但改变的只是当前枚举的这条线段的2个端点,其它的点不会变(这里我也有点难理解)。那么我们每次只需改变这2个端点的位置即可。

#include
using namespace std;
const int MAX=2e3+10;
const int MOD=1e9+7;
typedef long long ll;
struct Point{ll x,y;}p[MAX];
int cmp(const Point& A,const Point& B){return A.xx;}
Point operator-(Point A,Point B){return (Point){A.x-B.x,A.y-B.y};}        //向量A-B
ll operator^(Point A,Point B){return A.x*B.y-A.y*B.x;}            //向量A B的叉积
ll cal(int x,int y,int z){return abs((p[y]-p[x])^(p[z]-p[x]));}
struct lenka
{
    int x,y;
    double k;
};
vectorq;
int cmp1(const lenka& A,const lenka& B){return A.kint pos[MAX],id[MAX];
int main()
{
    int n;
    cin>>n;
    ll S;
    cin>>S;
    S*=2;
    for(int i=1;i<=n;i++)scanf("%lld%lld",&p[i].x,&p[i].y);
    sort(p+1,p+n+1,cmp);
    for(int i=1;i<=n;i++)pos[i]=id[i]=i;
    for(int i=1;i<=n;i++)
    {
        for(int j=i+1;j<=n;j++)
        {
            double k;
            if(p[i].x==p[j].x)k=1e18;
            else k=1.0*(p[j].y-p[i].y)/(p[j].x-p[i].x);
            q.push_back((lenka){pos[i],pos[j],k});
        }
    }
    sort(q.begin(),q.end(),cmp1);
    for(int i=0;i<q.size();i++)
    {
        int x=pos[q[i].x],y=pos[q[i].y];
        if(x>y)swap(x,y);
        int l=1,r=x-1;
        while(r>=l)
        {
            int m=(l+r)/2;
            if(cal(id[m],id[x],id[y])==S)
            {
                puts("Yes");
                printf("%lld %lld\n",p[id[m]].x,p[id[m]].y);
                printf("%lld %lld\n",p[id[x]].x,p[id[x]].y);
                printf("%lld %lld\n",p[id[y]].x,p[id[y]].y);
                return 0;
            }
            if(cal(id[m],id[x],id[y])>S)l=m+1;
            else r=m-1;
        }
        l=y+1,r=n;
        while(r>=l)
        {
            int m=(l+r)/2;
            if(cal(id[m],id[x],id[y])==S)
            {
                puts("Yes");
                printf("%lld %lld\n",p[id[m]].x,p[id[m]].y);
                printf("%lld %lld\n",p[id[x]].x,p[id[x]].y);
                printf("%lld %lld\n",p[id[y]].x,p[id[y]].y);
                return 0;
            }
            if(cal(id[m],id[x],id[y])>S)r=m-1;
            else l=m+1;
        }
        swap(pos[q[i].x],pos[q[i].y]);//交换位置
        swap(id[x],id[y]);
    }
    puts("No");
    return 0;
}

你可能感兴趣的:(计算几何)