usaco5.1.1 Fencing the Cows

本文介绍了一个经典的计算几何问题——围栏问题,并提供了一个使用C++实现的Graham Scan算法解决方案。该算法用于找到包围一系列点的最短围栏长度。

一 原题

Fencing the Cows
Hal Burch

Farmer John wishes to build a fence to contain his cows, but he's a bit short on cash right. Any fence he builds must contain all of the favorite grazing spots for his cows. Given the location of these spots, determine the length of the shortest fence which encloses them.

PROGRAM NAME: fc

INPUT FORMAT

The first line of the input file contains one integer, N. N (0 <= N <= 10,000) is the number of grazing spots that Farmer john wishes to enclose. The next N line consists of two real numbers, Xi and Yi, corresponding to the location of the grazing spots in the plane (-1,000,000 <= Xi,Yi <= 1,000,000). The numbers will be in decimal format.

SAMPLE INPUT (file fc.in)

4
4 8
4 12
5 9.3
7 8

OUTPUT FORMAT

The output should consists of one real number, the length of fence required. The output should be accurate to two decimal places.

SAMPLE OUTPUT (file fc.out)

12.00



二 分析

裸的闭包。凭着回忆写的graham scan。忘记了发现叉乘结果为负就删除队尾元素这一过程是要递归进行的,前几次提交这一过程都只做了一次。。


三 代码

运行结果:
USER: Qi Shen [maxkibb3]
TASK: fc
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.000 secs, 4420 KB]
   Test 2: TEST OK [0.000 secs, 4420 KB]
   Test 3: TEST OK [0.000 secs, 4420 KB]
   Test 4: TEST OK [0.000 secs, 4420 KB]
   Test 5: TEST OK [0.011 secs, 4684 KB]
   Test 6: TEST OK [0.011 secs, 4816 KB]
   Test 7: TEST OK [0.011 secs, 5212 KB]
   Test 8: TEST OK [0.022 secs, 5872 KB]

All tests OK.

Your program ('fc') produced all correct answers! This is your submission #5 for this problem. Congratulations!


AC代码:
/*
ID:maxkibb3
LANG:C++
PROB:fc
*/

#include<cstdio>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;

const int MAX = 10005;

int n;
int convex_size;

struct Coor {
    double x, y;
    double angle;
   
    Coor() {}
    Coor(double _x, double _y):x(_x),y(_y) {}
    
    Coor operator - (const Coor &obj) const {
        Coor *ret = new Coor(x - obj.x, y - obj.y);
        return *ret;
    }
    
    double dis(const Coor &obj) const {
        return sqrt((x - obj.x) * (x - obj.x) +
                    (y - obj.y) * (y - obj.y));
    }
    
    void set_angle(const Coor &obj) {
        if(x == obj.x && y == obj.y) {
            angle = MAX;
            return;
        }
        Coor v = *this - obj;
        angle =  v.x / dis(obj);
    }
}coor[MAX];
vector<Coor> convex;
Coor sc;

bool cmp(const Coor &a, const Coor &b) {
    if(a.angle == b.angle)
        return a.dis(sc) > b.dis(sc);
    return a.angle > b.angle;
}

void init() {
    freopen("fc.in", "r", stdin);
    freopen("fc.out", "w", stdout);
    scanf("%d", &n);
    for(int i = 0; i < n; i++) {
        scanf("%lf%lf", &coor[i].x, &coor[i].y);
        if(coor[i].y < sc.y ||
            (coor[i].y == sc.y && coor[i].x < sc.x))
            sc = coor[i];
    }
    for(int i = 0; i < n; i++) {
        coor[i].set_angle(sc);
    }
    sort(coor, coor + n, cmp);
}

double cross_product(Coor a, Coor b, Coor c) {
    Coor v1 = b - a;
    Coor v2 = c - b;
    return v1.x * v2.y - v1.y * v2.x;
}

void graham_scan() {
    if(n == 1) {
        convex.push_back(coor[0]);
        return;
    }
    convex.push_back(coor[0]);
    convex.push_back(coor[1]);
    convex_size = 2;
    for(int i = 2; i < n; i++) {
        while(cross_product(convex[convex_size - 2],
                        convex[convex_size - 1], coor[i]) <= 0) {  //这个地方没写成while,WA了好几次。。
            convex.pop_back();
            convex_size--;
        }
        convex.push_back(coor[i]);
        convex_size++;
    }
}

void solve() {
    graham_scan();
    double ans = 0;
    for(int i = 0; i < convex_size; i++)
        ans += convex[i].dis(convex[(i+1) % convex_size]);
    printf("%.2lf\n", ans);
}

int main() {
    init();
    solve();
    return 0;
}



### Feeding the Cows B 问题解析 在《USACO 2022年12月比赛》的 Silver 组第二题中,题目要求解决一个关于奶牛喂养的问题。具体描述如下: 输入包括一个长度为 $ n $ 的字符串,表示一排奶牛,其中每个字符为 'C' 或 'W',分别表示该位置有一头奶牛或是一片草地。目标是通过最少的操作次数,使得每头奶牛('C')都能在其左侧或右侧至少有一个相邻的草地('W'),以便能够被喂养。每次操作可以将一个 'C' 变成 'W' 或者将一个 'W' 变成 'C'。 输出为最小的操作次数,若无法满足条件则输出 -1。 #### 问题分析 1. **问题条件**: - 每个奶牛必须在其左右至少有一个相邻的草地。 - 每次操作可以修改一个字符('C' <-> 'W')。 - 需要找出最小操作次数。 2. **贪心策略**: - 从左到右遍历字符串,当遇到一个奶牛('C')时,检查其右侧是否有一个草地('W'),如果存在,将该草地变为奶牛的“喂养点”。 - 如果右侧没有草地,则需要修改当前奶牛或其右侧的某个字符以满足条件。 3. **实现逻辑**: - 遍历字符串,维护一个指针,标记当前可以使用的草地位置。 - 如果当前字符是 'C',且其右侧没有可用草地,则需要修改一个字符。 - 记录每次操作,并确保最终所有奶牛都能被喂养。 #### 示例代码实现 ```cpp #include <iostream> #include <string> using namespace std; int main() { int n; string s; cin >> n >> s; int operations = 0; int lastGrass = -1; for (int i = 0; i < n; ++i) { if (s[i] == 'C') { if (lastGrass == -1 || lastGrass < i - 1) { // Check if there is a grass to the right bool found = false; for (int j = i + 1; j < n; ++j) { if (s[j] == 'W') { lastGrass = j; found = true; break; } } if (!found) { cout << -1 << endl; return 0; } } lastGrass = i + 1; // Mark the next available grass position continue; } else if (s[i] == 'W') { lastGrass = i; } } cout << operations << endl; return 0; } ``` #### 时间复杂度 - 该算法的时间复杂度为 $ O(n) $,因为每个字符最多被访问两次(一次遍历,一次查找草地)。 #### 空间复杂度 - 空间复杂度为 $ O(1) $,仅使用了常量级的额外空间。 #### 注意事项 - 需要处理边界情况,例如字符串末尾没有草地。 - 如果无法满足条件(即存在无法喂养的奶牛),应输出 -1。 --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值