Codeforces 2015-2016 ACM-ICPC, NEERC, Southern Subregional Contest D题(计算几何+判断线段相交)


D. Boulevard
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Welcoming autumn evening is the best for walking along the boulevard and n people decided to do so.
The boulevard can be represented as the axis Ox. For every person there are three parameters characterizing the behavior: ti, si, fi — the moment of time when the i-th person starts walking, the start point and the end point of the walk respectively. Each person moves in a straight line along the boulevard from si to fi with a constant speed of either 1 or  - 1 depending on the direction.
When the i-th person appears on the boulevard at the point si she immediately starts walking towards the point fi.
If two or more persons meet at the boulevard (they are at the same point at the same time, no matter which directions they are going) they all greet each other. Like in the normal life, every pair of people greet each other at most once.
You task is to calculate for every person how many people she greets while walking along the boulevard.
Please, pay attention to the fact that i-th person may meet and greet any other person at points si and fi. After a person achieves the destination point fi she moves out of the boulevard and cannot greet anyone else. The same rule applies to the start of the walk: a person cannot greet anyone until she appears on the boulevard.
Input
In the first line there is an integer n (2 ≤ n ≤ 1000) — the number of people who decided to go for a walk.
The following n lines contain parameters for n people. In the i-th line there are three positive integers ti, si, fi (1 ≤ ti, si, fi ≤ 106,  si ≠ fi), where ti, si, fi — the moment of time when the i-th person starts walking, the start point and the end point of the walk respectively.
Output
The single line of the output should contain a sequence of n integers r1, r2, ..., rn separated by a space, where ri denotes the number which the i-th person greets other people while walking along the boulevard.
Sample test(s)
Input
3
1 1 10
5 8 2
9 9 10
Output
2 1 1
Input
3
3 2 4
4 3 4
3 6 4
Output
2 2 2

题意:在一个直线上,有n个人,每个人在 t 时刻从s位置出发到f位置,遇到一个没有打过招呼的人,会打招呼。问每个人要打几次招呼

思路:以t为x轴,s为y轴,那么每个人的s-t就是一个线段,就是判断两个线段是不是相交(端点在另一个直线上也算是相交)

判断线段相交一共2步:

一:快速排斥法,简而言之,就是以线段为对角线形成的最小矩形,那么判断两个矩形是否相交,判断矩形相交可以分别判断2个矩形在X,Y轴是否相交。

二:跨立,就是判断一个线段的2个点是不是分布在另一个线段的两侧。这个判断可以用叉乘快速判断。

#include<bits/stdc++.h>
using namespace std;
const int maxn=1001;
const int inf=1<<27;
#define LL long long
#define P pair<int,int>
#define X first
#define Y second
#define pb push_back
#define cl(a,b) memset(a,b,sizeof(a));

struct point{
    double x,y;
    point(){x=y=0;}
    point(double _x,double _y):x(_x),y(_y){}
};
struct seg{//线段
    point a,b;
    seg(){}
    seg(point _a,point _b):a(_a),b(_b){}
};
double cross(const point&a,const point&b,const point&o){//计算叉乘,,oa X ob 如果大于0表示,b在a的逆时针方向
    return (a.x-o.x)*(b.y-o.y)-(a.y-o.y)*(b.x-o.x);
}
seg myseg[maxn];
bool isIntersect(const seg&u,const seg&v){
    return
    (cross(v.a,u.b,u.a)*cross(u.b,v.b,u.a)>=0)&&//判断是不是跨立
    (cross(u.a,v.b,v.a)*cross(v.b,u.b,v.a)>=0)&&
    max(u.a.x,u.b.x)>=min(v.a.x,v.b.x)&&//快速排斥法
    max(v.a.x,v.b.x)>=min(u.a.x,u.b.x)&&
    max(u.a.y,u.b.y)>=min(v.a.y,v.b.y)&&
    max(v.a.y,u.a.y)>=min(u.a.y,v.b.y);
}
int ans[maxn];
int main(){
    int n;
    while(~scanf("%d",&n)){
        for(int i=0;i<n;i++){
            double t,s,f;
            scanf("%lf%lf%lf",&t,&s,&f);
            myseg[i].a=point(t,s);
            if(s<=f){
                myseg[i].b=point(t+f-s,f);
            }
            else {
                myseg[i].b=point(t+s-f,f);
            }
        }
        cl(ans,0);
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++)if(i!=j){
                if(isIntersect(myseg[i],myseg[j]))ans[i]++;
            }
        }
        for(int i=0;i<n;i++){
            printf("%d%c",ans[i],i==n-1?'\n':' ');
        }
    }
    return 0;
}









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CodeForces - 616D是一个关于找到一个序列中最长的第k好子段的起始位置和结束位置的问。给定一个长度为n的序列和一个整数k,需要找到一个子段,该子段中不超过k个不同的数字。目要求输出这个序列最长的第k好子段的起始位置和终止位置。 解决这个问的方法有两种。第一种方法是使用尺取算法,通过维护一个滑动窗口来记录\[l,r\]中不同数的个数。每次如果这个数小于k,就将r向右移动一位;如果已经大于k,则将l向右移动一位,直到个数不大于k。每次更新完r之后,判断r-l+1是否比已有答案更优来更新答案。这种方法的时间复杂度为O(n)。 第二种方法是使用枚举r和双指针的方法。通过维护一个最小的l,满足\[l,r\]最多只有k种数。使用一个map来判断数的种类。遍历序列,如果当前数字在map中不存在,则将种类数sum加一;如果sum大于k,则将l向右移动一位,直到sum不大于k。每次更新完r之后,判断i-l+1是否大于等于y-x+1来更新答案。这种方法的时间复杂度为O(n)。 以上是两种解决CodeForces - 616D问的方法。具体的代码实现可以参考引用\[1\]和引用\[2\]中的代码。 #### 引用[.reference_title] - *1* [CodeForces 616 D. Longest k-Good Segment(尺取)](https://blog.csdn.net/V5ZSQ/article/details/50750827)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces616 D. Longest k-Good Segment(双指针+map)](https://blog.csdn.net/weixin_44178736/article/details/114328999)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值