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

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

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

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

#include<stdio.h>
#include<string.h>
int b[100015];
int main()
{
int count=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;
}
}
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);
}
return 0;
}还有一种代码方式,上面跑的是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()
{ b;
cin>>b;
way.insert(lower_bound(way.begin(),way.end(),b),b);
} int eat(int x)
{ (way.size() == 2) x;
vector<int>::iterator result;
result = lower_bound(way.begin(),way.end(),x); y = lower_bound(way.begin(),way.end(),x)-way.begin(); a = abs(way[y] - x); b = abs(x - way[y-1]); (a==0)

sum+=a;
way.erase(result); x+a;
(b==0)

sum+=b;
way.erase(result-1); x-b;
(a<b)

sum+=a;
vis = 1;
way.erase(result); x+a;
(a>b)

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

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

sum+=b;
vis = 2;
way.erase(result-1); x-b;
}
}
int main()
{ L,n; t; x = 0;
scanf("%d",&t); (int k=1;k<=t;k++)

scanf("%d%d",&L,&n);
way.push_back(INF);
way.push_back(INF); (int i=1;i<=n;i++)
a;
scanf("%d",&a); (a==1)
x = eat(x);
add();


printf("Case %d: %d\n",k,sum);
sum = 0;
way.clear();
x = 0;
0;
}下面是我写的一直超时的代码,不知道为什么,
请帮忙指导一下,,,,,
*******************************************#include <stdio.h>
#include <string.h>
#define MIN 1000000
int map[100010];
int locale,L,n;
bool init;
int main()
{
int T,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);
}
return 0;
}