소스코드 ¶
~cpp
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;
#define TRUE 1
#define FALSE 0
const char DEBUG_INPUT[] = "4 5\n4 5 3 5\n3 5 2 6 4\n4 5\n4 5 3 5\n3 5 2 6 3\n0 0\n";
struct SNumberAndPosition
{
SNumberAndPosition(int inputNumber, int inputPosition)
{
number = inputNumber;
position = inputPosition;
}
int number;
int position;
};
char* InputBaseData(char* readData, vector<SNumberAndPosition>& tableSize, vector<SNumberAndPosition>& teamSize)
{
int numberOfTable = 0;
int numberOfTeam = 0;
int buffer;
sscanf(readData, "%d %d", &numberOfTeam, &numberOfTable);
readData = strchr(readData, '\n') + 1;
if (0 == numberOfTeam && 0 == numberOfTable)
return NULL;
for (register int i = 0; i < numberOfTeam; ++i)
{
if (0 != i)
readData = strchr(readData, ' ') + 1;
sscanf(readData, "%d", &buffer);
teamSize.push_back(SNumberAndPosition(buffer, i));
}
readData = strchr(readData, '\n') + 1;
for (register int i = 0; i < numberOfTable; ++i)
{
if (0 != i)
readData = strchr(readData, ' ') + 1;
sscanf(readData, "%d", &buffer);
tableSize.push_back(SNumberAndPosition(buffer, i));
}
return strchr(readData, '\n') + 1;
}
bool DeSort(SNumberAndPosition one, SNumberAndPosition another)
{
return one.number > another.number;
}
void CalculateAndPrintResult(vector<SNumberAndPosition>& tableSize, vector<SNumberAndPosition>& teamSize)
{
//// 테이블에 앉을 수 있는 사람수보다 팀의 사람수가 많을때 ////
int sumTable;
int sumTeamSIze;
for (register int i = 0; i < (int)tableSize.size(); ++i)
sumTable += tableSize[i].number;
for (register int i = 0; i < (int)teamSize.size(); ++i)
sumTeamSIze += teamSize[i].number;
if (sumTeamSIze > sumTable)
{
cout << "0" << endl;
return ;
}
sort(tableSize.begin(), tableSize.end(), DeSort);
sort(teamSize.begin(), teamSize.end(), DeSort);
//// 가장 많은 팀원수보다 테이블수가 적을때 ////
if (teamSize[0].number < (int)tableSize.size())
{
cout << "0" << endl;
return ;
}
//// 연산을 수행한다. ////
vector< vector<int> > teamTableNumber;
teamTableNumber.resize(teamSize.size());
for (register int i = 0; i < (int)teamSize.size(); ++i)
{
if (teamSize[i].number > (int)tableSize.size())
{
cout << "0" << endl;
return ;
}
for (register int j = 0; j < (int)tableSize.size(); ++j)
{
teamTableNumber[teamSize[i].position].push_back(tableSize[j].position);
--tableSize[j].number;
}
sort(tableSize.begin(), tableSize.end(), DeSort);
while (0 == tableSize[(int)tableSize.size() - 1].number)
tableSize.pop_back();
}
//// 결과를 출력한다. ////
cout << "1" << endl;
for (register int i = 0; i < (int)teamTableNumber.size(); ++i)
{
sort(teamTableNumber[i].begin(), teamTableNumber[i].end());
for (register int j = 0; j < (int)teamTableNumber[i].size(); ++j)
{
cout << teamTableNumber[i][j] + 1 << " ";
}
cout << endl;
}
}
void main()
{
char* inputString = new char[strlen(DEBUG_INPUT) + 1];
strcpy(inputString, DEBUG_INPUT);
vector<SNumberAndPosition> teamSize;
vector<SNumberAndPosition> tableSize;
while (1)
{
inputString = InputBaseData(inputString, tableSize, teamSize);
if (NULL == inputString)
break;
CalculateAndPrintResult(tableSize, teamSize);
teamSize.clear();
tableSize.clear();
}
delete[] inputString;
}