Sha 1425-metal (recurrence)

Source: Internet
Author: User
Ultraviolet A 1425-metal

Question Link

Given a metal sheet, there are some points on it. Now there is a cutting machine that needs to cut out a monotonic quadrilateral consisting of all points and ask how many situations there are.

Idea: recursive, set DP [I] [J], I as the top vertex, and J as the bottom vertex. Now add another vertex K to come in, therefore, the original DP [I] [J] must have one dimension of k-1, and the enumeration of the other one is all. In the process of adding more points, you should also consider whether or not you can add them. Write a judgment function and use the vector cross product to judge the enumeration of all vertices in the connection line. If the above line is used, it cannot be a bit above. If it is a line below, it cannot be a bit below.

Code:

#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;const int N = 55;int t, n;struct Point {    double x, y;    void read() {scanf("%lf%lf", &x, &y);    }} p[N];long long dp[N][N];bool cmp(Point a, Point b) {    return a.x < b.x;}double xmul(Point a1, Point a2, Point b1, Point b2) {    double ax = a2.x - a1.x, ay = a2.y - a1.y;    double bx = b2.x - b1.x, by = b2.y - b1.y;    return ax * by - bx * ay;}bool judge(int s, int e, double flag) {    for (int i = s + 1; i < e; i++) {double ans = xmul(p[s], p[i], p[s], p[e]) * flag;if (ans <= 0) return false;    }    return true;}long long solve() {    memset(dp, 0, sizeof(dp));    dp[1][0] = dp[0][1] = 1;    for (int i = 2; i < n; i++) {for (int j = 0; j < i - 1; j++) {    int k = i - 1;    dp[i][j] += dp[k][j];    dp[j][i] += dp[j][k];}for (int j = 0; j < i - 1; j++) {    if (judge(j, i, 1))dp[i][i - 1] += dp[j][i - 1];    if (judge(j, i, -1))dp[i - 1][i] += dp[i - 1][j];}    }    long long ans = 0;    for (int i = 0; i < n - 1; i++) {if (judge(i, n - 1, 1))    ans += dp[i][n - 1];if (judge(i, n - 1, -1))    ans += dp[n - 1][i];    }    return ans;}int main() {    scanf("%d", &t);    while (t--) {scanf("%d", &n);for (int i = 0; i < n; i++)    p[i].read();sort(p, p + n, cmp);printf("%lld\n", solve() / 2);    }    return 0;}


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: info-contact@alibabacloud.com 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.