Balloons

来源:互联网 发布:粮票知乎 编辑:程序博客网 时间:2024/05/17 21:49

Balloons

Description

As you may know, balloons are handed out during ACM contests to teams as they solve problems. However, this sometimes presents logistical challenges. In particular, one contest hosting site maintains two rooms, A and B, each containing a supply of balloons. There are N teams attending the contest at that site, each sitting at a different location. Some are closer to room A, others are closer to room B, and others are equally distant. Given the number of balloons needed by each team and the distance from each team to room A, and to room B, what is the minimum total possible distance that must be traveled by all balloons as they are delivered to their respective teams, assuming they are allocated in an optimal fashion from rooms A and B? For the purposes of this problem, assume that all of the balloons are identical. 

Input

There will be several test cases in the input. Each test case will begin with a line with three integers: N A B Where N is the number of teams (1<=N<=1, 000), and A and B are the number of balloons in rooms A and B, respectively (0<=A, B<=10, 000). On each of the next N lines there will be three integers, representing information for each team: K DA DB Where K is the total number of balloons that this team will need, DA is the distance of this team from room A, and DB is this team's distance from room B (0<=DA, DB<=1, 000). You may assume that there are enough balloons - that is, (求和)(K's)<=A + B. The input will end with a line with three 0s. 

Output

For each test case, output a single integer, representing the minimum total distance that must be traveled to deliver all of the balloons. Count only the outbound trip, from room A or room B to the team. Don't count the distance that a runner must travel to return to room A or room B. Print each integer on its own line with no spaces. Do not print any blank lines between answers. 

Sample Input

3 15 35
10 20 10
10 10 30
10 40 10
0 0 0

Sample Output

300

题目:n只队伍打比赛,需要两种气球,数目有限,给出每支队伍距离气球制造的地方的距离,以及所需要的气球数目,求最少的总距离是多少

解法:假设每支队伍都取最优的情况,若是气球数目不够,那么改变所造成的增量为max-min。因此我们只要按照max-min进行排序,再贪心就好了

#include <iostream>using namespace std;#include <stdio.h>int a[20010][5], n, ka, kb, temp;int add(int x, int y) {    if (x > y) return x-y;    return y-x;}int min(int x, int y) {    if (x < y) return x;    return y;}void sort(int i, int j) {    int p = i, q = j;    int mid = add(a[(p+q)/2][0], a[(p+q)/2][1]);    while (p <= q) {        while (add(a[p][0], a[p][1]) > mid) p++;        while (add(a[q][0], a[q][1]) < mid) q--;        if (p <= q) {            for (int k = 0; k <= 1; k++) {                temp = a[p][k];                a[p][k] = a[q][k];                a[q][k] = temp;            }            p++; q--;        }    }    if (i < q) sort(i, q);    if (p < j) sort(p, j);}int main() {    int temp;    while (1) {        cin >> n >> ka >> kb;        if (n == 0 && ka == 0 && kb == 0) break;        int tot = 0, k, x, y;        for(int i = 0; i < n; i++) {              cin >> k >> x >> y;              for(int j = 1; j <= k; j++) {                  tot++;                a[tot][0] = x;                a[tot][1] = y;              }        }          sort(1, tot);        int ans = 0;        for(int i = 1; i <= tot; i++) {              if(a[i][0] <= a[i][1]) {                  if (ka > 0) {                      ans += a[i][0];                      ka--;                } else {                      ans += a[i][1];                      kb--;                  }            } else {                  if (kb > 0) {                      ans += a[i][1];                      kb--;                  } else {                      ans += a[i][0];                      ka--;                }              }          }          cout << ans << endl;      }}
0 0
原创粉丝点击