Soju UVA - 1511 贪心

问题

https://vjudge.net/problem/UVA-1511

分析

两个点(x1,y1)和(x2,y2),现在x1<x2,y1和y2之间的关系未知。
∣ x 1 − x 2 ∣ + ∣ y 1 − y 2 ∣ = x 2 − x 1 + ∣ y 1 − y 2 ∣ |x1-x2|+|y1-y2|=x2-x1+|y1-y2| x1x2+y1y2=x2x1+y1y2,当 y 1 < y 2 时 , x 2 − x 1 + y 2 − y 1 = − ( x 1 + y 1 ) + ( x 2 + y 2 ) y1<y2时,x2-x1+y2-y1=-(x1+y1)+(x2+y2) y1<y2x2x1+y2y1=(x1+y1)+(x2+y2)
y 1 > y 2 时 , x 2 − x 1 + y 1 − y 2 = ( − x 1 + y 1 ) + ( x 2 − y 2 ) y1>y2时,x2-x1+y1-y2=(-x1+y1)+(x2-y2) y1>y2x2x1+y1y2=(x1+y1)+(x2y2)

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <map>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long LL;
const int maxn=100000+5,Inf=0x3f3f3f3f;
struct Point{
    int x,y,type;
}point[2*maxn];

bool cmp1(const Point &lhs,const Point &rhs){
    return lhs.y<rhs.y;
}
//
//bool cmp2(const Point &lhs,const Point &rhs){
//    return lhs.y>rhs.y;
//}

int n,m;
int s,kase=0,a[maxn],b[maxn];
int main(void){
    scanf("%d",&kase);
    while(kase--){
        scanf("%d",&n);
        for(int i=0;i<n;++i){
            scanf("%d%d",&point[i].x,&point[i].y);
            point[i].type=0;
        }
        scanf("%d",&m);
        for(int i=n;i<n+m;++i){
            scanf("%d%d",&point[i].x,&point[i].y);
            point[i].type=1;
        }
        sort(point,point+n+m,cmp1);
        int ans=Inf,low1=Inf,low2=Inf;
        for(int i=0;i<n+m;++i){
            if(point[i].type==1){
                ans=min(ans,point[i].x+point[i].y+low1);
                low2=min(low2,point[i].x-point[i].y);
            }else{
                ans=min(ans,-point[i].x+point[i].y+low2);
                low1=min(low1,-point[i].x-point[i].y);
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值