NOIP 2012 Day1 T3 开车旅行

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const long long w=1000000000;
struct data{
    int no;
    long long sa,sb;
};
    data f[100005][55];
int n,m,s,p,q;
long long a,b,k;
double z=1e10;
long long h[100005],hf[100005],hs[100005];
int fir[100005],sec[100005];
bool comp(int a,int b)
{
    if (a<=b)  return true;
    return false;
}
void dfs(int x,int t,int w)
{
    if (w%2!=0&&a+b+hs[x]<=t)
    {
        a+=hs[x];
        dfs(sec[x],t,w+1);
    }
    if (w%2==0&&a+b+hf[x]<=t)
    {
        b+=hf[x];
        dfs(fir[x],t,w+1);
    }
    return ;
}
void zzz(int x,int kk)
{
    q=x;
    for (int j=50;j>=0;j--)
        if (f[x][j].no!=0&&f[x][j].sa<=w&&f[x][j].sb<=w&&f[x][j].sa!=0&&f[x][j].sb!=0&&f[x][j].sa+f[x][j].sb<=kk)
        {
            a+=f[x][j].sa;
            b+=f[x][j].sb;
            zzz(f[x][j].no,kk-f[x][j].sa-f[x][j].sb);
            break;
        }
    return ;
}
int main()
{
    freopen ("drive.in","r",stdin);
    //freopen ("drive.out","w",stdout);
    scanf ("%d",&n);
    for (int i=1;i<=n;i++)  scanf ("%lld",&h[i]);
    memset (hf,127,sizeof(hf));
    memset (hs,127,sizeof(hs));
    for (int i=1;i<=n-1;i++)
        for (int j=i+1;j<=n;j++)
        {
            if (hf[i]>abs(h[i]-h[j])||(hf[i]==abs(h[i]-h[j])&&h[j]abs(h[i]-h[j])||(hs[i]==abs(h[i]-h[j])&&h[j]zz||(z==zz&&h[i]>h[p]))
            {
                z=zz;
                p=i;
            }
        }
    }
    printf ("%d\n",p);
    for (int i=1;i<=m;i++)
    {
        scanf ("%d%lld",&s,&k);
        a=0;b=0;
        //dfs(s,k,1);
        //printf ("%lld %lld\n",a,b);
        zzz(s,k);
        if (f[q][0].sa+a+b<=k)  a+=f[q][0].sa;
        printf ("%lld %lld\n",a,b);
    }
    return 0;
}

你可能感兴趣的:(NOIP 2012 Day1 T3 开车旅行)