Codeforces 437E The Child and Polygon (interval DP)

Source: Internet
Author: User

Link: Codeforces 437E The Child and Polygon

Question: Give a polygon and ask how many kinds of segmentation methods are available. Divide the Polygon into multiple triangles.

Solution: first, we need to understand the nature of the Cross Product of the vector. At the beginning, we need to convert the given point to clockwise, and then use the interval dp for calculation. Dp [I] [j] indicates that a dp [I] [j] cutting method can be provided from point I to Point j. Then, determine whether I and j can be used as the cutting line. If k is selected in I and j, the point k must be in the clockwise direction of I and j.

    #include using namespace std;typedef long long ll;const int N = 205;const ll MOD = 1e9+7;struct point {    ll x, y;    ll operator * (const point& c) const {        return x * c.y - y * c.x;    }    point operator - (const point& c) const {        point u;        u.x = x - c.x;        u.y = y - c.y;        return u;    }}p[N];int n;ll dp[N][N];void init () {    scanf("%d", &n);    memset(dp, -1, sizeof(dp));    for (int i = 0; i < n; i++)        scanf("%lld %lld", &p[i].x, &p[i].y);    ll tmp = 0;    p[n] = p[0];    for (int i = 0; i < n; i++)        tmp += p[i] * p[i+1];    if (tmp < 0)        reverse(p, p + n);}ll solve (int l, int r) {    if (dp[l][r] != -1)        return dp[l][r];    ll& ans = dp[l][r];    ans = 0;    if (r - l == 1)        return ans = 1;    for (int i = l + 1; i < r; i++) {        if ((p[l] - p[r]) * (p[i] - p[r]) > 0)            ans = (ans + solve(l, i) * solve(i, r)) % MOD;    }    return ans;}int main () {    init();    printf("%lld\n", solve(0, n-1));    return 0;}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.