ZOJ Monthly, January 2019 Little Sub and his Geometry Problem 【推导 + 双指针】

本文介绍了一道编程竞赛中的几何问题,需要计算特定条件下点到一组预设点的距离总和等于给定值的点的数量。通过巧妙的数学转换和算法设计,文章详细解释了如何在O(Q*N)的时间复杂度内解决这一问题。

传送门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5861

Little Sub and his Geometry Problem


Time Limit: 4 Seconds       Memory Limit: 65536 KB

Little Sub loves math very much, and has just come up with an interesting problem when he is working on his geometry homework.

It is very kind of him to share this problem with you. Please solve it with your coding and math skills. Little Sub says that even Mr.Potato can solve it easily, which means it won't be a big deal for you.

The problem goes as follows:

Given two integers  and , and  points  with their Euclidean coordinates  on a 2-dimensional plane. Different points may share the same coordinate.

Define the function

 
where
 

 

You are required to solve several queries.

In each query, one parameter  is given and you are required to calculate the number of integer pairs  such that  and .

Input

There are multiple test cases. The first line of the input contains an integer  (), indicating the number of test cases. For each test case:

The first line contains two positive integers  and  ().

For the following  lines, the -th line contains two integers  and  (), indicating the coordinate of the -th point.

The next line contains an integer  (), indicating the number of queries.

The following line contains  integers  (), indicating the parameters for each query.

It's guaranteed that at most 20 test cases has .

Output

For each test case, output the answers of queries respectively in one line separated by a space.

Please, DO NOT output extra spaces at the end of each line, or your answer may be considered incorrect!

Sample Input
2
4 2
1 1
2 3
5
1 2 3 4 5
15 5
1 1 
7 3 
5 10 
8 6 
7 15
3
25 12 31
Sample Output
2 3 4 1 2
5 11 5

 

 

题意概括:

N*N的大小的二维平面, M 个特殊点。

Q 次查询:查询 到达特殊点距离总和为 Ci 的点的数量。

只有在特殊点的右上方的点才会产生距离。

 

解题思路:

过特殊点作斜率为 -1 的直线,发现这些线上的点到特殊点距离相等,也就是说同一X,只有一个点满足距离和为 Ci。

选择最左下角的点作为参考点(假设虚拟一个(0,0))。

那么平面上坐标为 (x,y)的点所产生的距离和 C = (x+y)*cnt-sum;

其中 cnt 为 该点左下方的特殊点个数,sum为这些特殊点到参考点的距离总和。

(也就是相当于 (x, y)到参考点的距离 - 特殊点 i 到参考点的距离 = (x,y)到特殊点的距离。)

很显然x,y具有线性关系,随着 x 增大 y 减小,每次查询遍历一遍 x 而 y 的值会随着 x 的增加而相应减少,总负责度 O(Q*N)约为 1e6;

 

AC code:

 1 #include <set>
 2 #include <map>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <iostream>
 6 #include <algorithm>
 7 #define INF 0x3f3f3f3f
 8 #define LL long long
 9 using namespace std;
10 const int MAXN = 1e5+10;
11 struct Node
12 {
13     LL x, y;
14 }node[MAXN];
15 LL C[20];
16 bool cmp(Node a, Node b)
17 {
18     if(a.x != b.x) return a.x < b.x;
19     else return a.y > b.y;
20 }
21 LL vis[MAXN];
22 LL vis_sum[MAXN];
23 int N, M, Q;
24 
25 
26 int main()
27 {
28     int T_case;
29     scanf("%d", &T_case);
30     while(T_case--){
31         scanf("%d %d", &N, &M);
32         for(int i = 1;  i <= M; i++){
33             scanf("%lld %lld", &node[i].x, &node[i].y);
34         }
35         scanf("%d", &Q);
36         for(int i = 1; i <= Q; i++){
37             scanf("%lld", &C[i]);
38         }
39         //sort(C+1, C+1+Q);
40 
41 
42         sort(node+1, node+M+1, cmp);
43         LL ty = N, tx = 1;
44         LL cnt = 0, sum = 0, ans = 0;
45         for(int i = 1; i <= Q; i++){
46             cnt = 0; sum = 0; ans = 0;
47             int top = 0;
48             memset(vis, 0, sizeof(vis));
49             memset(vis_sum, 0, sizeof(vis_sum));
50             tx = 1;ty = N;
51             while(tx <= N){
52                 while(top+1 <= M && node[top+1].x <= tx){
53                     top++;
54                     if(node[top].y <= ty){
55                         cnt++;
56                         vis[node[top].y]++;
57                         sum+=node[top].x+node[top].y;
58                         vis_sum[node[top].y]+=tx;
59                         //top++;
60                     }
61                     //else break;
62                     //puts("zjy");
63                 }
64 
65                 while(cnt*(tx+ty)-sum > C[i]){
66                     cnt-=vis[ty];
67                     sum-=(vis_sum[ty]+vis[ty]*ty);
68                     ty--;
69                 }
70                 if(cnt*(tx+ty)-sum == C[i]) ans++;
71                 tx++;
72             }
73 
74             printf("%lld", ans);
75             if(i < Q) printf(" ");
76             else puts("");
77         }
78     }
79     return 0;
80 }

 

【无人机】基于改进粒子群算法的无人机路径规划研究[和遗传算法、粒子群算法进行比较](Matlab代码实现)内容概要:本文围绕基于改进粒子群算法的无人机路径规划展开研究,重点探讨了在复杂环境中利用改进粒子群算法(PSO)实现无人机三维路径规划的方法,并将其与遗传算法(GA)、标准粒子群算法等传统优化算法进行对比分析。研究内容涵盖路径规划的多目标优化、避障策略、航路点约束以及算法收敛性和寻优能力的评估,所有实验均通过Matlab代码实现,提供了完整的仿真验证流程。文章还提到了多种智能优化算法在无人机路径规划中的应用比较,突出了改进PSO在收敛速度和全局寻优方面的优势。; 适合人群:具备一定Matlab编程基础和优化算法知识的研究生、科研人员及从事无人机路径规划、智能优化算法研究的相关技术人员。; 使用场景及目标:①用于无人机在复杂地形或动态环境下的三维路径规划仿真研究;②比较不同智能优化算法(如PSO、GA、蚁群算法、RRT等)在路径规划中的性能差异;③为多目标优化问题提供算法选型和改进思路。; 阅读建议:建议读者结合文中提供的Matlab代码进行实践操作,重点关注算法的参数设置、适应度函数设计及路径约束处理方式,同时可参考文中提到的多种算法对比思路,拓展到其他智能优化算法的研究与改进中。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值