POJ 2653 Pick-up sticks(几何)

本文介绍了一道名为 Pick-upsticks 的几何模板题目,主要内容包括如何通过算法判断棍子之间的交叉情况,以此来找出所有顶部的棍子。文章提供了完整的 C++ 代码实现,并附带了解题思路。
Pick-up sticks
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 13377 Accepted: 5039

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.

Hint

Huge input,scanf is recommended.

Source

 
 

题解:

  几何模板题目

  

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <string>
 5 #include <algorithm>
 6 #include <cmath>
 7 #include <vector>
 8 #include <queue>
 9 #include <map>
10 #include <stack>
11 #include <set>
12 using namespace std;
13 typedef long long LL;
14 #define ms(a, b) memset(a, b, sizeof(a))
15 #define pb push_back
16 #define mp make_pair
17 #define eps 0.0001
18 const LL INF = 0x7fffffff;
19 const int inf = 0x3f3f3f3f;
20 const int mod = 1e9+7;
21 const int maxn = 100000+10;
22 int n, cnt;
23 struct POINT
24 {
25     double x, y, z;
26     POINT():x(0), y(0), z(0){};
27     POINT(double _x_, double _y_, double _z_ = 0):x(_x_), y(_y_), z(_z_) {};
28 };
29 struct SEG
30 {
31     POINT a;
32     POINT b;
33     SEG(){};
34     SEG(POINT _a_, POINT _b_):a(_a_),b(_b_) {};
35 };
36 double Cross(const POINT &a, const POINT & b, const POINT &o)//叉乘
37 {
38     return (a.x - o.x)*(b.y - o.y) - (b.x - o.x)*(a.y - o.y);
39 }
40 bool IsIntersect(const SEG &u, const SEG &v)
41 {
42     return (Cross(v.a, u.b, u.a)*Cross(u.b, v.b, u.a)>=0)&&
43         (Cross(u.a, v.b, v.a)*Cross(v.b, u.b, v.a)>=0)&&
44         (max(u.a.x, u.b.x) >= min(v.a.x, v.b.x))&&
45         (max(v.a.x, v.b.x) >= min(u.a.x, u.b.x))&&
46         (max(u.a.y, u.b.y) >= min(v.a.y, v.b.y))&&
47         (max(v.a.y, v.b.y) >= min(u.a.y, u.b.y));
48 }
49 int ans[maxn];
50 SEG stick[maxn];
51 void init()
52 {
53     cnt = 0;
54 }
55 void solve() {
56     double a, b, c, d;
57     for(int i = 1;i<=n;i++){
58         scanf("%lf%lf%lf%lf", &a, &b, &c, &d);
59         stick[i].a.x=a;stick[i].a.y=b;
60         stick[i].b.x=c;stick[i].b.y=d;
61     }
62     for(int i = n;i>0;i--){
63         bool flag = 1;
64         for(int j = i+1;j<=n;j++){
65             if(IsIntersect(stick[i], stick[j])){
66                 flag = 0;
67                 break;
68             }
69         }
70         if(flag){
71             ans[cnt++] = i;
72         }
73         if(cnt>=1000){
74             break;
75         }
76     }
77     printf("Top sticks:");
78     for(int i = cnt-1;i>=0;i--){
79         if(i==0)    printf(" %d.",ans[i]);
80         else printf(" %d,",ans[i]);
81     }
82     printf("\n");
83 }
84 int main() {
85 #ifdef LOCAL
86     freopen("input.txt", "r", stdin);
87 //        freopen("output.txt", "w", stdout);
88 #endif
89     while(~scanf("%d", &n)&&n){
90         init();
91         solve();
92     }
93     return 0;
94 }
View Code

 

转载于:https://www.cnblogs.com/denghaiquan/p/7223237.html

【电动汽车充电站有序充电调度的分散式优化】基于蒙特卡诺和拉格朗日的电动汽车优化调度(分时电价调度)(Matlab代码实现)内容概要:本文介绍了基于蒙特卡洛和拉格朗日方法的电动汽车充电站有序充电调度优化方案,重点在于采用分散式优化策略应对分时电价机制下的充电需求管理。通过构建数学模型,结合不确定性因素如用户充电行为和电网负荷波动,利用蒙特卡洛模拟生成大量场景,并运用拉格朗日松弛法对复杂问题进行分解求解,从而实现全局最优或近似最优的充电调度计划。该方法有效降低了电网峰值负荷压力,提升了充电站运营效率与经济效益,同时兼顾用户充电便利性。 适合人群:具备一定电力系统、优化算法和Matlab编程基础的高校研究生、科研人员及从事智能电网、电动汽车相关领域的工程技术人员。 使用场景及目标:①应用于电动汽车充电站的日常运营管理,优化充电负荷分布;②服务于城市智能交通系统规划,提升电网与交通系统的协同水平;③作为学术研究案例,用于验证分散式优化算法在复杂能源系统中的有效性。 阅读建议:建议读者结合Matlab代码实现部分,深入理解蒙特卡洛模拟与拉格朗日松弛法的具体实施步骤,重点关注场景生成、约束处理与迭代收敛过程,以便在实际项目中灵活应用与改进。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值