IT Companies
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 395 Accepted Submission(s): 116
Problem Description
There are N IT companies which are labeled from 1 to N. Each of them has a branch (a branch is not a company, and companies and branches are all called "unit").The branches are labeled from -1 to –N. The branch of company i is labeled as -i. The number of workers of each company and its branch has to fit the rules below:
1. The number of workers of a company must be larger than that of the branch of it.
2. There are more workers in company i than company j if and only if there are more workers in the branch of company i than the branch of company j.
Among the companies whose label is larger than i(range from i+1 to n),and the branches whose label is larger than -i (range from -1 to –(i-1) ), there are c[i] units which have more workers than company i.
You need to sort the 2×N units in the ascending order by the number of workers.
1. The number of workers of a company must be larger than that of the branch of it.
2. There are more workers in company i than company j if and only if there are more workers in the branch of company i than the branch of company j.
Among the companies whose label is larger than i(range from i+1 to n),and the branches whose label is larger than -i (range from -1 to –(i-1) ), there are c[i] units which have more workers than company i.
You need to sort the 2×N units in the ascending order by the number of workers.
Input
The input contains multiple test cases. Each test case begins with a single line containing one integer N indicating the number of companies (0 < N ≤ 100000). Next line contains N integers which are c[1],c[2]…c[N] (c[i] ≤ N).
The input ends with N = 0.
The input ends with N = 0.
Output
For each test case, output the sorted label sequence of all units in a line. If there are no solutions, output "Impossible" instead.
This problem is special judged.
This problem is special judged.
Sample Input
2 1 1 10 4 8 3 4 2 0 5 7 1 6 0
Sample Output
Impossible -8 -2 -10 -7 8 2 10 -4 7 -1 4 -3 -5 -9 1 3 5 9 -6 6
Source
http://hi.baidu.com/isaacpei/item/1c5c7fc1d9bddfd9ee183bc4
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
struct data
{
int v, id;
};
const int inf = 0x3f3f3f3f;
data tree[444444];
int mark[444444];
int a[111111],ans[222222];
int n, m , cnt;
data Min(data x , data y)
{
if ( x.v < y.v || ( (x.v==y.v && x.id < y.id) ) )
return x;
else
return y;
}
void build(int o, int l, int r)
{
mark[o] = 0;
if ( l == r )
{
tree[ o ].v= a[l];
tree[ o ].id = l;
return ;
}
int mid = (l + r) >> 1;
build( o*2, l , mid );
build( o*2 + 1, mid + 1, r );
tree[o] = Min( tree[o*2], tree[o*2+1]);
}
void pushdown(int o, int l, int r)
{
if ( l!=r)
{
if ( mark[o]!= 0)
{
tree[o*2].v += mark[o];
tree[o*2+1].v += mark[o];
mark[o*2] += mark[o];
mark[o*2+ 1] += mark[o];
mark[o] = 0;
}
}
}
void pushup(int o, int l, int r)
{
tree[o] = Min( tree[o*2] , tree[o*2 + 1]);
}
void update(int o, int l, int r, int x, int y, int v)
{
if ( x<=l && r<= y)
{
if ( v == inf)
{
tree[o].v = inf;
mark[o] = 0;
return;
}
tree[o].v +=v;
mark[o] +=v;
return ;
}
pushdown( o, l ,r );
int mid= ( l + r) >> 1;
if ( x<=mid) update( o * 2 , l, mid , x , y ,v);
if ( y> mid) update( o * 2 + 1, mid+1, r, x, y, v);
pushup( o, l, r );
}
data query(int o, int l, int r, int x, int y)
{
if ( x<=l && r <= y)
return tree[o];
int mid= ( l + r) >> 1;
data tmp1, tmp2;
if ( x<=mid) tmp1 = query( o * 2 , l, mid , x , y);
if ( y> mid) tmp2 = query( o * 2 + 1, mid+1, r, x, y);
return Min( tmp1, tmp2);
}
int main()
{
while (1)
{
scanf("%d",&n);
if ( n==0 ) break;
for (int i=1; i<=n; i++)
scanf("%d", &a[i]);
build( 1, 1, n );
queue <int> q;
m = cnt = 0;
bool flag;
while ( cnt < n)
{
data tmp;
flag = 1;
tmp = query( 1, 1, n, 1, n );
if ( tmp.v == 0)
{
m ++;
ans[ m ] = tmp.id;
if ( tmp.id -1 > 0)
update( 1, 1, n,1, tmp.id-1, -1);
update( 1, 1, n, tmp.id, tmp.id, inf);
q.push( -tmp.id );
cnt ++;
}
else if ( tmp.v > 0)
{
if (!q.empty())
{
int p = q.front();
q.pop();
if ( -(p-1) <= n)
update( 1, 1, n , -(p-1) , n ,-1 );
m++;
ans[ m ] = p;
}
else
flag = 0;
}
else
flag = 0;
if ( !flag ) break;
}
if (!flag) printf("Impossible\n");
else
{
while (!q.empty())
{
int p = q.front();
q.pop();
m++;
ans[m] = p;
}
for (int i=m; i>=1; i--)
printf("%d ",ans[i]);
printf("\n");
}
}
return 0;
}