题意:N个单元的文件,Boss要求剩下M个,其余的要你完成。但是在deadline之前你完不成所以你要雇佣机构来帮你。L个机构,每个机构都可以分A、B两种方案,A是帮你完成一个单元,B是帮你完成剩下的一半。输出每个机构的最小花费。注意rounding down是四舍五入的意思。
方法:贪心计算就行,循环结束的条件是(1) temp_N - (temp_N+1)/2 >= M,(2) agen[i].B < (temp_N+1)/2 * agen[i].A。注意这两个就行,下边的WA代码也是这个思路但是wa.. 不知道原因。
AC代码:
#include <iostream>
#include <iomanip>
#include <string>
#include <cstring>
#include <cstdio>
#include <queue>
#include <stack>
#include <algorithm>
#include <cmath>
#include <ctime>
using namespace std;
const int maxn_f = 100000+10;
const int maxn_a = 100+10;
struct Agency
{
char name[20];
int A;
int B;
int cost;
};
Agency agen[maxn_a];
void Handle_Input(char temp[], int i)
{
int j = 0, k = 0;
char temp1[50];
memset(temp1, '\0', sizeof(temp1));
for (j = 0; temp[j] != ':'; j++)
agen[i].name[k++] = temp[j];
for (k = 0, j++; temp[j] != ','; j++)
temp1[k++] = temp[j];
agen[i].A = atoi(temp1);
memset(temp1, '\0', sizeof(temp1));
for (k = 0, j++; j < strlen(temp); j++)
temp1[k++] = temp[j];
agen[i].B = atoi(temp1);
}
int cmp(const void *a, const void *b)
{
struct Agency *c = (Agency *)a;
struct Agency *d = (Agency *)b;
if ((*c).cost != (*d).cost)
return (*c).cost - (*d).cost;
else
return strcmp((*c).name, (*d).name);
}
int main()
{
#ifdef Local
freopen("a.txt", "r", stdin);
#endif
int t = 0;
cin >> t;
for (int kases = 1; kases <= t; kases++)
{
memset(agen, 0, sizeof(agen));
int N = 0, M = 0, L = 0;
int i = 0;
cin >> N >> M >> L;
for (i = 0; i < L; i++)
{
char temp[50];
cin >> temp;
Handle_Input(temp, i);
int temp_N = N;
while (temp_N - (temp_N+1)/2 >= M && agen[i].B < (temp_N+1)/2 * agen[i].A)
{
agen[i].cost += agen[i].B;
temp_N -= (temp_N+1) / 2;
}
agen[i].cost += (temp_N-M) * agen[i].A;
}
qsort(agen, L, sizeof(agen[0]), cmp);
cout << "Case " << kases << endl;
for (i = 0; i < L; i++)
cout << agen[i].name << " " << agen[i].cost << endl;
}
return 0;
}
WA代码:
#include <iostream>
#include <iomanip>
#include <string>
#include <cstring>
#include <cstdio>
#include <queue>
#include <stack>
#include <algorithm>
#include <cmath>
#include <ctime>
using namespace std;
const int maxn_f = 100000+10;
const int maxn_a = 100+10;
struct Agency
{
char name[20];
int A;
int B;
int cost;
};
Agency agen[maxn_a];
void Handle_Input(char temp[], int i)
{
int j = 0, k = 0;
char temp1[50];
memset(temp1, '\0', sizeof(temp1));
for (j = 0; temp[j] != ':'; j++)
agen[i].name[k++] = temp[j];
for (k = 0, j++; temp[j] != ','; j++)
temp1[k++] = temp[j];
agen[i].A = atoi(temp1);
memset(temp1, '\0', sizeof(temp1));
for (k = 0, j++; j < strlen(temp); j++)
temp1[k++] = temp[j];
agen[i].B = atoi(temp1);
}
int cmp(const void *a, const void *b)
{
struct Agency *c = (Agency *)a;
struct Agency *d = (Agency *)b;
if ((*c).cost != (*d).cost)
return (*c).cost - (*d).cost;
else
return strcmp((*c).name, (*d).name);
}
int main()
{
#ifdef Local
freopen("a.txt", "r", stdin);
#endif
int t = 0;
cin >> t;
for (int kases = 1; kases <= t; kases++)
{
memset(agen, 0, sizeof(agen));
double N = 0, M = 0, L = 0;
int i = 0;
cin >> N >> M >> L;
for (i = 0; i < L; i++)
{
char temp[50];
cin >> temp;
Handle_Input(temp, i);
double temp_N = N;
while (true)
{
if (temp_N / 2 < M)
{
agen[i].cost += (temp_N-M) * agen[i].A;
break;
}
else if ((agen[i].B / (int)(temp_N/2+0.5)) < agen[i].A)
{
agen[i].cost += agen[i].B;
temp_N = temp_N - (int)(temp_N/2+0.5);
}
else
{
agen[i].cost += (temp_N-M) * agen[i].A;
break;
}
}
}
qsort(agen, L, sizeof(agen[0]), cmp);
cout << "Case " << kases << endl;
for (i = 0; i < L; i++)
cout << agen[i].name << " " << agen[i].cost << endl;
}
return 0;
}