-
Notifications
You must be signed in to change notification settings - Fork 0
/
store-credit.c
141 lines (103 loc) · 2.66 KB
/
store-credit.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <errno.h>
FILE *stream;
struct item_price {
int price;
int index;
};
static int get_int_from_stdin(int *i) {
int r;
assert(i);
r = scanf("%d", i);
if (r != 1)
return -errno;
return r;
}
static int load_test_case(int *credit, int *num_items, int **prices) {
int r, i, *p;
assert(credit);
assert(num_items);
assert(prices);
r = get_int_from_stdin(credit);
if (r != 1)
return -errno;
r = get_int_from_stdin(num_items);
if (r != 1)
return -errno;
p = calloc(*num_items, sizeof(*p));
if (!p)
return -ENOMEM;
for (i = 0; i < *num_items; ++i) {
int price;
r = get_int_from_stdin(&price);
if (r != 1) {
free(p);
return -errno;
}
p[i] = price;
}
*prices = p;
return 0;
}
static int cmp_prices(const void *p1, const void *p2) {
int left = ((const struct item_price *) p1)->price, right = ((const struct item_price *) p2)->price;
return ((left > right) - (left < right));
}
static int cmp_price_credit(const void *key, const void *elem) {
int price = ((const struct item_price *) elem)->price, credit = *(const int *) key;
return credit < price ? -1 : credit > price ? 1 : 0;
}
static void solve(int case_num, int credit, int num_items, int *prices) {
int i;
struct item_price *price_list;
assert(prices);
price_list = calloc(num_items, sizeof(*price_list));
if (!price_list)
return;
for (i = 0; i < num_items; ++i) {
price_list[i].price = prices[i];
price_list[i].index = i;
}
/* FIXME: qsort may or may not be stable, use stable sorting algorithm instead */
qsort(price_list, num_items, sizeof(*price_list), cmp_prices);
for (i = 0; i < num_items; ++i) {
struct item_price *e;
int c;
c = credit - prices[i];
e = bsearch(&c, price_list, num_items, sizeof(*price_list), cmp_price_credit);
if (e && e->index != i) {
assert(e->price + prices[i] == credit);
fprintf(stream, "Case #%d: %d %d\n", case_num, i + 1, e->index + 1);
fflush(stream);
break;
}
}
}
int main(void) {
int i, r, test_cases, credit, num_items, *prices = NULL;
char *stream_buf = NULL;
size_t size;
stream = open_memstream(&stream_buf, &size);
r = get_int_from_stdin(&test_cases);
if (r < 0) {
fprintf(stderr, "Failed to determine number of test cases\n");
return EXIT_FAILURE;
}
for (i = 0; i < test_cases; ++i) {
free(prices);
prices = NULL;
r = load_test_case(&credit, &num_items, &prices);
if (r < 0) {
free(stream_buf);
fprintf(stderr, "Failed to parse test cases from stdin\n");
return EXIT_FAILURE;
}
solve(i + 1, credit, num_items, prices);
}
free(prices);
printf("%s", stream_buf);
free(stream_buf);
return 0;
}