POJ2653.Pick-up sticks(线段相交)

Pick-up sticks
http://poj.org/problem?id=2653
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 14325 Accepted: 5420
Description

Stan has n sticks of various length. He throws them one at a time on the floor in a random way. After finishing throwing, Stan tries to find the top sticks, that is these sticks such that there is no stick on top of them. Stan has noticed that the last thrown stick is always on top but he wants to know all the sticks that are on top. Stan sticks are very, very thin such that their thickness can be neglected.
Input

Input consists of a number of cases. The data for each case start with 1 <= n <= 100000, the number of sticks for this case. The following n lines contain four numbers each, these numbers are the planar coordinates of the endpoints of one stick. The sticks are listed in the order in which Stan has thrown them. You may assume that there are no more than 1000 top sticks. The input is ended by the case with n=0. This case should not be processed.
Output

For each input case, print one line of output listing the top sticks in the format given in the sample. The top sticks should be listed in order in which they were thrown.

The picture to the right below illustrates the first case from input.
Sample Input

5
1 1 4 2
2 3 3 1
1 -2.0 8 4
1 4 8 2
3 3 6 -2.0
3
0 0 1 1
1 0 2 1
2 0 3 1
0
Sample Output

Top sticks: 2, 4, 5.
Top sticks: 1, 2, 3.
题解:向地上扔棒子,求没被后扔的棒子压住的棒子号码。

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
struct Line { //一个棒子相当于一根线段 
    double x, y, x1, y1;
};
double Direction(int a, int b, int c, int d, int e, int f) {//以(a,b)为共享端点,判断一条线段相对另一条线段的方向 
    return (c-a)*(f-b) - (e-a)*(d-b);
}
bool OnSegment(int a, int b, int c, int d, int e, int f) {//判断点是否在另一条线段的坐标范围内 
    if (min(a, e) <= c && c <= max(a, e) && min(b, f) <= d && d <= max(b, f))
        return true;
    return false;
}
bool Point(struct Line a, struct Line b) {  //判断两个棒子是否相交 ,相交返回true,不相交返回false 
    double d1, d2, d3, d4;                      
    d1 = Direction(a.x, a.y, b.x, b.y, b.x1, b.y1);
    d2 = Direction(a.x1, a.y1, b.x, b.y, b.x1, b.y1);
    d3 = Direction(b.x, b.y, a.x, a.y, a.x1, a.y1);
    d4 = Direction(b.x1, b.y1, a.x, a.y, a.x1, a.y1);
    if (((d1 > 0 && d2 < 0) || (d1 < 0 && d2 > 0)) && ((d3 > 0 && d4 < 0) || (d3 < 0 && d4 > 0)))
        return true; 
    else if (d1 == 0 && OnSegment(a.x, a.y, b.x, b.y, b.x1, b.y1)) //当它们共线时,判断它们是否相交 
        return true;
    else if (d2 == 0 && OnSegment(a.x1, a.y1, b.x, b.y, b.x1, b.y1))
        return true;
    else if (d3 == 0 && OnSegment(b.x, b.y, a.x, a.y, a.x1, a.y1))
        return true;
    else if (d4 == 0 && OnSegment(b.x1, b.y1, a.x, a.y, a.x1, a.y1))
        return true;
    else
        return false;
}
int main() {
    int n;
    while (scanf("%d", &n) == 1 && n) { 
        struct Line a[n+1];
        int num[n+1] = {0};
        int temp = 1;
        for (int i = 1; i <= n ; i++) { //输入 
            scanf("%lf %lf %lf %lf", &a[i].x, &a[i].y, &a[i].x1, &a[i].y1);
        }
        for (int i = 1; i <= n; i++) {
            bool flag;
            for (int j = i+1; j <= n; j++) {
                flag = Point(a[i], a[j]);//判断当前的棒子是否被后来的棒子压住 
            }
            if (!flag) //记录没被压住的棒子的序号 
                num[temp++] = i;
        }
        printf("Top sticks:"); //输出 
        for (int i = 1; i < temp-1; i++)
            printf(" %d,", num[i]);
        printf(" %d.", num[temp-1]);
        printf("\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
POJ - 3616是一个题目,题目描述如下: 给定一组区间,每个区间有一个权重,要求选择一些区间,使得这些区间的右端点都小于等于k,并且权重之和最大。请问最大的权重和是多少? 解决这个问题的思路是使用动态规划。首先,将区间按照左端点从小到大进行排序。然后,定义一个dp数组,dp[i]表示右端点小于等于i的所有区间所能得到的最大权重。 接下来,遍历每一个区间,对于每个区间i,将dp[i]初始化为区间i的权重。然后,再遍历i之前的每个区间j,如果区间j的右端点小于等于k,并且区间j的权重加上区间i的权重大于dp[i],则更新dp[i]为dp[j]加上区间i的权重。 最后,遍历整个dp数组,找到最大的权重和,即为所求的答案。 下面是具体的代码实现: ```cpp #include <cstdio> #include <cstring> #include <algorithm> using namespace std; struct interval{ int start, end, weight; }; interval intervals[10005]; int dp[10005]; int n, m, k; bool compare(interval a, interval b) { if (a.start == b.start) { return a.end < b.end; } else { return a.start < b.start; } } int main() { while(~scanf("%d %d %d", &n, &m, &k)) { memset(dp, 0, sizeof dp); for (int i = 0; i < m; i++) { scanf("%d %d %d", &intervals[i].start, &intervals[i].end, &intervals[i].weight); } sort(intervals, intervals + m, compare); for (int i = 0; i < m; i++) { dp[i] = intervals[i].weight; for (int j = 0; j < i; j++) { if (intervals[j].end <= k && dp[j] + intervals[i].weight > dp[i]) { dp[i] = dp[j] + intervals[i].weight; } } } int maxWeight = 0; for (int i = 0; i < m; i++) { maxWeight = max(maxWeight, dp[i]); } printf("%d\n", maxWeight); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值