UVa 10691 - Subway (思维 + 区间选点)

题意

每个地铁有一段有效距离,问最少需要多少段地铁。

思路

处理一下每个点的左距离和有距离,问题就变成了区间选点。

但是这个是个圆,所以要都加上2PI来算一遍。

代码

#include <stack>
#include <cstdio>
#include <list>
#include <cassert>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <functional>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <string>
#include <map>
#include <cmath>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define SZ(x) (int)x.size()
#define Lowbit(x) ((x) & (-x))
#define MP(a, b) make_pair(a, b)
#define MS(arr, num) memset(arr, num, sizeof(arr))
#define PB push_back
#define X first
#define Y second
#define ROP freopen("input.txt", "r", stdin);
#define MID(a, b) (a + ((b - a) >> 1))
#define LC rt << 1, l, mid
#define RC rt << 1|1, mid + 1, r
#define LRT rt << 1
#define RRT rt << 1|1
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const double eps = 1e-9;
const int MAXN = 1e5 + 10;
const int MOD = 9901;
const int dir[][2] = { {-1, 0}, {0, -1}, { 1, 0 }, { 0, 1 } };
int cases = 0;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
 
int cmp(pdd a, pdd b)
{
    return a.Y < b.Y;
}
 
vector<pdd> arr;
 
int Solve()
{
    int ans = SZ(arr);
    sort(arr.begin(), arr.end(), cmp);
    int tmpSize = SZ(arr);
    for (int i = 0; i < tmpSize; i++) arr.PB(MP(arr[i].X+2*PI, arr[i].Y+2*PI));
    for (int i = 0; i < tmpSize; i++)
    {
        int cnt = 1;
        double cur = arr[i].Y;
        for (int j = i+1; j < i+tmpSize; j++)
            if (arr[j].X-cur > eps)
            {
                cnt++;
                cur = arr[j].Y;
            }
        ans = min(ans, cnt);
    }
    return ans;
}
 
double Dis(double x, double y)
{
    return hypot(x, y);
}
 
int main()
{
    //ROP;
    int T;
    scanf("%d", &T);
    while (T--)
    {
        arr.clear();
        int n;
        double dis;
        scanf("%d%lf", &n, &dis);
        for (int i = 0; i < n; i++)
        {
            double x, y;
            scanf("%lf%lf", &x, &y);
            if (Dis(x, y) <= dis) continue;
            double arc = atan2(y, x);
            double add = asin(dis / Dis(x, y));
            arr.PB(MP(arc-add, arc+add));
            if (arr.back().Y > PI) arr.back().X -= 2*PI, arr.back().Y -= 2*PI;
        }
        printf("%d\n", Solve());
    }
    return 0;
}

Powered by Jekyll and Theme by solid