题目大意:鞋匠有许多鞋要做,但每次只能做一双鞋,别的鞋子在开始之前都需要赔偿延期的费用,给出每个单子需要的天数和延期一天所需要赔的费用,输出赔偿最少的情况的顺序。
解题思路:贪心,考虑只有两双鞋的顺序,若 a b 那么所需费用就是 b 的费用×a 的天数,若 b a 则是 a 的费用×b 的天数,比较两种费用选择费用少的那种顺序。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<stack>
using namespace std;
const int MAXN = 1000+5;
struct node {
int id;
int t;
int s;
}num[MAXN];
bool cmp(node a, node b) {
return a.s * b.t > b.s * a.t;
}
int main() {
int T, n;
scanf("%d", &T);
while (T--) {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
num[i].id = i + 1;
scanf("%d%d", &num[i].t, &num[i].s);
}
sort (num, num+n, cmp);
for (int i = 0; i < n-1; i++) {
printf("%d ", num[i].id);
}
printf("%d\n", num[n-1].id);
if (T) printf("\n");
}
return 0;
}