HDOJ  4302    Holedox Eating

来源:互联网 发布:百度世界人工智能大会 编辑:程序博客网 时间:2024/06/05 06:28

题目:http://acm.hdu.edu.cn/showproblem.php?pid=4302

这是第一场比赛的题,做了好长时间没有做出来,超时啊

以下是同小组的代码,没有用线段树也过了,

我还没有学习线段树,所以用模拟的方法

#include<stdio.h>
#include<string.h>
int b[100015];
int main()
{
    intcount=1;
    int t;
   scanf("%d",&t);
   while(t--)
    {
       int k,a;
       int max=0;
       int fx=1;
       memset(b,0,sizeof(b));
       int first=0;
       int l,n;
       scanf("%d%d",&l,&n);
       for(int i=0;i<n;i++)
       {
           scanf("%d",&a);
           if(a==0)
           {
               scanf("%d",&k);
               b[k]+=1;
           }
           if(a==1)
           {
               int c1=99999999;
               int c2=99999999;
               for(int ppt=first;ppt>=0;ppt--)
               {
                    if(b[ppt]>0)
                   {
                       c1=first-ppt;
                       break;

                   }
               }
               for(int ppt=first;ppt<=l;ppt++)
               {
                   if(b[ppt]>0)
                   {

                       c2=ppt-first;
                       break;
                   }
                   if((ppt-first)>c1)
                   break;
               }
               if(c1==99999999&&c2==99999999)
               continue;
               if(c1>c2)
               {

                   max+=c2;
                   b[first+c2]-=1;
                   fx=1;
                   first=first+c2;
                   continue;
               }
               if(c1<c2)
               {

                   max+=c1;
                   b[first-c1]-=1;
                   fx=-1;
                   first=first-c1;
                   continue;
               }
               if(c1==c2)
               {
                   if(fx==1)
                   {

                       max+=c2;
                       b[first+c2]-=1;
                       fx=1;
                       first=first+c2;
                   }
                   else
                   {
                       max+=c1;
                       b[first-c1]-=1;
                       fx=-1;
                       first=first-c1;
                   }
               }
           }
       }
       printf("Case %d: %d\n",count++,max);
    }
    return0;
}

还有一种代码方式,上面跑的是1921ms,而下面的智勇1296ms就过了

#include<iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace
std;
vector<int> way;
long long
sum =0;
const
int INF= 10000000;
int
vis =0;
void
add()
{

   int
b;
   cin>>b;
   way.insert(lower_bound(way.begin(),way.end(),b),b);
}

int
eat(intx)
{

   if
(way.size()== 2)
       return
x;
   vector<int>::iteratorresult;
    result= lower_bound(way.begin(),way.end(),x);
   int
y =lower_bound(way.begin(),way.end(),x)-way.begin();
   int
a =abs(way[y] -x);
   int
b =abs(x - way[y-1]);
   if
(a==0)
   {

       sum+=a;
       way.erase(result);
       return
x+a;
   }

    elseif
(b==0)
   {

       sum+=b;
       way.erase(result-1);
       return
x-b;
   }

    elseif
(a<b)
   {

       sum+=a;
       vis = 1;
       way.erase(result);
       return
x+a;
   }

    elseif
(a>b)
   {

       sum+=b;
       vis = 2;
       way.erase(result-1);
       return
x-b;
   }

    elseif
(vis==1)
   {

       sum+=a;
       vis = 1;
       way.erase(result);
       return
x+a;
   }

   else

   {

       sum+=b;
       vis = 2;
       way.erase(result-1);
       return
x-b;
    }
}


int
main()
{

   int
L,n;
   int
t;
   int
x = 0;
   scanf("%d",&t);
   for
(int k=1;k<=t;k++)
   {

       scanf("%d%d",&L,&n);
       way.push_back(INF);
       way.push_back(INF);
       for
(int i=1;i<=n;i++)
       {

           int
a;
           scanf("%d",&a);
           if
(a==1)
               x = eat(x);
           else

               add();

       }

       printf("Case %d: %d\n",k,sum);
       sum = 0;
       way.clear();
       x = 0;
   }

   return
0;
}

下面是我写的一直超时的代码,不知道为什么,

请帮忙指导一下,,,,,
*******************************************

#include <stdio.h>
#include <string.h>
#define MIN 1000000
int map[100010];
int locale,L,n;
bool init;
int main()
{
    intT,H,run,sum,temp,i,j,down,up;
   scanf("%d",&T);
    H=1;
   while(T--)
    {
       memset(map,0,sizeof(map));
       sum=0;
       locale=0;
       init=true;
       scanf("%d%d",&L,&n);
       while(n--)
       {
           scanf("%d",&run);
           if(run==0)
               scanf("%d",&temp),map[temp]+=1;
           if(run==1)
           {
               down=MIN,up=MIN;
               for(j=locale;j<=L;j++)
               {
                   if(map[j])
                   {
                       up=j-locale;
                       break;
                   }
               }
               for(i=locale;i>=0;i--)
               {
                   if(map[i])
                   {
                       down=locale-i;
                       break;
                   }
                   if((i-locale)>up)
                       break;
               }
               if(down==MIN&&up==MIN)
                   continue;
               if(up<down)
               {
                   locale+=up;
                   map[locale]--;
                   sum+=up;
                   init=true;
                   continue;
               }
               if(up>down)
               {
                   locale-=down;
                   map[locale]--;
                   sum+=down;
                   init=false;
                   continue;
               }
               if(up==down)
               {
                   if(init)
                   {
                       locale+=up;
                       sum+=up;
                       map[locale]--;
                       continue;
                   }
                   else
                   {
                       locale-=down;
                       sum+=down;
                       map[locale]--;
                       continue;
                   }
               }
           }
       }
       printf("Case %d: %d\n",H++,sum);
    }
    return0;
}

0 0
原创粉丝点击