相遇理解为直接穿过。
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
struct person
{
int f,b;
}p[1005];
bool cmp(struct person a,struct person b)
{
return a.f<b.f;
}
int main()
{
int T;
scanf("%d",&T);
while (T--)
{
int n,f,b;
int i;
int total=0;
scanf("%d%d%d",&n,&f,&b);
bool zeroflag=true;
for (i=0;i<n;i++)
{
scanf("%d%d",&p[i].f,&p[i].b);
if (p[i].b)
zeroflag=false;
}
sort(p,p+n,cmp);
total+=b/n*2*f;
b%=n;
if (!b && zeroflag)
{
total-=2*f;
total+=p[n-1].f+f;
printf("%d\n",total);
continue;
}
else if (!b && !zeroflag)
{
for (i=0;i<n;i++)
if (p[i].b)
break;
total+=f-p[i].f;
printf("%d\n",total);
continue;
}
int counter=0;
for (i=0;i<n && counter<b;i++)
{
if (!p[i].b)
counter++;
if (counter==b)
total+=p[i].f+f;
}
for (i=n-1;i>=0 && counter<b;i--)
{
if (p[i].b)
counter++;
if (counter==b)
total+=(f-p[i].f)+2*f;
}
printf("%d\n",total);
}
}