CodeForces 682E Alyona and Triangles【最大三角形 旋转卡壳or迭代】

题目大意:

给出n个点,任意三个点组成的三角形面积不超过S,S大于n个点可以组成三角形的最大面积;
构造一个大三角形覆盖上述所有n个点,并且面积不超过4S;

解题思路:

不得不说思路很巧妙,不过题目中的S取值也相当于给了个暗示。

结论:

先求出最大三角形 ABC A B C ,再以abc分别作为三边中点所得三角形 ABC A ′ B ′ C ′ 即是所求三角形。如图:
这里写图片描述

证明:

首先易得 SΔABC=4SΔABC4S S Δ A ′ B ′ C ′ = 4 S Δ A B C ≤ 4 S

再证明所有点都会被 ΔABC Δ A ′ B ′ C ′ 包含,考虑反证法。
假设有一个点 D D 不在ΔABC中,比如在 AB A ′ B ′ 右侧,那么有
dis(D,AB)>dis(C,AB) d i s ( D , A B ) > d i s ( C , A B )
所以 SΔABC<SΔABD S Δ A B C < S Δ A B D ,这与 ΔABC Δ A B C 是最大三角形矛盾,所以不成立。
得证。

求最大三角形的方法有两种,第一种是旋转卡壳,第二种是迭代,复杂度都是 O(n2) O ( n 2 ) ,不过不知道迭代复杂度是怎么证明的。

AC代码:

旋转卡壳版:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int N=5005;
struct point
{
    int x,y;
    point(){}
    point(int _x,int _y):x(_x),y(_y){}
    inline friend point operator - (const point &a,const point &b)
    {return point(a.x-b.x,a.y-b.y);}
    inline friend point operator + (const point &a,const point &b)
    {return point(a.x+b.x,a.y+b.y);}
    inline friend ll operator * (const point &a,const point &b)
    {return 1ll*a.x*b.y-1ll*a.y*b.x;}
    inline ll dis(){return 1ll*x*x+1ll*y*y;}
}p[N],A,B,C;
int n,top;
ll S;

inline bool cmp(const point &a,const point &b)
{
    ll res=(a-p[1])*(b-p[1]);
    if(res)return res>0;
    return (a-p[1]).dis()<(b-p[1]).dis();
}

inline ll area(int i,int j,int k)
{
    return abs((p[j]-p[i])*(p[k]-p[i]));
}

void graham()
{
    for(int i=2;i<=n;i++)
        if((p[i].x<p[1].x)||(p[i].x==p[1].x&&p[i].y<p[1].y))swap(p[1],p[i]);
    sort(p+2,p+n+1,cmp),top=1;
    for(int i=2;i<=n;i++)
    {
        while(top>1&&(p[top-1]-p[i])*(p[top]-p[i])<0)top--;
        p[++top]=p[i];
    }n=top;
}

int main()
{
    //freopen("lx.in","r",stdin);
    scanf("%d%lld",&n,&S);
    for(int i=1;i<=n;i++)scanf("%d%d",&p[i].x,&p[i].y);
    graham();
    S=0;point a,b,c;
    for(int i=1;i<=n-2;i++)
        for(int j=i+1,k=j+1;j<=n-1;j++)
        {
            while(k<=n&&area(i,j,k+1)>=area(i,j,k))k++;
            if(area(i,j,k)>S)S=area(i,j,k),a=p[i],b=p[j],c=p[k];
        }
    A=a+(b-c),B=b+(c-a),C=c+(a-b);
    printf("%d %d\n%d %d\n%d %d\n",A.x,A.y,B.x,B.y,C.x,C.y);
    return 0;
}

迭代版:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int N=5005;
struct point
{
    int x,y;
    point(){}
    point(int _x,int _y):x(_x),y(_y){}
    inline friend point operator - (const point &a,const point &b)
    {return point(a.x-b.x,a.y-b.y);}
    inline friend point operator + (const point &a,const point &b)
    {return point(a.x+b.x,a.y+b.y);}
    inline friend ll operator * (const point &a,const point &b)
    {return 1ll*a.x*b.y-1ll*a.y*b.x;}
}p[N],A,B,C;
int n,top;
ll S;

inline ll area(int i,int j,int k)
{
    return abs((p[j]-p[i])*(p[k]-p[i]));
}

int main()
{
    //freopen("lx.in","r",stdin);
    scanf("%d%lld",&n,&S);
    for(int i=1;i<=n;i++)scanf("%d%d",&p[i].x,&p[i].y);
    int a=1,b=2,c=3;bool bz=1;
    S=area(a,b,c);
    while(bz)
    {
        bz=0;
        for(int i=1;i<=n;i++)
        {
            ll tmp;
            tmp=area(a,b,i);
            if(tmp>S)S=tmp,c=i,bz=1;
            tmp=area(a,i,c);
            if(tmp>S)S=tmp,b=i,bz=1;
            tmp=area(i,b,c);
            if(tmp>S)S=tmp,a=i,bz=1;
        }
    }
    A=p[a]+(p[b]-p[c]),B=p[b]+(p[c]-p[a]),C=p[c]+(p[a]-p[b]);
    printf("%d %d\n%d %d\n%d %d\n",A.x,A.y,B.x,B.y,C.x,C.y);
    return 0;
}
### Codeforces 887E Problem Solution and Discussion The problem **887E - The Great Game** on Codeforces involves a strategic game between two players who take turns to perform operations under specific rules. To tackle this challenge effectively, understanding both dynamic programming (DP) techniques and bitwise manipulation is crucial. #### Dynamic Programming Approach One effective method to approach this problem utilizes DP with memoization. By defining `dp[i][j]` as the optimal result when starting from state `(i,j)` where `i` represents current position and `j` indicates some status flag related to previous moves: ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = ...; // Define based on constraints int dp[MAXN][2]; // Function to calculate minimum steps using top-down DP int minSteps(int pos, bool prevMoveType) { if (pos >= N) return 0; if (dp[pos][prevMoveType] != -1) return dp[pos][prevMoveType]; int res = INT_MAX; // Try all possible next positions and update 'res' for (...) { /* Logic here */ } dp[pos][prevMoveType] = res; return res; } ``` This code snippet outlines how one might structure a solution involving recursive calls combined with caching results through an array named `dp`. #### Bitwise Operations Insight Another critical aspect lies within efficiently handling large integers via bitwise operators instead of arithmetic ones whenever applicable. This optimization can significantly reduce computation time especially given tight limits often found in competitive coding challenges like those hosted by platforms such as Codeforces[^1]. For detailed discussions about similar problems or more insights into solving strategies specifically tailored towards contest preparation, visiting forums dedicated to algorithmic contests would be beneficial. Websites associated directly with Codeforces offer rich resources including editorials written after each round which provide comprehensive explanations alongside alternative approaches taken by successful contestants during live events. --related questions-- 1. What are common pitfalls encountered while implementing dynamic programming solutions? 2. How does bit manipulation improve performance in algorithms dealing with integer values? 3. Can you recommend any online communities focused on discussing competitive programming tactics? 4. Are there particular patterns that frequently appear across different levels of difficulty within Codeforces contests?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值