CodeForces 81D.Polycarp's Picture Gallery 乱搞
Polycarp loves not only to take pictures, but also to show his photos to friends. On his personal website he has recently installed a widget that can display n photos with the scroll option. At each moment of time the widget displays exactly one photograph with the option showing the previous/next one. From the first photo, you can switch to the second one or to the n-th one, from the second photo you can switch to the third one or to the first one, etc. Thus, navigation is performed in a cycle.
Polycarp's collection consists of m photo albums, the i-th album contains ai photos. Polycarp wants to choose n photos and put them on a new widget. To make watching the photos interesting to the visitors, he is going to post pictures so that no two photos from one album were neighboring (each photo will have exactly two neighbors, the first photo's neighbors are the second and the n-th one).
Help Polycarp compile a photo gallery. Select n photos from his collection and put them in such order that no two photos from one album went one after the other.
The first line contains two integers n and m (3 ≤ n ≤ 1000, 1 ≤ m ≤ 40), where n is the number of photos on the widget, and m is the number of albums. The second line contains m integers a1, a2, ..., am (1 ≤ ai ≤ 1000), where ai is the number of photos in the i-th album.
Print the single number -1 if there is no solution. Otherwise, print n numbers t1, t2, ..., tn, where ti represents the number of the album of the i-th picture in the widget. The albums are numbered from 1 in the order of their appearance in the input. If there are several solutions, print any of them.
4 3
1 3 5
3 1 3 2
10 2
5 5
2 1 2 1 2 1 2 1 2 1
10 3
1 10 3
-1
#include<cstdio> #include<queue> #include<algorithm> using namespace std; int a[42],p[1024],co[42]; typedef struct { int num,con; }data; data d[41]; priority_queue<data>q; bool operator<( data x, data y ){ return x.con<y.con; } int main() { int n,m,t,cnt,sum=0,co,temp,right; scanf("%d%d",&n,&m); for(int i=1; i<=m; i++) { scanf("%d",&a[i]); if(a[i]>n/2) a[i]=n/2; sum+=a[i]; } if(sum<n) printf("-1"); else { for(int i=1;i<=m;i++) { d[i].num=i; d[i].con=a[i]; q.push(d[i]); } right=0; while(right<n) { data u=q.top(); q.pop(); for(int i=1;i<=2*u.con-1&&(i+right<=n);i++) if(i&1) p[i+right]=u.num; else { data temp=q.top(); q.pop(); p[i+right]=temp.num; temp.con--; q.push(temp); } right=min(right+2*u.con-1,n); } for(int i=1;i<=n;i++) printf("%d ",p[i]); } return 0; }