문제

[UVa - 10026] 구두수선공 문제 (shoemaker's problem)

구코딩 2022. 9. 28. 17:00
반응형

Shoemaker has N jobs (orders from customers) which he must make. Shoemaker can work on only one job in each day. For each i-th job, it is known the integer Ti (1 ≤ Ti ≤ 1000), the time in days it takes the shoemaker to finish the job. For each day of delay before starting to work for the i-th job, shoemaker must pay a fine of Si (1 ≤ Si ≤ 10000) cents. Your task is to help the shoemaker, writing a programm to find the sequence of jobs with minimal total fine.

 

Input

The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs. First line of input contains an integer N (1 ≤ N ≤ 1000). The next N lines each contain two numbers: the time and fine of each task in order.

 

Output

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line. You programm should print the sequence of jobs with minimal fine. Each job should be represented by its number in input. All integers should be placed on only one output line and separated by one space. If multiple solutions are possible, print the first lexicographically.

 

//Input_1.txt
1

4
3 4
1 1000
2 2
5 5


//Output_1.txt
2 1 3 4

 

//Input_2.txt
1

5
2 10
3 100
1 2
2 4
10 1


//Output_2.txt
2 1 3 4 5

 

//Input_3.txt
2

4
3 4
3 4
1 10
5 5

3
7 3
3 7
2 2


//Output_3.txt
3 1 2 4 
2 3 1

 

//Input_4.txt
2

10
10 10
9 9
8 8
7 7
6 6
5 5
4 4
3 3
2 2
1 1

4
2 100
100 2
3 66
2 1


//Output_4.txt
1 2 3 4 5 6 7 8 9 10 
1 3 4 2

 

//Input_5.txt
3

4
1000 1
10 10000
5 5
3 1

8
777 777
777 666
777 333
777 444
444 888
222 666
111 999
1 1

5
3 10
5 8
2 2
1 1
20 2


//Output_5.txt
2 3 4 1 
7 6 5 1 8 2 4 3 
1 2 3 4 5

 

Solution

#define _CRT_SECURE_NO_WARNINGS
#define NUMBER_OF_FILES 5
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int t[1002];
int s[1002];
int priority[1002];

void Algorithm(int i) {
	char Input[5][15] = {
		"Input_1.txt", "Input_2.txt", "Input_3.txt", "Input_4.txt", "Input_5.txt"
	};
	char Output[5][15] = {
		"Output_1.txt", "Output_2.txt", "Output_3.txt", "Output_4.txt", "Output_5.txt"
	};

	printf("input_%d.txt\n\n", i + 1);
	FILE* input;
	input = fopen(Input[i], "r");

	FILE* output;
	output = fopen(Output[i], "w");

	int order = 0;
	char arr[10];
	int number_of_sets, number_of_t;
	int temp;

	if (input != NULL) {
		int a =fscanf(input, "%d", &number_of_sets);
		printf("%d\n\n", number_of_sets);
		for (int i = 0; i < number_of_sets; i++) {
			a = fscanf(input, "%d", &number_of_t);
			printf("%d\n", number_of_t);
			for (int j = 0; j < number_of_t; j++) {
				a = fscanf(input, "%d %d", &t[j], &s[j]);
				priority[j] = j;
				printf("%d %d\n", t[j], s[j]);

			}
			for (int i = 0; i < number_of_t - 1; i++) {
				for (int j = 0; j < number_of_t - 1 - i; j++) {
					if (t[priority[j]] * s[priority[j + 1]] > t[priority[j + 1]] * s[priority[j]]) {
						temp = priority[j];
						priority[j] = priority[j + 1];
						priority[j + 1] = temp;
					}
				}
				
			}
			for (int k = 0; k < number_of_t; k++)
				fprintf(output, "%d ", priority[k] + 1);
			fprintf(output, "\n");
		}

	}
	else {
		printf("Error.");
		exit(1);
	}

	fclose(input);
	fclose(output);
}

int main(void) {
	for (int i = 0; i < NUMBER_OF_FILES; i++) {
		Algorithm(i);
	}
}
반응형

'문제' 카테고리의 다른 글

[백준/BOJ-9251] LCS  (0) 2022.10.24
[UVa-10090] Marbles (jewel box)  (0) 2022.10.10
[UVa-10034] Freckles (Saving ink) 백준 4386  (0) 2022.10.08
[UVa - 116] Unidirectional TSP (The Cheapest Way)  (1) 2022.10.04
[UVa - 10069] Distinct Subsequences  (0) 2022.10.03