| # | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 | 
|---|---|---|---|---|---|---|---|
| 1127073 | Requiem | Fence (CEOI08_fence) | C++20 | 0 ms | 0 KiB | 
#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
#define MOD 1000000007
#define inf 1e18
#define fi first
#define se second
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORD(i,a,b) for(int i=a;i>=b;i--)
#define sz(a) ((int)(a).size())
#define endl '\n'
#define pi 3.14159265359
#define TASKNAME "baoxoai"
using namespace std;
template<typename T> bool maximize(T &res, const T &val) { if (res < val){ res = val; return true; }; return false; }
template<typename T> bool minimize(T &res, const T &val) { if (res > val){ res = val; return true; }; return false; }
typedef pair<int,int> ii;
typedef pair<int,ii> iii;
typedef vector<int> vi;
/**
Chi phí = (số cọc phải đóng) + số cây xoài ko đc bảo vệ
1 cây xoài: 111
1 cộc: 20
Gọi dp[l][r]: chi phí nhỏ nhất trong đoạn [l, r].
Mỗi lần mình bỏ 1 cây xoài thì mình sẽ mất q.
Tức là mình cần làm gì?
Với 1 bộ ba điểm trên bao lồi thì mình sẽ muốn biết số điểm nằm trong bao lồi là bao nhiêu.
Giả sử mình quyết định đặt cây cọc ở vị trí u.
dp[u]: xét đến vị trí u, chi phí nhỏ nhất của mình khi xét đến u là bao nhiêu.
dp[u] = dp[v] + cost(u, v).
dp1[l][r]: số cây xoài nằm trong cái khúc từ l đến r.
dp1[l][r] = dp1[l][r - 1] + calc(r - 1, r, l)
**/
const int MAXN = 2e2 + 9;
const int costFence = 20;
const int costTree = 111;
int n, m;
struct point{
    int x, y;
    point(int _x = 0, int _y = 0): x(_x), y(_y) {}
    point operator + (const point &other){
        return point(x + other.x, y + other.y);
    }
    point operator - (const point &other){
        return point(x - other.x, y - other.y);
    }
    bool operator < (const point &other) const {
        return (x == other.x) ? (y < other.y) : (x < other.x);
    }
};
int ccw(point a, point b, point c){
    b = b - a;
    c = c - a;
    int val = b.x * c.y - c.x * b.y;
    if (val < 0) return -1;
    if (val > 0) return 1;
    if (val == 0) return 0;
}
vector<point> holes;
vector<point> tree;
vector<point> CH;
vector<point> buildConvexHull(vector<point> polygon){
    sort(polygon.begin(), polygon.end());
    if (polygon.size() == 2) return polygon;
    point p0 = polygon[0];
    point p1 = polygon.back();
    vector<point> upper;
    vector<point> lower;
    vector<point> convexhull;
    upper.pb(p0);
    lower.pb(p0);
    FOR(i, 1, (int) polygon.size() - 1){
        if (i == polygon.size() - 1 or ccw(p0, polygon[i], p1) < 0){
            while(upper.size() > 1 and ccw(upper[upper.size() - 2], upper.back(), polygon[i]) >= 0) upper.pop_back();
            upper.pb(polygon[i]);
        }
        if (i == polygon.size() - 1 or ccw(p0, polygon[i], p1) > 0){
            while(lower.size() > 1 and ccw(lower[lower.size() - 2], lower.back(), polygon[i]) <= 0) lower.pop_back();
            lower.pb(polygon[i]);
        }
    }
    FOR(i, 0, (int) upper.size() - 1){
        convexhull.pb(upper[i]);
    }
    FORD(i, (int) lower.size() - 2, 1){
        convexhull.pb(lower[i]);
    }
    return convexhull;
}
namespace brute{
    bool check(){
        return n <= 20;
    }
    vector<point> currentPolygon;
    int ans = inf;
    bool insidePolygon(vector<point> &p, point pt){
        if (p.size() <= 1){
            return false;
        }
        if (ccw(p[0], p[1], pt) == 0) return true;
        FOR(i, 1, (int) p.size() - 1){
            point p1 = p[i];
            point p2 = p[(i + 1) % p.size()];
            if (ccw(p1, p2, pt) == 0) return true;
            if (ccw(p1, p2, pt) != ccw(p[0], p[1], pt)) return false;
        }
        return true;
    }
    int calc(){
        int res = currentPolygon.size() * costFence;
        vector<point> subPolygon = buildConvexHull(currentPolygon);
        FOR(i, 0, m - 1){
            if (!insidePolygon(pt, tree[i])) {
                res += costTree;
            }
        }
        return res;
    }
    void backtrack(int pos){
        if (pos == holes.size()) {
            minimize(ans, calc());
            return;
        }
        currentPolygon.pb(holes[pos]);
        backtrack(pos + 1);
        currentPolygon.pop_back();
        backtrack(pos + 1);
    }
    void solve(){
        backtrack(0);
        cout << ans << endl;
    }
}
namespace subtask1{
    bool check(){
        return true;
    }
    int dp[MAXN], cntTri[MAXN][MAXN][MAXN], cost[MAXN][MAXN];
    inline bool inside(point p1, point p2, point p3, point s){
        int dir1 = ccw(p1, p2, s);
        int dir2 = ccw(p2, p3, s);
        int dir3 = ccw(p3, p1, s);
        if (dir1 > 0 and dir2 > 0 and dir3 > 0) return true;
        if (dir1 < 0 and dir2 < 0 and dir3 < 0) return true;
        return false;
    }
    bool insidePolygon(vector<point> p, point pt){
        if (p.size() == 1){
            return false;
        }
        if (ccw(p[0], p[1], pt) == 0) return true;
        FOR(i, 1, (int) p.size() - 1){
            point p1 = p[i];
            point p2 = p[(i + 1) % p.size()];
            if (ccw(p1, p2, pt) == 0) return true;
            if (ccw(p1, p2, pt) != ccw(p[0], p[1], pt)) return false;
        }
        return true;
    }
    int refine(int x){
        return (x + n) % n;
    }
    void solve(){
        n = CH.size();
        if (n <= 2){
            cout << m * costTree << endl;
            return;
        }
        int ans = m * costTree; //khong lam gi ca
        FOR(i, 0, (int) n - 1){
            FOR(j, i + 1, (int) n - 1){
                FOR(k, j + 1, (int) n - 1){
                    FOR(t, 0, (int) tree.size() - 1){
                        cntTri[i][j][k] += inside(CH[i], CH[j], CH[k], tree[t]);
                    }
                    int val = cntTri[i][j][k];
                    cntTri[i][k][j] = val;
                    cntTri[j][i][k] = val;
                    cntTri[j][k][i] = val;
                    cntTri[k][i][j] = val;
                    cntTri[k][j][i] = val;
                }
            }
        }
        int constantBonus = 0;
        FOR(i, 0, (int) m - 1){
            if (!insidePolygon(CH, tree[i])) constantBonus++;
        }
        for(int len = 3; len <= n; len++){
            for(int l = 0, r = l + len - 1; l < n;l++, r++){
                cost[refine(l)][refine(r)] = cost[refine(l)][refine(r - 1)] + cntTri[l][refine(r - 1)][refine(r)];
            }
        }
        FOR(u, 0, n - 1){
            FOR(j, 0, 2 * n) dp[j] = inf;
            dp[u] = 0;
            FOR(j, u + 1, u + n){
                FOR(k, max(j - n + 1, u), j - 1){
                    minimize(dp[j], dp[k] + costFence + cost[refine(k)][refine(j)] * costTree);
                }
            }
            minimize(ans, dp[u + n] + constantBonus * costTree);
        }
        cout << ans << endl;
    }
}
main()
{
    fast;
    if (fopen(TASKNAME".inp","r")){
        freopen(TASKNAME".inp","r",stdin);
        freopen(TASKNAME".out","w",stdout);
    }
    cin >> n >> m;
    FOR(i, 1, n){
        int x, y;
        cin >> x >> y;
        holes.pb(point(x, y));
    }
    FOR(i, 1, m){
        int x, y;
        cin >> x >> y;
        tree.pb(point(x, y));
    }
    CH = buildConvexHull(holes);;
//    cerr << CH.size() << endl;
    if (subtask1::check()) return subtask1::solve(), 0;
}
/**
Warning:
Đọc sai đề???
Cận lmao
Code imple thiếu case nào không.
Limit.
4 3
800 300
200 200
200 700
600 700
400 300
600 500
800 900
**/
