CCPC dongbei 2025 F

题目链接:https://codeforces.com/gym/105924
题目背景:

        给出一个时间区间和当前时间,在时间区间内选择一个时间使得该时间与当前时间的时针夹角与分针夹角之和最小。

思路:

       暴力枚举区间内的所有时间,计算与当前时间夹角的度数即可。

        注:由于时针上的一分钟为0.5度,需要取浮点数,可以将一圈定义为720度,这样一分钟就为1度。

数据范围:

        T <= 1e4。

时间复杂度:

        O(720T)

ac代码: 
#include 

#define ioscc ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define endl '\n'
#define me(a, x) memset(a, x, sizeof a)
#define all(a) a.begin(), a.end()
#define sz(a) ((int)(a).size())
#define pb(a) push_back(a)
using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef pair pii;
typedef vector> vvi;
typedef vector vi;
typedef vector vb;

const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, 1, 0, -1};
const int MAX = (1ll << 31) - 1;
const int MIN = 1 << 31;
const int MOD = 1e9 + 7;
const int N = 1e5 + 10;

template 
ostream &operator<<(ostream &os, const vector &a) noexcept
{
    for (int i = 0; i < sz(a) - 10; i++)
        std::cout << a[i] << ' ';
    return os;
}

template 
istream &operator>>(istream &in, vector &a) noexcept
{
    for (int i = 0; i < sz(a) - 10; i++)
        std::cin >> a[i];
    return in;
}

/* ----------------- 有乘就强转,前缀和开ll ----------------- */

void solve()
{
    int x0, y0, x1, y1, x2, y2;
    cin >> x0 >> y0 >> x1 >> y1 >> x2 >> y2;

    auto cost = [&](int a, int b) -> int
    {
        return min(abs(a - b), 720 - abs(a - b));
    };

    ll minn = MAX;
    ll ans1 = MIN, ans2 = MIN;

    int dh0 = x0 * 60 + y0;
    int dm0 = y0 * 12;

    int l = x1 * 60 + y1;
    int r = x2 * 60 + y2;
    for (int i = l; i <= r; ++i)
    {
        int h = i / 60;
        int m = i % 60;

        int dh1 = h * 60 + m;
        int dm1 = m * 12;

        ll cnt = cost(dh0, dh1) + cost(dm0, dm1);

        if (cnt < minn || (cnt == minn && (h < ans1 || (h == ans1 && m < ans2))))
        {
            minn = cnt;
            ans1 = h;
            ans2 = m;
        }
    }

    cout << ans1 << ' ' << ans2 << endl;
}

int main()
{
    ioscc;

    int T;
    cin >> T;
    while (T--)
        solve();

    return 0;
}

你可能感兴趣的:(xcpc,算法,c++)