1604. Country of Fools
Time limit: 1.0 second
Memory limit: 64 MB
The chief traffic policeman of the Country of Fools wants to impose a speed limit on the motorway going from the Field of Wonders to the City of Simple Simons. He ordered n speed limit signs. When the order arrived it turned out that the signs had different numbers on them, which showed limits in kilometers per hour. There were k different limits: n1 signs with the first limit, n2 signs with the second limit, etc.; n1 + … + nk = n.
To make the life of drivers not so easy, the chief policeman decided to place the signs on the motorway in such a way that a driver would have to change speed as many times as possible. According to the traffic regulations in the Country of Fools, a speed limitation is valid until the following speed limit sign, and the speed shown in the sign must be observed exactly. For example, if there is the number 60 on the sign, then a car must go until the following sign with the speed of exactly 60 kilometers per hour.
Input
The first line contains the number k of different types of speed limit signs; 1 ≤ k ≤ 10000. The second line contains positive integers n1, …, nk separated with a space. The sum of all ni does not exceed 10000.
Output
Output the order in which the signs must be place on the motorway, in the form of n integers in the range from 1 to k. Assume that a driver must change speed when the car passes by the first sign, irrespective of the initial speed. If there are several solutions, you may output any of them.
Sample
input | output |
---|---|
2 | 1 2 1 2 |
2 2 |
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXNUM = 10010;
struct obstacle
{
int count;
int num;
};
obstacle o[MAXNUM];
int ans[MAXNUM][MAXNUM];
bool cmp(obstacle a, obstacle b) {
return (a.count > b.count);
}
int main()
{
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> o[i].count;
o[i].num = i + 1;
}
sort(o, o + n, cmp);
int m = o[0].count;
int t = 0;
for (int i = 0; i < n&&t<n; i++) {
for (int j = 0; j < m;j++) {
if (o[t].count > 0) {
ans[i][j] = o[t].num;
o[t].count--;
}
if (o[t].count == 0)t++;
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (ans[j][i] == 0)break;
else {
cout << ans[j][i] << " ";
}
}
}
return 0;
}