hihoCoder 1582 Territorial Dispute

针对平面点集的染色问题,提出了一种有效算法。该算法通过构建凸包并采用特定染色策略来解决无法用直线完全分开两类点的问题。适用于竞赛及实际应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem

hihocoder.com/problemset/problem/1582
vjudge.net/problem/HihoCoder-1582

Reference

hihocoder 1582 : Territorial Dispute (计算几何)(2017 北京网络赛E)

Meaning

给出 n 个平面上的点,要把它们染成两种颜色 A 和 B,使得无法找到一条直线可以把所有的 A 和 B 完全的分开,求是否存在分配方案,如果有,输出任意一种分配。

Analysis

大概思路就是求个凸包,把凸包上的点间隔着 ABAB 地染色,凸包里面的点随便染。或者凸包一种色,凸包里的点另一种色。
如果所有点共线,且多于两个点,也是可以得。所有点共线时凸包里只有两个点(假的凸包?)。
如果只有 3 个点,且不共线,则是不行的。
综上,可以总结出一种策略:
先求凸包,如果凸包上的点 m < n(包含所有点共线的情况),则包上染 A,包里染 B;
否则,就是点全在凸包上(可能只有一个点),只要点数大于 3,就可以间隔着染色;
否则,就不在可行方案。

Code

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 100;

struct pnt
{
    int x, y, id;

    pnt(int _x = 0, int _y = 0) :
        x(_x), y(_y) {}

    bool operator < (const pnt &rhs) const
    {
        return x != rhs.x ? x < rhs.x : y < rhs.y;
    }

    pnt operator - (const pnt &rhs) const
    {
        return pnt(x - rhs.x, y - rhs.y);
    }

    int operator ^ (const pnt &rhs) const
    {
        return x * rhs.y - rhs.x * y;
    }
} p[N], ch[N<<1];

int Graham(int n)
{
    sort(p, p + n);
    int k = 0;
    for(int i = 0; i < n; ++i)
    {
        while(k > 1 && ((ch[k-1] - ch[k-2]) ^ (p[i] - ch[k-1])) <= 0)
            --k;
        ch[k++] = p[i];
    }
    for(int i = n - 2, m = k; i >= 0; --i)
    {
        while(k > m && ((ch[k-1] - ch[k-2]) ^ (p[i] - ch[k-1])) <= 0)
            --k;
        ch[k++] = p[i];
    }
    // 注意 k = 1 的情况
    return k > 1 ? k - 1 : k;
}

char ans[N+1];

int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        int n;
        scanf("%d", &n);
        for(int i = 0; i < n; ++i)
        {
            scanf("%d%d", &p[i].x, &p[i].y);
            p[i].id = i;
        }

        int m = Graham(n);
        // 先全部染成 A
        memset(ans, 'A', sizeof ans);
        ans[n] = '\0';

        if(m < n)
        {
            // 凸包上染 B
            for(int i = 0; i < m; ++i)
                ans[ch[i].id] = 'B';
            printf("YES\n%s\n", ans);
        }
        else if(m > 3)
        {
            // 间隔着染 B(有两个就行)
            ans[ch[0].id] = ans[ch[2].id] = 'B';
            printf("YES\n%s\n", ans);
        }
        else
            puts("NO");
    }
    return 0;
}
breed[runners runner] runners-own [ vx ;; x velocity vy ;; y velocity desired-direction ;; my desired direction driving-forcex ;; my main motivating force driving-forcey obstacle-forcex ;; force exerted by obstacles obstacle-forcey territorial-forcex;; force exerted by neighbors territorial-forcey speed tense? ] ;; Set up the view to setup ca set-default-shape runners "person" ;; create patrons create-runners number [ setxy ((random-float 16) - 8) ((random-float 6) - 14) set size 1.5 let init-direction -90 + random 180 set vx sin init-direction set vy cos init-direction set speed 0 set tense? false ] ;; set boundary patches as walls ask patches with [pxcor = min-pxcor or pxcor = max-pxcor or pycor = min-pycor ] [ set pcolor gray ] ask patches with [pycor = max-pycor and abs pxcor >= max-pxcor - 5 ] [set pcolor gray] ;; create exit ask patches with [pycor = max-pycor and abs pxcor < max-pxcor - 5 ] [set pcolor green] ;;create gate ask patches with[3 < pycor and pycor < 7 and pxcor = 1] [set pcolor gray] ask patches with[3 < pycor and pycor < 7 and pxcor = 3] [set pcolor gray] ask patches with[3 < pycor and pycor < 7 and pxcor = 5] [set pcolor gray] ask patches with[3 < pycor and pycor < 7 and pxcor = 7] [set pcolor gray] ask patches with[3 < pycor and pycor < 7 and pxcor = 9] [set pcolor gray] ask patches with[3 < pycor and pycor < 7 and pxcor = -1] [set pcolor gray] ask patches with[3 < pycor and pycor < 7 and pxcor = -3] [set pcolor gray] ask patches with[3 < pycor and pycor < 7 and pxcor = -5] [set pcolor gray] ask patches with[3 < pycor and pycor < 7 and pxcor = -7] [set pcolor gray] ask patches with[3 < pycor and pycor < 7 and pxcor = -9] [set pcolor gray] reset-ticks end ;; run the simulation to go ;; run the social forces model ;; calculate the forces first... ask runners with[tense? = true] [ calc-desired-direction calc-driving-force calc-obstacle-force if any? other runners [ calc-territorial-forces ] ] ;; then move the runners and have them grow impatient if need be ask runners with [tense? = true] [ move-turtle ] ask runners with [tense? = false][ let w 1 / poisson if random-float 3 < poisson[ ask runners with[tense? = false] [set tense? true] ]] ;; control the service rate of bartenders. follow an exponential distribution for service times let p 1 / mean-service-time if random-float 1 < p [ ask patches with[pcolor = green] [ service-patron ] ] if not any? runners [ stop ] tick end ;; through the gate to service-patron if any? runners in-radius 2.5 [ ;; default to "random" service-plan let next-served one-of runners in-radius 2.5 if service-plan = "waited-longest" [ set next-served max-one-of runners in-radius 2.5 [ ticks ]] ask next-served [ die ] ] end ;; helper function to find the magnitude of a vector to-report magnitude [x y] report sqrt ((x ^ 2) + (y ^ 2)) end ;; returns 1 if the angle between the desired vector and the force vector is within a threshold, else return c to-report field-of-view-modifier [desiredx desiredy forcex forcey] ifelse (desiredx * (- forcex) + desiredy * (- forcey)) >= (magnitude forcex forcey) * cos (field-of-view / 2) [ report 1 ] [ report c] end ;;;; Functions for calculating the social forces ;;;; ;; move the turtle according to the rules of the social forces model to move-turtle let ax driving-forcex + obstacle-forcex + territorial-forcex let ay driving-forcey + obstacle-forcey + territorial-forcey set vx vx + ax set vy vy + ay set speed magnitude vx vy ;; scale down the velocity if it is too high let vmag magnitude vx vy let multiplier 1 if vmag > max-speed [set multiplier max-speed / vmag] set vx vx * multiplier set vy vy * multiplier set xcor xcor + vx set ycor ycor + vy end ;; find the territorial force according to the social forces model to calc-territorial-forces set territorial-forcex 0 set territorial-forcey 0 ask other runners with [distance myself > 0] [ let to-agent (towards myself) - 180 let rabx [xcor] of myself - xcor let raby [ycor] of myself - ycor set speed magnitude vx vy let to-root ((magnitude rabx raby) + (magnitude (rabx - (speed * sin desired-direction)) (raby - (speed * cos desired-direction)))) ^ 2 - speed ^ 2 if to-root < 0 [set to-root 0] let b 0.5 * sqrt to-root let agent-force (- n) * exp (- b / sigma) ask myself [ let agent-forcex agent-force * (sin to-agent) let agent-forcey agent-force * (cos to-agent) ;; modify the effect this force has based on whether or not it is in the field of view let vision field-of-view-modifier driving-forcex driving-forcey agent-forcex agent-forcey set territorial-forcex territorial-forcex + agent-forcex * vision set territorial-forcey territorial-forcey + agent-forcey * vision ] ] end ;; find the obstacle force of the turtle according to the social forces model to calc-obstacle-force set obstacle-forcex 0 set obstacle-forcey 0 ask patches with [pcolor = gray] [ let to-obstacle (towards myself) - 180 let obstacle-force (- u0) * exp (- (distance myself) / λ) ask myself [ set obstacle-forcex obstacle-forcex + obstacle-force * (sin to-obstacle) set obstacle-forcey obstacle-forcey + obstacle-force * (cos to-obstacle) ] ] end ;; find the driving force of the turtle to calc-driving-force set driving-forcex (1 / tau) * (max-speed * (sin desired-direction) - vx);; * pushiness set driving-forcey (1 / tau) * (max-speed * (cos desired-direction) - vy) ;;* pushiness end ;; find the heading towards the nearest goal to calc-desired-direction let goal min-one-of (patches with [pcolor = green]) [ distance myself ] set desired-direction towards goal end
06-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值