forked from sitz/UVa-Online-Judge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path10980.cpp
100 lines (98 loc) · 1.33 KB
/
10980.cpp
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
#include <bits/stdc++.h>
using namespace std;
/*
10980
Lowest Price in Town
*/
#define maxn 202
double C[maxn];
char Fg[maxn];
struct ss
{
double price;
int quantiy;
};
int com(const void *a, const void *b)
{
ss *x = (ss *)a;
ss *y = (ss *)b;
return y->quantiy - x->quantiy;
}
ss Lists[maxn];
int Items;
double Mins;
char input[1000];
void Recur(int n, double price, int lim)
{
int i;
if (price >= Mins)
{
return;
}
if (Fg[n] && price > C[n] && n)
{
if (n >= lim)
{
Mins = C[n];
}
return;
}
Fg[n] = 1;
C[n] = price;
if (n >= lim)
{
Mins = price;
return;
}
for (i = 0; i < Items; i++)
{
Recur(Lists[i].quantiy + n, price + Lists[i].price, lim);
}
}
void Cal()
{
int k;
char *p;
qsort(Lists, Items, sizeof(ss), com);
gets(input);
p = strtok(input, " ");
while (p)
{
k = atoi(p);
p = strtok(NULL, " ");
Mins = 99999999;
Recur(0, 0, k);
printf("Buy %d for $%.2lf\n", k, Mins);
}
}
void Free()
{
int i;
for (i = 0; i <= 200; i++)
{
Fg[i] = 0;
}
}
int main()
{
double p;
int k, kase = 1;
ss t;
while (gets(input))
{
sscanf(input, "%lf%d", &p, &Items);
t.price = p;
t.quantiy = 1;
Lists[0] = t;
for (k = 1; k <= Items; k++)
{
gets(input);
sscanf(input, "%d%lf", &Lists[k].quantiy, &Lists[k].price);
}
Items = k;
printf("Case %d:\n", kase++);
Cal();
Free();
}
return 0;
}