水题,枚举即可.
第一次提交WA,没有注意这道题并不是Special Judge,如果有多组顺序符合要求,需要输出字典序最靠前的一组.
虽然用了string,不过代码写的还是很垃圾.
/*
UVa 102; Ecological Bin Packing
- sqybi's code
*/
//for my winsty
#include
#include
using namespace std;
const int nn = 3;
int t, res, now;
int a[nn][nn];
bool u[nn];
char ch[nn] = {'B', 'G', 'C'};
string ress, nows;
void get(int i)
{
if (i == 3)
{
if (now > res || (now == res && nows < ress))
{
res = now;
ress = nows;
}
return;
}
for (int j = 0; j != 3; ++ j)
if (! u[j])
{
u[j] = true;
now = now + a[i][j];
nows += ch[j];
get(i+1);
nows = nows.erase(i, 1);
now = now - a[i][j];
u[j] = false;
}
}
int main()
{
while (scanf("%d", &a[0][0]) != EOF)
{
t = a[0][0];
for (int i = 0; i != 3; ++ i)
for (int j = 0; j != 3; ++ j)
if (i + j)
{
scanf("%d", &a[i][j]);
t += a[i][j];
}
res = 0;
now = 0;
nows = "";
memset(u, false, sizeof(u));
get(0);
cout << ress << ' ' << t - res << endl;
}
}