UVA10670 POJ1907 ZOJ2372 Work Reduction贪心

Posted 海岛Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA10670 POJ1907 ZOJ2372 Work Reduction贪心相关的知识,希望对你有一定的参考价值。

Work Reduction
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 1784 Accepted: 631

Description

Paperwork is beginning to pile up on your desk, and tensions at the workplace are starting to mount. Your boss has threatened to fire you if you don’t make any progress by the end of the day. You currently have N units of paperwork on your desk, and your boss demands that you have exactly M units of paperwork left by the end of the day.
The only hope for you now is to hire help. There are various agencies which offer paperwork reduction plans:

For $A they will reduce your paperwork by one unit.
For $B they will reduce your entire paperwork by half (rounding down when necessary).

Note that work can never be reduced to less than 0.

Your task now is to produce a sorted table of agency names and their respective minimum costs to solve your workload problem.

Input

The first line of input consists of a single positive integer representing the number of cases to follow. Each case begins with three positive integers separated by spaces: N - your starting workload, M - your target workload, and L - the number of work reduction agencies available to you, (1 <= M <= N <= 100000, 1 <= L <= 100). The next L lines have the format “[agency name]:A,B”, where A and B are the rates as described above for the given agency. (0 <= A,B <= 10000) The length of the agency name will be between 1 and 16, and will consist only of capital letters. Agency names will be unique.

Output

For each test case, print “Case X”, with X being the case number, on a single line, followed by the table of agency names and their respective minimum costs, sorted in non-decreasing order of minimum costs. Sort job agencies with identical minimum costs in alphabetical order by agency name. For each line of the table, print out the agency name, followed by a space, followed by the minimum required cost for that agency to solve your problem.

Sample Input

2
100 5 3
A:1,10
B:2,5
C:3,1
1123 1122 5
B:50,300
A:1,1000
C:10,10
D:1,50
E:0,0

Sample Output

Case 1
C 7
B 22
A 37
Case 2
E 0
A 1
D 1
C 10
B 50

Source

Waterloo local 2004.06.12

问题链接UVA10670 POJ1907 ZOJ2372 Work Reduction
问题简述:(略)
问题分析:贪心问题,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA10670 POJ1907 ZOJ2372 Work Reduction */

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>

using namespace std;

const int L = 100;
struct Node {
    char name[20];
    int a, b, cost;
} age[L];

int getcost(int sum, int d, Node age)
{
    int ans = 0;
    while (sum > d) {
        if (sum / 2 < d)
            ans += (sum - d) * age.a, sum = d;
        else if ((sum - sum / 2) * age.a > age.b)
            ans += age.b, sum /= 2;
        else
            ans += (sum - d) * age.a, sum = d;
    }
    return ans;
}

bool cmp(Node a, Node b)
{
    return a.cost == b.cost ? strcmp(a.name, b.name) < 0 : a.cost < b.cost;
}

int main()
{
    int t, n, m, l, caseno = 0;
    scanf("%d", &t);
    while (t--) {
        scanf("%d%d%d\\n", &n, &m, &l);
        string s;
        for (int i = 0; i < l; i++) {
            getline(cin, s);

            int j;
            for (j = 0; s[j] != ':'; j++)
                age[i].name[j] = s[j];
            age[i].name[j++] = '\\0';

            age[i].a = 0;
            for (; s[j] != ','; j++)
                age[i].a = age[i].a * 10 + s[j] - '0';

            age[i].b = 0;
            for(j++; s[j]; j++)
                age[i].b = age[i].b * 10 + s[j] - '0';

            age[i].cost = getcost(n, m, age[i]);
        }

        sort(age, age + l, cmp);

        printf("Case %d\\n", ++caseno);
        for (int i = 0; i < l; i++)
            printf("%s %d\\n", age[i].name, age[i].cost);
    }

    return 0;
}

以上是关于UVA10670 POJ1907 ZOJ2372 Work Reduction贪心的主要内容,如果未能解决你的问题,请参考以下文章

UVA542 POJ2261 ZOJ1950 France ‘98概率

UVA542 POJ2261 ZOJ1950 France ‘98概率

UVA10081 POJ2537 ZOJ1883 Tight WordsDP

UVA10277 POJ2646 ZOJ1856 Boastin‘ Red Socks数学

POJ2689 ZOJ1842 UVA10140 Prime Distance筛选法

POJ2351 ZOJ1916 UVA10371 Time Zones时区计算