[UVA 10026] Shoemaker’s Problem

2017年9月7日

题意是鞋匠有 n 个任务。每个任务有一个完成所需时间day。和每过一天要陪的钱数 fine。要求一个任务顺序使得赔钱最少。

贪心,按 fine / day (即单位时间内赔钱数)对任务进行从大到小排序,然后直接输出排序后的任务编号即可。

#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
struct edge {
  int t, c, id;
  bool operator < (const edge &a) const {
    return c * a.t  > a.c * t || c * a.t == a.c * t && id < a.id;
  }
} e[N];
int main()
{
  int n, t;
  scanf("%d", &t);
  while (t--) {
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
      scanf("%d %d", &e[i].t, &e[i].c);
      e[i].id = i + 1;
    }
    sort(e, e + n);
    for (int i = 0; i < n; i++) {
      printf("%d", e[i].id);
      if (i != n - 1)
        putchar(' ');
    }
    puts("");
    if (t)
      puts("");
  }
  return 0;
}

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注