cf1555B. Two Tables

Posted Jozky86

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了cf1555B. Two Tables相关的知识,希望对你有一定的参考价值。

cf1555B. Two Tables

题意:

一个大矩阵空间内放置一个矩阵a,现在要再往这个空间内放一个矩阵b,a移动距离len才能放下b,问len最小是多少

题解:

不难发现左右或上下移动是最佳的,斜着移动是最不好的。此时我们就需要判断左右移动是否有足够的空间,并且移动后是否可以安放b
b的安防一定是靠着墙角的

代码:

// Problem: B. Two Tables
// Contest: Codeforces - Educational Codeforces Round 112 (Rated for Div. 2)
// URL: https://codeforces.com/contest/1555/problem/B
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// Data:2021-08-16 23:47:26
// By Jozky

#include <bits/stdc++.h>
#include <unordered_map>
#define debug(a, b) printf("%s = %d\\n", a, b);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
clock_t startTime, endTime;
//Fe~Jozky
const ll INF_ll= 1e18;
const int INF_int= 0x3f3f3f3f;
void read()
{
}
template <typename _Tp, typename... _Tps> void read(_Tp& x, _Tps&... Ar)
{
    x= 0;
    char c= getchar();
    bool flag= 0;
    while (c < '0' || c > '9')
        flag|= (c == '-'), c= getchar();
    while (c >= '0' && c <= '9')
        x= (x << 3) + (x << 1) + (c ^ 48), c= getchar();
    if (flag)
        x= -x;
    read(Ar...);
}

template <typename T> inline void write(T x)
{
    if (x < 0) {
        x= ~(x - 1);
        putchar('-');
    }
    if (x > 9)
        write(x / 10);
    putchar(x % 10 + '0');
}
void rd_test()
{
#ifdef LOCAL
    startTime= clock();
    freopen("in.txt", "r", stdin);
#endif
}
void Time_test()
{
#ifdef LOCAL
    endTime= clock();
    printf("\\nRun Time:%lfs\\n", (double)(endTime - startTime) / CLOCKS_PER_SEC);
#endif
}
int main()
{
    //rd_test();
    int t;
    read(t);
    while (t--) {
        int a, b, x1, x2, y1, y2, a1, b1, a2, b2;
        read(a);
        read(b);
        cin >> x1 >> y1 >> x2 >> y2 >> a2 >> b2;
        a1= x2 - x1;//矩阵a的长
        b1= y2 - y1;//矩阵a的宽
        if (a1 + a2 > a && b1 + b2 > b) {//如果矩阵a和矩阵b长宽加起来会超出空间
            printf("-1\\n"); //超界
            continue;
        }
        int ans= 0x3f3f3f3f;
        if (a1 + a2 <= a) { //横向移动不会出界
            if (x1 >= a2 || x2 + a2 <= a)
                ans= 0; //不用移动
            else
                ans= min(ans, min(a2 - x1, x2 + a2 - a)); //左右移动取最小
        }
        if (b1 + b2 <= b) {
            if (y1 >= b2 || y2 + b2 <= b)
                ans= 0;
            else
                ans= min(ans, min(b2 - y1, y2 + b2 - b));
        }
        printf("%d.000000000\\n", ans);
    }
    return 0;
    //Time_test();
}

以上是关于cf1555B. Two Tables的主要内容,如果未能解决你的问题,请参考以下文章

codeforces 1555 B. Two Tables (思维)

cf1555C Coin Rows

cf1555D. Say No to Palindromes

Educational Codeforces Round 112 (Rated for Div. 2)-B. Two Tables-题解

Educational Codeforces Round 112 (Rated for Div. 2)-B. Two Tables-题解

cf1555A. PizzaForces