[Educational Codeforces Round 99 (Rated for Div. 2)]-E. Four Points(枚举,贪心,思维)

[Educational Codeforces Round 99 (Rated for Div. 2)]-E. Four Points(枚举,贪心,思维)

题面:

题意:

每组数据给定四个二维平面上的点,每一次操作:你可以选择一个点将其移动到上下左右四个方向中的一个点。

现在问你最少多少次操作,可以将这四个点移动构成正方形。

思路:

我们(4!)次枚举,可以枚举出这四点分别在正方形的左下,右下,左上,右上的所有方案。

我们对每一种情况求出最小的操作数使其构成正方形,所有方案操作数的最小值就是答案。

移动为正方形的最小操作数(cost)求法如下:

我们不妨设正方形左下,右下,左上,右上的点分别为(p_1,p_2,p_3,p_4)

我们不妨将横坐标和纵坐标对操作数的贡献分开单独考虑,即(cost=cost_x+cost_y)

(x_1=min(p_1.x,p_3.x),x_2=max(p_1.x,p_3.x),x_3=min(p_2.x,p_4.x),x_4=max(p_2.x,p_4.x).)

那么对于正方形左边的横坐标,一定是在区间([x_1,x_2])最优,贡献均为(x_2-x_1)。右边的横坐标,一定是在区间([x_3,x_4])最优,贡献均为(x_4-x_3)

此时正方形的边长区间([x_3-x_2,x_4-x_1])

注意特例:(x_4<x_1) 时,正方形的左右边的横坐标一定在([x_4,x_1])最优,此时正方形的边长为0。

上述中的最优取法源于一个经典问题:给定序列求最少总变化量使所有数相等,结论是取中位数(若序列长度为偶数,可以取中部区间中的任意一值。).

这样我们可以得出(cost_x),以及横坐标问题中的正方形边长区间([x_l,x_r])

同理可以得出(cost_y),以及横坐标问题中的正方形边长区间([y_l, y_r])

如果题目让求一个矩形的话,就已经做完了,答案就是(cost_x+cost_y)

不过本题要求必须是正方形,因此得让两边的长度相同。

我们发现若区间([x_l,x_r])与区间([y_l, y_r])有交集,我们可以在两个维度上可以适当调节使得正方形边长相等,且成本不变。

若无交集,区间的间隔为(val)时,我们需要多花(2*val)使两边的长度相同。

至此就可以解决本题了。

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <bits/stdc++.h>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define chu(x)  if(DEBUG_Switch) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) { if (a == 0ll) {return 0ll;} a %= MOD; ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
ll poww(ll a, ll b) { if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a ;} a = a * a ; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("
");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("
");}}}
inline long long readll() {long long tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') { fh = -1; } c = getchar();} while (c >= '0' && c <= '9') { tmp = tmp * 10 + c - 48, c = getchar(); } return tmp * fh;}
inline int readint() {int tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') { fh = -1; } c = getchar();} while (c >= '0' && c <= '9') { tmp = tmp * 10 + c - 48, c = getchar(); } return tmp * fh;}
void pvarr_int(int *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%d%c", arr[i], i == n ? '
' : ' ');}}
void pvarr_LL(ll *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%lld%c", arr[i], i == n ? '
' : ' ');}}
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
#define DEBUG_Switch 0
pll a[5];
int b[5];
int main()
{
#if DEBUG_Switch
    freopen("D:\code\input.txt", "r", stdin);
#endif
    //freopen("D:\code\output.txt","w",stdout);
    int t;
    t = readint();
    while (t--) {
        repd(i, 1, 4) {
            a[i].fi = readint();
            a[i].se = readint();
            b[i] = i;
        }
        ll ans = 1e18;
        do {
            ll x1 = min(a[b[1]].fi, a[b[3]].fi), x2 = max(a[b[1]].fi, a[b[3]].fi), x3 = min(a[b[2]].fi, a[b[4]].fi), x4 = max(a[b[2]].fi, a[b[4]].fi);
            ll y1 = min(a[b[1]].se, a[b[2]].se), y2 = max(a[b[1]].se, a[b[2]].se), y3 = min(a[b[3]].se, a[b[4]].se), y4 = max(a[b[3]].se, a[b[4]].se);
            ll x_l, x_r, y_l, y_r;
            ll cost_x, cost_y;
            if (x4 < x1) {
                x_l = x_r = 0;
                cost_x = x2 - x3 + x1 - x4;
            } else {
                x_l = max(x3 - x2, 0ll);
                x_r = x4 - x1;
                cost_x = x2 - x1 + x4 - x3;
            }

            if (y4 < y1) {
                y_l = y_r = 0;
                cost_y = y2 - y3 + y1 - y4;
            } else {
                y_l = max(y3 - y2, 0ll);
                y_r = y4 - y1;
                cost_y = y2 - y1 + y4 - y3;
            }

            ll temp = cost_x + cost_y;
            if (y_l > x_r) {
                temp += 2ll * (y_l - x_r);
            } else if (x_l > y_r) {
                temp += 2ll * (x_l - y_r);
            }
            ans = min(ans, temp);
        } while (next_permutation(b + 1, b + 1 + 4));
        
        printf("%lld
", ans );
    }

    return 0;
}



原文地址:https://www.cnblogs.com/qieqiemin/p/14069636.html