Pagini recente » Cod sursa (job #3292712) | Cod sursa (job #2671038) | Cod sursa (job #2889337) | Cod sursa (job #3160703) | Cod sursa (job #1865280)
// Ghiozdan.cpp : Defines the entry point for the console application.
//
#include <stdio.h>
unsigned int ghiozdan()
{
FILE *in;
unsigned int noItems, totWeight, i, j;
unsigned int pos, max = 0;
unsigned int inputs[201], trace[201][201], steps[201];
in = fopen("ghiozdan.in", "r");
if (!fscanf(in, "%u %u", &noItems, &totWeight))
{
return -1;
}
for (i = 0; i < 201; ++i)
{
steps[i] = 0;
inputs[i] = 0;
}
for (i = 0; i < noItems; ++i)
{
if (!fscanf(in, "%u", &j))
return -1;
++inputs[j];
}
for (i = 0; i < 201; ++i)
for (j = 0; j < 201; ++j)
trace[i][j] = inputs[j];
for (i = 0; i < 201;++i)
if (inputs[i] != 0)
{
steps[i] = 1;
--trace[i][i];
if ( i <= totWeight)
max = i;
}
for (pos = 0; pos < totWeight; ++pos)
{
if (steps[pos%201] != 0)
{
for (i = 1; i < 201;++i)
if (trace[pos % 201][i] != 0)
{
if ((steps[(pos + i) % 201] == 0) || (steps[(pos + i) % 201] > steps[pos % 201] + 1))
{
steps[(pos + i) % 201] = steps[pos % 201] + 1;
for (j = 0; j < 201; ++j)
trace[(pos + i) % 201][j] = trace[pos % 201][j];
--trace[(pos + i) % 201][i];
if ((pos + i > max) && (pos + i <= totWeight))
max = pos + i;
}
}
}
}
FILE *out;
out = fopen("ghiozdan.out", "w");
fprintf(out, "%u %u", max, steps[max % 201]);
/*for (i = 0; i < 201; ++i)
{
for (j = 0; j < inputs[i] - trace[max % 201][i]; ++j)
fprintf(out, "\n%u", i);
}*/
fclose(out);
fclose(in);
return 0;
}
int main(int argc, char* argv[])
{
(void) ghiozdan();
return 0;
}