[UVA] 234 - Switching Channels
Switching Channels
Switching Channels |
CPN (The Couch Potato Network) owns several cable channels. They would like to arrange the timing of programmes so viewers can switch channels without missing the end of one programme or the beginning of another. To do this they have identified certain times, called ``alignment points," where ideally one programme should end and another should begin. Some of these alignment points are more important than others. For example, the time when the nightly news begins is an important alignment point. Since many viewers watch the news, they would be less likely to watch a CPN programme whose ending time causes them to miss the beginning of the news, or which starts before the news finishes. Your task is to write a solution which determines the best order in which programmes can be shown on one channel.
A ``miss" time is the absolute value of the difference between the time of an alignment point and the nearest time of the beginning or end of a programme. The ``total miss time" at a particular level of importance is the sum of all the miss times for alignment points at that level of importance. One programme order is better than another if it has a lower total miss time at some level of importance and the same total miss time at all higher levels of importance (if any).
Input
Your solution must accept multiple input data sets. Each set will begin with an integer, p ( ), specifying the number of programmes to be ordered. When a data set beginning with 0 is encountered, your solution should terminate. Following p on the same line will be p integers specifying the lengths of the programmes in minutes. There is no significance to the order in which these are given.
The next line of input specifies the alignment points. The total number of such points, a ( ), appears first followed by a pairs of integers. The first integer in each pair, i ( ), gives the importance of the alignment point. Alignment points marked 1 are most important; those marked 2 are of secondary importance, etc. The second integer in each pair, t, specifies the time when the alignment point occurs. No two alignment points in the same data set will have the same value of t.
Output
Your solution must output three lines for each data set. The first line identifies the data set being processed and should be in the form:
Data set n
where n is the number of the data set (1 for the first data set, 2 for the second, etc.). On the following line, your solution should output the lengths of the programmes in the order in which they should be shown to achieve the best synchronization with the alignment points. On the third line, output the total number of minutes by which the alignment points were missed (the sum of all total miss times).
There may be more than one best programme order for an input data set. Any one of these best orders is acceptable.
Sample Input
4 30 45 45 15 3 1 60 2 90 3 15 6 10 15 13 18 25 33 4 1 30 2 15 2 45 1 60 0
Sample Output
Data set 1 Order: 15 45 30 45 Error: 0 Data set 2 Order: 15 13 33 25 18 10 Error: 19
題目描述:
排序一個節目時間,然後其中有幾個重要的時間點,最好是能在那個時間點進行新的節目,否則將會產生一個 miss time, miss time 決定於一個 |最近時間點 - 對齊時間點|。
根據重要程度的權重,找到一個最小的排序方式。
題目解法:
窮舉。
例如 Order :15 13 33 25 18 10
累積時間為 0, 15, 28, 61, 86, 104, 114
對齊時間為
level 1:30, 60 => miss time : abs(30-28)+abs(60-61) = 3
level 2:15, 45 => miss time : abs(15-15)+abs(45-61) = 16
total miss time = error = 3+16 = 19
bool compare() {
if(level1 != other.level1)
return level1 < other.level1;
if(level2 != other.level2)
return level2 < other.level2;
if(level3 != other.level3)
return level3 < other.level3;
... most level 5
}
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
int cases = 0;
int n, m, A[10];
int i, j, k;
while(scanf("%d", &n) == 1 && n) {
for(i = 0; i < n; i++)
scanf("%d", &A[i]);
scanf("%d", &m);
vector<pair<int, int> > time;
for(i = 0; i < m; i++) {
int l, ap;
scanf("%d %d", &l, &ap);
time.push_back(make_pair(ap, l));
}
sort(A, A+n);
sort(time.begin(), time.end());
int order[10], impt[10];
for(i = 0; i < 10; i++)
impt[i] = 0xfffffff;
do {
int B[10] = {};
for(i = 0; i < n; i++)
B[i+1] = B[i]+A[i];
B[n+1] = 0xfffffff;
int x = 0, y = 0;
int tmp[10] = {};
while(x < m) {
while(y <= n && B[y] < time[x].first)
y++;
int tt = abs(B[y]-time[x].first);
if(y-1 != 0)
tt = min(tt, abs(B[y-1]-time[x].first));
tmp[time[x].second] += tt;
x++;
}
int flag = 0;
for(i = 0; i < 10; i++) {
if(impt[i] < tmp[i]) {
flag = 1;
break;
} else if(impt[i] > tmp[i]) {
flag = 2;
break;
}
}
if(flag == 2) {
for(i = 0; i < 10; i++) {
order[i] = A[i];
impt[i] = tmp[i];
}
}
} while(next_permutation(A, A+n));
printf("Data set %d\n", ++cases);
printf("Order:");
for(i = 0; i < n; i++)
printf(" %d", order[i]);
puts("");
int err = 0;
for(i = 0; i < 10; i++)
err += impt[i];
printf("Error: %d\n", err);
}
return 0;
}