UVa 10026 - Shoemaker's Problem

Contents

  1. 1. Problem
  2. 2. Solution
  3. 3. Code

Problem

題目網址
中文網址

依照工作天和罰金,判斷要先做哪個會使的罰金最少?

Solution

直接將 罰金除以天數 ,然後排序,高的先做即可。(注意相同時的處理)

Code

UVa 10026UVa 10026 - Shoemaker's Problem
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
#include<cstdio>
#include<algorithm>

struct Shoe
{
int id, d, p;
float cp;
}shoe[1001];

int main()
{
int Case;
scanf("%d", &Case);
while (Case--)
{
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
scanf("%d%d", &shoe[i].d, &shoe[i].p);
shoe[i].cp = (float)shoe[i].p / shoe[i].d;
shoe[i].id = i;
}
std::sort(shoe + 1, shoe + n + 1,
[](const Shoe& a, const Shoe& b)->bool
{
if (a.cp == b.cp)
return a.id < b.id;
else return a.cp > b.cp;
});

for (int i = 1; i < n; i++)
printf("%d ", shoe[i].id);
printf("%d\n", shoe[n].id);
if (Case)
putchar('\n');
}

return 0;
}