uva11997 K Smallest Sums&&UVALive 3135 Argus(优先队列,多路归并)
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<string> #include<cmath> #include<map> #include<set> #include<vector> #include<algorithm> #include<stack> #include<queue> #include<cctype> #include<sstream> using namespace std; #define pii pair<int,int> #define LL long long int const double eps=1e-10; const int INF=1000000000; const int maxn=750+10; int n,a[maxn][maxn]; struct node { int s,b; node(int s,int b): s(s),b(b){} bool operator < (const node &x) const { return s>x.s; } }; priority_queue<node>pq; void he(int *A,int *B,int *C) { while(!pq.empty()) pq.pop(); for(int i=0; i<n; i++) { pq.push(node(A[i]+B[0],0)); } for(int i=0; i<n; i++) { node t=pq.top(); pq.pop(); C[i]=t.s; pq.push(node(t.s-B[t.b]+B[t.b+1],t.b+1)); } } int main() { //freopen("in1.txt","r",stdin); //freopen("out.txt","w",stdout); while(scanf("%d",&n)==1) { for(int i=0; i<n; i++) { for(int j=0; j<n; j++) { scanf("%d",&a[i][j]); } sort(a[i],a[i]+n); } for(int i=1; i<n; i++) { he(a[0],a[i],a[0]); } for(int i=0; i<n-1; i++) printf("%d ",a[0][i]); printf("%d\n",a[0][n-1]); } return 0; }
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<string> #include<cmath> #include<map> #include<set> #include<vector> #include<algorithm> #include<stack> #include<queue> #include<cctype> #include<sstream> using namespace std; #define pii pair<int,int> #define LL long long int const double eps=1e-10; const int INF=1000000000; const int maxn=1000+10; int k,a,b,ans[10000+10]; string s; struct node { int num,t,p; node (int num,int t,int p):num(num),t(t),p(p){} bool operator <(const node &x)const { if(t!=x.t) { return t>x.t; } else { return num>x.num; } } }; priority_queue<node>pq; int main() { //freopen("in1.txt","r",stdin); //freopen("out.txt","w",stdout); while(cin>>s) { if(s[0]=='#') { scanf("%d",&k); for(int i=0;i<k;i++) { node tmp=pq.top(); pq.pop(); ans[i]=tmp.num; pq.push(node(tmp.num,tmp.t+tmp.p,tmp.p)); } } else { scanf("%d%d",&a,&b); pq.push(node(a,b,b)); } for(int i=0;i<k;i++) { printf("%d\n",ans[i]); } } return 0; }