uva 10026 Shoemaker's Problem(排序)

题目连接;10026 Shoemaker's Problem


题目大意:有一个鞋匠接了n双要修的鞋子, 修每双鞋需要d天,每推迟一天修将亏损val元,问按什么样的顺序修鞋可以保证损失最少,如果有多种情况输出字典序最小的。


解题思路:最开始把损失钱数最大的放在前面,后来发现每层子问题是相互有影响的,所以不能从整体的损失来看,所以后来改成对两个鞋的装态比较,只要考虑哪双鞋放前和哪双鞋放后就可以了。


#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int N = 1005;

struct State {
    int id;
    int day;
    int val;
}num[N];

bool cmp(const State &a, const State &b) {
    return a.val * b.day > b.val * a.day;
}

int main() {
    int cas, n;
    scanf("%d", &cas);
    while (cas--) {
	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
	    num[i].id = i + 1;
	    scanf("%d%d", &num[i].day, &num[i].val);
	}
	sort(num, num + n, cmp);
	for (int i = 0; i < n - 1; i++)
	    printf("%d ", num[i].id);
	printf("%d
", num[n -1].id);
	if (cas)
	    printf("
");
    }
    return 0;
}


原文地址:https://www.cnblogs.com/pangblog/p/3278152.html