hdu 4128 Running relay ——2011ACM福州赛区H题

我用的是一种贪心算法,证明很烦,先证明每个人跑完d后要取得最优最多让剩下的两个人跑,如果选3个人或3个以上的话可以通过转换他们跑的距离来证明这种方法不是最优的,所以一定是2个人!因为我每次都是通过 ex/ey这个值来选人,还可以证明选的这两个人的ex/ey的值在所有ex/ey中一定相邻,如果不相邻还可以得出矛盾!这样就存在一个O(n)的枚举算法,不过以下算法是O(n2)的,因为是昨晚写的。。。当时还没想的这么彻底

step 1:排序,状态好的情况的速度记为 x,状态差的速度记为 y,以x为主y为辅排序;

step 2:删点,若某个 yi<yj ,则删去 j 这个点;

step 3: 让每个人都先跑 d 的距离;

step 4:贪心,让x最小的点先跑完全程,然后所得到的 s 如果小于w则这种结果是最优的,若大于w则 goto step 5;

step 5:算 ex/ey ,和这个x最小的点交换,看len*y[?]是否小于 w,小于的话则可以计算出结果,否则 goto step 4;

#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<cmath>
#include<cassert>
#include<cstring>
#include<iomanip>
using namespace std;

#ifdef _WIN32
#define i64 __int64
#define out64 "%I64d\n"
#define in64 "%I64d"
#else
#define i64 long long
#define out64 "%lld\n"
#define in64 "%lld"
#endif

#define FOR(i,a,b)      for( int i = (a) ; i <= (b) ; i ++)
#define FF(i,a)         for( int i = 0 ; i < (a) ; i ++)
#define FFD(i,a)        for( int i = (a)-1 ; i >= 0 ; i --)
#define S64(a)          scanf(in64,&a)
#define SS(a)           scanf("%d",&a)
#define LL(a)           ((a)<<1)
#define RR(a)           (((a)<<1)+1)
#define SZ(a)           ((int)a.size())
#define PP(n,m,a)       puts("---");FF(i,n){FF(j,m)cout << a[i][j] << ' ';puts("");}
#define pb              push_back
#define CL(Q)           while(!Q.empty())Q.pop()
#define MM(name,what)   memset(name,what,sizeof(name))
#define read            freopen("in.txt","r",stdin)
#define write           freopen("out1.txt","w",stdout)

const int inf = 0x3f3f3f3f;
const i64 inf64 = 0x3f3f3f3f3f3f3f3fLL;
const double oo = 10e9;
const double eps = 10e-10;
const double pi = acos(-1.0);
const int maxn = 11234;
struct zz
{
    i64 x;
    i64 y;
    bool operator < (const zz & cmp) const
    {
        if(x!=cmp.x)
        {
            return x<cmp.x;
        }
        else
        {
            return y<cmp.y;
        }
    }
}zx;

int t;
i64 n,d,len,w,mm;
i64 y[maxn];
i64 x[maxn];
vector<zz>vc;
vector<zz>v;
double c[maxn];
double ex,ey,add;

double start()
{
    double nowx = v[0].x * len;
    double nowy = v[0].y * len;
    i64 nb = v[0].y *len;
    if(nb <= w)
    {
        return nowx;
    }
    i64 tx=0;
    double have = len;
    double temp;
    double to;

    while(true)
    {
        int ti;
        double tc=oo;
        for(int i=tx+1;i<v.size();i++)
        {
            ex = v[i].x - v[tx].x;
            ey = v[tx].y - v[i].y;
            c[i] = ex/ey;
            if(c[i]<tc)
            {
                ti = i;
                tc = c[i];
            }
        }
        if( (v[ti].y * len) <= w )
        {
            to = (nowy - w)/double(v[tx].y - v[ti].y) * double(v[ti].x - v[tx].x);
            nowx += to;
            return nowx;
        }
        else
        {
            nowx = len*v[ti].x;
            nowy = len*v[ti].y;
            tx = ti;
        }
        continue;
    }
}

int main()
{
    cin>>t;
    while(t--)
    {
        mm=inf;
        v.clear();
        vc.clear();
        cin>>n>>d>>len>>w;
        for(int i=1;i<=n;i++)
        {
            cin>>y[i]>>x[i];
            mm=min(mm,y[i]);
            zx.x=x[i];
            zx.y=y[i];
            vc.push_back(zx);
        }
        sort(vc.begin(),vc.end());
        i64 ty=vc[0].y;

        v.push_back(vc[0]);
        for(int i=1;i<vc.size();i++)
        {
            if(vc[i].y >= ty)
            {
                continue;
            }
            else
            {
                ty = vc[i].y;
                v.push_back(vc[i]);
            }
        }
        if(n*d > len)
        {
            cout<<"No solution"<<endl;
            continue;
        }
        i64 temp=0;
        add = 0.0;
        FOR(i,1,n)
        {
            temp += y[i]*d;
            add += x[i]*d;
        }
        w -= temp;
        if(w < 0)
        {
            cout<<"No solution"<<endl;
            continue;
        }
        len -= n*d;
        if(mm*len > w)
        {
            cout<<"No solution"<<endl;
            continue;
        }
        printf("%.2lf\n",start()+add);
    }
    return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值