Shoemaker's Problem

Shoemaker's Problem

时间: 1ms        内存:64M

描述:

Shoemaker has N jobs (orders from customers) which he must make. Shoemaker can
work on only one job in each day. For each ith job, it is known the integer Ti (1<=Ti<=1000), the time in days it takes the shoemaker to finish the job.
For each day of delay before starting to work for the ith job, shoemaker must pay a fine of
Si (1<=Si<=10000) cents. Your task is to help the shoemaker, writing a programm to find the sequence of jobs with minimal total fine.

输入:

The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.

First line of input contains an integer N (1<=N<=1000). The next N lines each contain two numbers: the time and fine of each
task in order.

输出:

For each test case, your program should print the sequence of jobs with minimal fine. Each job should be represented by its position in the input. All integers should be placed on only one output line and each pair separated by one space. If multiple solutions are possible, print the first one in lexicographic order.

The output of two consecutive cases must be separated by a blank line.

示例输入:

1

4
3 4
1 1000
2 2
5 5

示例输出:

2 1 3 4

提示:

参考答案(内存最优[1104]):

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int aa[1000];
void sortt(double *f,int n)
{
    for(int i=0; i<n; i++)
        for(int j=0; j<n-i-1; j++)
        {
            if(f[j]<f[j+1])
            {
                double a=f[j];
                f[j]=f[j+1];
                f[j+1]=a;
                int b=aa[j];
                aa[j]=aa[j+1];
                aa[j+1]=b;
            }
        }
}
int main()
{
    int n;
    scanf("%d",&n);
    while(n--)
    {
        int s;
        scanf("%d",&s);
        double f[s],a,b;
        for(int i=0; i<s; i++)aa[i]=i;
        for(int i=0; i<s; i++)
        {
            scanf("%lf%lf",&a,&b);
            f[i]=b/a;
        }
        sortt(f,s);
        for(int i=0; i<s; i++)
            printf(i!=s-1?"%d ":"%d\n\n",aa[i]+1);
    }
    return 0;
}

参考答案(时间最优[0]):

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
struct job
{
	int count;//记录位置
	int s;//交付的钱
	int t;//时间
}shoemaker[1001];
bool cmp(struct job a,struct job b)
{
	return a.s*b.t>a.t*b.s;
}
int main()
{
	int T,n,i;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d",&n);
		for(i=1;i<=n;i++)
		{
			scanf("%d%d",&shoemaker[i-1].t,&shoemaker[i-1].s);
			shoemaker[i-1].count=i;
		}
		sort(shoemaker,shoemaker+n,cmp);
		for(i=0;i<n;i++)
		{
			printf("%d",shoemaker[i].count);
			if(i!=n-1)
				printf(" ");
			if(i==n-1)
				printf("\n");
		}
		if(T)
			printf("\n");
	}
	return 0;
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

点赞

发表评论

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