The Fortified Forest UVALive - 5211 [二进制枚举+凸包周长]

最小价值砍树策略
本文介绍了一种通过二进制枚举与凸包求周长的方法来解决在保证篱笆长度的同时选择价值最小树木的问题。该算法适用于UVALive-5211题目,通过对所有可能的树木组合进行模拟,并利用几何技巧来确定最优解。

The Fortified Forest UVALive - 5211

题意:n棵树,每棵树对应有x,y,v,l分别代表横坐标,纵坐标,价值,砍这棵树能构成的篱笆长度。现在要求输出,在最小被砍价值的情况下,输出选了哪些树,还剩下多少长的篱笆。如果有多个相同的最小值,输出选点最少的。

思路: 二进制枚举选树情况,凸包求周长,模拟下去。几何题小心精度问题

#include<cstdio>
#include<vector>
#include<cmath>
#include<math.h>
#include<string>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<map>
#define PI acos(-1.0)
#define pb push_back
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=1005;
const int MOD=1e9+7;
template <class T>
bool sf(T &ret){ //Faster Input
    char c; int sgn; T bit=0.1;
    if(c=getchar(),c==EOF) return 0;
    while(c!='-'&&c!='.'&&(c<'0'||c>'9')) c=getchar();
    sgn=(c=='-')?-1:1;
    ret=(c=='-')?0:(c-'0');
    while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
    if(c==' '||c=='\n'){ ret*=sgn; return 1; }
    while(c=getchar(),c>='0'&&c<='9') ret+=(c-'0')*bit,bit/=10;
    ret*=sgn;
    return 1;
}
int n;
int sign(double x){
    return abs(x)<1e-7?0:x<0?-1:1;
}
struct Point{
    int x,y;
    int v,l,id;
    Point(int x=0, int y=0,int id=0) : x(x), y(y),id(id){}
    Point operator - (const Point &rhs) const{
        return Point(x-rhs.x,y-rhs.y);
    }
    bool operator == (const Point &rhs) const{
        return sign(x-rhs.x)==0&&sign(y-rhs.y)==0;
    }
    bool operator < (const Point &rhs)const{
        if(x==rhs.x)    return y<rhs.y;
        else    return x<rhs.x;
    }
}p[N];
typedef Point Vector;
double cross(Vector A,Vector B){
    return (double)A.x*B.y-(double)A.y*B.x;
}
typedef vector<Point> Polygon;
Polygon convex_hull(Polygon P) {
    sort(P.begin(), P.end());  //排序
    P.erase(unique(P.begin(), P.end()), P.end());  //删除重复点
    int n = P.size(), k = 0;
    Polygon Q(n*2);
    for (int i=0; i<n; ++i) {
        while (k > 1 && cross(Q[k-1]-Q[k-2], P[i]-Q[k-2]) <= 0) k--;
        Q[k++] = P[i];
    }
    for (int t=k, i=n-2; i>=0; --i) {
        while (k > t && cross(Q[k-1]-Q[k-2], P[i]-Q[k-2]) <= 0) k--;
        Q[k++] = P[i];
    }
    Q.resize(k);
    return Q;
}
double dist(Point a,Point b){
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double maxv=0;
double restlen=0;
vector<int> nb;
void mian(){
    for(int i=0;i<n;i++)   sf(p[i].x),sf(p[i].y),sf(p[i].v),sf(p[i].l),p[i].id=i+1;
    for(int i=0;i<=(1<<n)-1;++i){
        vector<Point> t;
        vector<int> choose;
        int totlen=0,totv=0;
        for(int j=0;j<=n-1;j++){
            if((1<<j)&i)    totlen+=p[j].l,choose.pb(j+1);
            else    t.pb(p[j]),totv+=p[j].v;
        }
        vector<Point> ans=convex_hull(t);
        double dis=0;
        for(int i=0;i<(int)ans.size()-1;i++)    dis+=dist(ans[i],ans[i+1]);
        if(sign(totlen-dis)<0)  continue; // 这里直接减被卡精度。几何题判正负用sign安全
        if(totv>maxv){
            maxv=totv;
            restlen=totlen-dis;
            nb.resize((int)choose.size());
            for(int i=0;i<(int)choose.size();++i)
                nb[i]=choose[i];
        }
        else if(totv==maxv){
            int sz1=(int)choose.size();
            int sz2=(int)nb.size();
            if(sz1>=sz2)    continue;
            restlen=totlen-dis;
            nb.resize((int)choose.size());
            for(int i=0;i<(int)choose.size();++i)
                nb[i]=choose[i];
        }
    }

}
int main(void){
    int ks=0;
    while(cin >>n){
        maxv=restlen=0;
        nb.clear();
        if(!n)  break;
        if(ks>0)    printf("\n");
        mian();
        printf("Forest %d\n",++ks);
        printf("Cut these trees:");
//        cout <<"sz="<<nb.size()<<endl;
//        sort(nb.begin(),nb.end());
        for(int i=0;i<(int)nb.size();i++)   printf(" %d",nb[i]);
        printf("\n");
        printf("Extra wood: %.2f\n",restlen);
    }

    return 0;
}

 

gcc -Iinclude/ -Isrc/ -DCUDNN -Wall -Wno-unused-result -Wno-unknown-pragmas -Wfatal-errors -fPIC -Ofast -DCUDNN -c ./src/gemm.c -o obj/gemm.o gcc -Iinclude/ -Isrc/ -DCUDNN -Wall -Wno-unused-result -Wno-unknown-pragmas -Wfatal-errors -fPIC -Ofast -DCUDNN -c ./src/utils.c -o obj/utils.o In file included from /usr/include/string.h:495, from ./src/utils.c:3: In function 'strncpy', inlined from 'copy_string' at ./src/utils.c:426:5: /usr/include/x86_64-linux-gnu/bits/string_fortified.h:106:10: warning: '__builtin_strncpy' specified bound depends on the length of the source argument [-Wstringop-overflow=] 106 | return __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest)); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ./src/utils.c: In function 'copy_string': ./src/utils.c:426:22: note: length computed here 426 | strncpy(copy, s, strlen(s)+1); | ^~~~~~~~~ gcc -Iinclude/ -Isrc/ -DCUDNN -Wall -Wno-unused-result -Wno-unknown-pragmas -Wfatal-errors -fPIC -Ofast -DCUDNN -c ./src/cuda.c -o obj/cuda.o gcc -Iinclude/ -Isrc/ -DCUDNN -Wall -Wno-unused-result -Wno-unknown-pragmas -Wfatal-errors -fPIC -Ofast -DCUDNN -c ./src/deconvolutional_layer.c -o obj/deconvolutional_layer.o gcc -Iinclude/ -Isrc/ -DCUDNN -Wall -Wno-unused-result -Wno-unknown-pragmas -Wfatal-errors -fPIC -Ofast -DCUDNN -c ./src/convolutional_layer.c -o obj/convolutional_layer.o ./src/convolutional_layer.c: In function 'get_workspace_size': ./src/convolutional_layer.c:91:9: warning: implicit declaration of function 'cudnnGetConvolutionForwardWorkspaceSize' [-Wimplicit-function-declaration] 91 | cudnnGetConvolutionForwardWorkspaceSize(cudnn_handle(), | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ./src/convolutional_layer.c:91:49: warning: implicit declaration of function 'cudnn_handle' [-Wimplicit-function-declaration] 91 | 这个错误应该如何解决,请提供参考网址
最新发布
04-04
### 关于 `strncpy` 函数的 `stringop-overflow` 警告 在 GCC 编译器中,当检测到字符串操作可能导致缓冲区溢出时会触发 `-Wstringop-overflow` 警告。对于 `strncpy` 的使用,通常是因为目标缓冲区大小不足以容纳源字符串加上终止符 `\0` 所致。 为了修复此问题,可以采取以下方法: 1. **确保目标缓冲区足够大** 使用 `strncpy` 时,应明确指定目标缓冲区的最大长度,并确保其能够容纳完整的源字符串和额外的一个字符用于存储 NULL 终止符[^1]。 ```c char dest[SIZE]; const char* src = "source_string"; size_t n = sizeof(dest) - 1; strncpy(dest, src, n); dest[n] = '\0'; // 明确设置 NULL 终止符 ``` 2. **启用编译选项 `-D_FORTIFY_SOURCE=2`** 如果项目启用了 `_FORTIFY_SOURCE` 宏,则可以通过调整代码逻辑来避免潜在的安全隐患。该宏会在运行时检查数组边界并报告可能的越界访问行为。 --- ### 隐式声明警告 (`implicit declaration`) #### 对于 `cudnnGetConvolutionForwardWorkspaceSize` 如果遇到 `cudnnGetConvolutionForwardWorkspaceSize` 或其他 cuDNN API 的隐式声明警告,通常是由于缺少必要的头文件或库链接配置所致。 以下是解决方案: 1. **确认已包含正确的头文件** 确保程序中包含了 `<cudnn.h>` 文件,这是 CUDA Deep Neural Network 库的核心接口定义所在位置[^2]。 ```cpp #include <cudnn.h> ``` 2. **验证链接器参数** 在构建命令中加入 `-lcudnn` 参数以连接 cuDNN 动态库。如果没有正确链接静态/动态版本的 cuDNN 库,可能会导致函数不可见的情况发生。 示例 Makefile 片段如下所示: ```makefile LDFLAGS += -L/path/to/cudnn/lib -lcudnn CPPFLAGS += -I/path/to/cudnn/include ``` 3. **检查 cuDNN SDK 是否安装完整** 若上述步骤均已完成但仍存在问题,请重新下载最新版 NVIDIA cuDNN 并按照官方文档说明完成环境搭建过程。 --- #### 对于 `cudnn_handle` 类型未识别的问题 这可能是由于未正确定义 `cudnnHandle_t` 数据类型的缘故。它实际上是一个指向内部实现细节的不透明句柄对象,在调用任何实际功能之前需先创建实例化后的上下文环境。 示例代码片段展示如何安全地初始化此类资源管理单元: ```cpp #include <cudnn.h> int main() { cudnnHandle_t handle; cudnnStatus_t status; status = cudnnCreate(&handle); // 创建新句柄 if (status != CUDNN_STATUS_SUCCESS) { fprintf(stderr, "Failed to create cuDNN context\n"); return EXIT_FAILURE; } // ...其余业务逻辑... cudnnDestroy(handle); // 销毁不再使用的句柄 } ``` --- ### 总结 通过以上措施可有效消除由不当使用标准库函数引发的相关编译期提示信息;同时针对第三方扩展模块则务必遵循厂商给出的最佳实践指南来进行集成开发工作流程设计。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值