i代表田忌最慢的马在数组中的位置,end代表田忌最快的马的位置,max齐威王最快的马的位置,min齐威王最慢的马的位置
从i和min开始比较
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int cmp(int a, int b)
{
return a>b;
}
int main()
{
int n,i,max,min,end,ans,hor1[1000],hor2[1000];
while(scanf("%d",&n)!=EOF && n!=0)
{
for(i=0;i<n;i++)
scanf("%d",&hor1[i]);
for(i=0;i<n;i++)
scanf("%d",&hor2[i]);
sort(hor1,hor1+n,cmp);
sort(hor2,hor2+n,cmp);
ans=0;
max=0;
min=n-1;
end=0;
for(i=n-1;i>=end;i--)
{
if(hor1[i]>hor2[min]) //能赢直接比
{
ans+=200;
min--;
}
else if(hor1[i]<hor2[min]) // 不能赢和齐威王最快的比,max++
{
ans-=200;
max++;
}
else //相等转到没比的最快的马
{
for(;max<=min;end++,max++)
{
if(hor1[end]>hor2[max]) //能赢end++,max++,i--继续比
ans+=200;
else
{
if(hor1[i]<hor2[max]) //不能赢或平 max++,i--回到i,min处
ans-=200;
max++;
break;
}
}
}
if(max>min)
break;
}
printf("%d\n",ans);
}
}