題意:有N個黑點和N個白點,每個黑點只能水平向右和豎直向下延伸,與一個白點配對。權值爲兩個點之間的曼哈頓距離。求使得n對點均配對最小的配對總權值。
思路:假設存在一組解,那麼我們交換一對配對,若依舊能夠滿足條件,畫畫圖就知道答案不變。
因此對於任意可行配對答案均相同。
於是我們統計出所有黑點的-x+y,所有白點的x-y,累加起來即可。
Code:
#include <cstdio>
#include <cstring>
typedef long long LL;
int main() {
int n;
scanf("%d", &n);
long long res = 0;
int x, y;
register int i;
for(i = 1; i <= n; ++i)
scanf("%d%d",&x, &y), res += y-x;
for(i = 1; i <= n; ++i)
scanf("%d%d", &x, &y), res -=y-x;
printf("%lld", res);
return 0;
}