小兴奋啊,竟然是1Y,(*^__^*) 嘻嘻。这个题目的意思是让你单独算用每一个agency所需的费用,
For
$A
they will reduce your paperwork by one unit.
For $B they will reduce your entire paperwork by half (rounding down when necessary).有这两种方案,在剩余不用的工作量的情况下判断一下哪一个用的钱少就可以了。rounding down 是四舍五入的意思。
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
using namespace std;
typedef struct agen
{
char name[20];
int cost,A,B;
} agen;
int cmp(const void *_a,const void *_b)
{
agen *a=(agen *)_a;
agen *b=(agen *)_b;
if((*a).cost==(*b).cost)
return strcmp((*a).name,(*b).name);
else return (*a).cost-(*b).cost;
}
int main()
{
//freopen("in.txt","r",stdin);
int cs,M,N,L,MM,NN;
char str[100];
agen agency[109];
scanf("%d",&cs);
for(int cas=1; cas<=cs; cas++)
{
scanf("%d%d%d",&MM,&NN,&L);
getchar();
for(int i=0; i<L; i++)
{
agency[i].cost=0;
M=MM;
N=NN;
gets(str);
{
int num=0,j;
for( j=0; str[j]!=':'; j++)
agency[i].name[num++]=str[j];
agency[i].name[num]='\0';
num=0;
for(j++; str[j]!=','; j++)
num=num*10+str[j]-'0';
agency[i].A=num;
num=0;
for(j++; str[j]!='\0'; j++)
num=num*10+str[j]-'0';
agency[i].B=num;
}
while(1)
{
if(M==N) break;
int rest=M/2;
if(agency[i].B<(M-rest)*agency[i].A&&rest>=N)
{
M=rest;
agency[i].cost+=agency[i].B;
}
else
{
M--;
agency[i].cost+=agency[i].A;
}
}
}
qsort(agency,L,sizeof(agency[0]),cmp);
cout<<"Case "<<cas<<endl;
for(int i=0; i<L; i++)
cout<<agency[i].name<<" "<<agency[i].cost<<endl;
}
return 0;
}