2018 “百度之星”程序设计大赛 - 初赛(B)1006rect

2018 “百度之星”程序设计大赛 - 初赛(B)rect

 Accepts: 1682

 Submissions: 3028

 Time Limit: 2000/1000 MS (Java/Others)

 Memory Limit: 131072/131072 K (Java/Others)

Problem Description

度度熊有一个大小为 MX \times MYMX×MY 的矩形,左下角坐标为 (0, 0)(0,0),右上角坐标为 (MX, MY)(MX,MY)。此矩形内有 NN 个整数坐标的点 (x_i, y_i)(x​i​​,y​i​​),x_ix​i​​ 彼此不重复,y_iy​i​​ 彼此也不重复。

现在要从每一个点画出一条线段,满足下列条件:

  • 线段起点为坐标点,终点在矩形范围的四个边界之一上。
  • 线段彼此不能交叉。

现在要让画出的线段的长度总和最小,请输出这个最小的长度总和值。

Input

输入的第一行有一个正整数 TT,代表接下来有几笔测试资料。

对于每笔测试资料: 第一行有三个整数 MXMX, MYMY 以及 NN。 接下来的 NN 行每行有两个正整数 x_ix​i​​ 及 y_iy​i​​。

  • 2 \le MX, MY \le 10^62≤MX,MY≤10​6​​
  • 0 \le N \le 10^50≤N≤10​5​​
  • 如果 i \ne ji≠j,则保证 x_i \ne x_jx​i​​≠x​j​​ 及 y_i \ne y_jy​i​​≠y​j​​
  • 0 < x_i < MX0<x​i​​<MX
  • 0 < y_i < MY0<y​i​​<MY
  • 1 \le T \le 201≤T≤20
  • 至多 22 笔测试资料中的 N > 1000N>1000

Output

对于每一笔测试资料,请依序各自在一行内输出一个整数,代表可能的最小长度和。

Sample Input

2
4 4 1
2 2
10 7 3
6 3
2 6
9 5

Sample Output

2
5 

思路:终点在矩形边上的话,明显垂直于xORy轴比斜线短,就是不知道不全部平行会不会相交:

           假设(x1,y1)有y1<My-y1 , x1 , Mx-x1  ,对于另外一点(x2,y2): 若y2或者My-y2比较小就平行了不会交叉所以不讨论了

                                                                1.y2<y1, 若x2是四个中最小的:y2<y1<x1,x2<y2  ==》x2<x1 ==不会相交

                                                                                  若Mx-x2最小:y2< y1< Mx - x1,Mx-x2<y2==》Mx-x2 <Mx - x1,不会相交

                                                                2.y2>y1同理

                                 其他都一样的就不写了。

所以就把min(y,My-y, x, Mx-x )的和算出来就可以了

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <malloc.h>
#define Twhile() int T;scanf("%d",&T);while(T--)
#define clc(a,b,n) for(int i=0;i<=n;i++)a[i]=b
#define clc2(a,b,n,m) for(int i=0;i<=n;i++)for(int j=0;j<=m;j++)a[i][j]=b
#define fora(i,a,b) for(int i=a;i<b;i++)
#define fors(i,a,b) for(int i=a;i>b;i--)
#define fora2(i,a,b) for(int i=a;i<=b;i++)
#define fors2(i,a,b) for(int i=a;i>=b;i--)
#define PI acos(-1.0)
#define eps 1e-6
#define INF 0x3f3f3f3f
#define BASE 131

typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
using namespace std;
const int maxn=200000+11;
LL MX,MY,N;
int main()
{
    Twhile()
    {
        scanf("%lld%lld%lld",&MX,&MY,&N);
        LL ans=0;
        fora2(i,1,N)
        {
            LL x,y;
            scanf("%lld%lld",&x,&y);
            ans+=min(min(x,y),min(MX-x,MY-y));
        }
        printf("%lld\n",ans);

    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/liyang__abc/article/details/81612122
今日推荐