2017.10.16 T1 1983
样例数据
输入
3
3 5
1 2
4 3
6 3
5 2
2 1
输出
9
分析:由于白点永远在黑点右下方,所以曼哈顿距离就可以去掉绝对值了,最后所有的距离之和就是 sum(x白)−sum(x黑)+sum(y黑)−sum(y白) 。脑经急转弯……我还想了好久怎么求“最短”距离……
代码
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<ctime>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<iomanip>
#include<queue>
#include<set>
using namespace std;
int getint()
{
int sum=0,f=1;
char ch;
for(ch=getchar();!isdigit(ch)&&ch!='-';ch=getchar());
if(ch=='-')
{
f=-1;
ch=getchar();
}
for(;isdigit(ch);ch=getchar())
sum=(sum<<3)+(sum<<1)+ch-48;
return sum*f;
}
int n;
long long x,y,sumx,sumy;
int main()
{
freopen("match.in","r",stdin);
freopen("match.out","w",stdout);
n=getint();
for(int i=1;i<=n;++i)
{
x=getint(),y=getint();
sumx-=x;
sumy+=y;
}
for(int i=1;i<=n;++i)
{
x=getint(),y=getint();
sumx+=x;
sumy-=y;
}
cout<<sumx+sumy;
return 0;
}
本题结。