hihoCoder1090 Highway

本文提供了一个使用C++进行编程竞赛的示例代码,主要介绍了如何通过结构体存储节点信息,并利用排序算法来解决特定问题。代码中包含了对结构体的定义、排序条件的设定及关键算法的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<sstream>
#include<string>
#include<bitset>
using namespace std;

typedef long long LL;

const LL LINF = (1LL <<63);
const int INF = 1 << 31;


const int NS = 1010;
const int MS = 19;
const int MOD = 1000000007;
const double eps = 1e-8;

bool isequal(double xa, double xb)
{
    return bool(fabs(xa - xb) < eps);
}

struct node{
    int id;
    double x,y,v;

    void print()
    {
        printf("ID:%2d (%.2lf, %.2lf, %.2lf)\n", id, x, y, v);
    }
}c[NS];

node p[NS];
double t[NS];
double ans[NS];
int n;


bool cmpx(node ca, node cb)
{
    return ca.x > cb.x;
}

bool cmpy(node ca, node cb)
{
    if(!isequal(ca.y, cb.y))
        return ca.y < cb.y;
    return ca.x > cb.x;
}

int getPos(double* p, int st, int len)
{
    return 0;
}

int main()
{

#ifndef ONLINE_JUDGE
//    freopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);
#endif

    int nCase = 0;

    while(~scanf("%d", &n))
    {
        for(int i = 0; i < n; i++)
        {
            c[i].id = i;
            scanf("%lf %lf %lf", &c[i].x, &c[i].y, &c[i].v);
            p[i].id = c[i].id;
            p[i].x = c[i].x;
            p[i].y = c[i].y;
            p[i].v = c[i].v;

            t[i] = 0;
        }
        sort(p, p+n, cmpy);
        sort(c, c+n, cmpx);

        double curpos;
        for(int i = 0; i < n; i++)
        {
            double v = c[i].v;
            curpos = c[i].x;

            double ti = 0;
            for(int j = 0; j < n; j++)
            {
                if(p[j].y <= c[i].x) continue ;

                ti += (p[j].y - curpos) / v;
                ti = max(t[j], ti);
                t[j] = ti;
                //t[j]记录位置大于等于x[i]的点到达p[j].y的时间最大值

                if(p[j].id == c[i].id)
                {
                    ans[c[i].id] = t[j];
                    break;
                }
                curpos = p[j].y;
            }
        }
 //       printf("case:%d\n", ++nCase);
        for(int i = 0; i < n; i++)
        {
            printf("%.2lf\n", ans[i]);
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值