Sequence
Time limit:6000 ms |
|
Memory limit:65536 K |
Total submissions:7019 |
|
Accepted:2266 |
N sequences are given. Each sequence has each element. One element is extracted from each sequence and the first n smallest and
Priority queue
#include<cstring>#include<cstdio>#include<cstring>#include<queue>#include<iostream>#include<algorithm>using namespace std;int main(){ int cas,n,m,i,j; int a[2005],b[2005]; priority_queue<int,deque<int>,less<int> >big; scanf("%d",&cas); while(cas--) { scanf("%d%d",&m,&n); for(i=0;i<n;i++) scanf("%d",&a[i]); sort(a,a+n); for(i=1;i<m;i++) { for(j=0;j<n;j++) { scanf("%d",&b[j]); big.push(a[0]+b[j]); } sort(b,b+n); for(int k=1;k<n;k++) for(int l=0;l<n;l++) { if(a[k]+b[l]>big.top()) break; big.pop(); big.push(a[k]+b[l]); } for(int k=0;k<n;k++) { a[n-k-1]=big.top(); big.pop(); } } printf("%d",a[0]); for(i=1;i<n;i++) printf(" %d",a[i]); printf("\n"); } return 0;}
Poj 2442 Sequence