Sequence
http://poj.org/problem?id=2442
用STL写的时间为:5657MS
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio> 2 #include<algorithm> 3 #include<queue> 4 #define MAXN 2005 5 using namespace std; 6 int main() 7 { 8 int t,n,m,c; 9 scanf("%d",&t); 10 while(t--){ 11 scanf("%d%d",&n,&m); 12 int a[MAXN]; 13 priority_queue< int,vector<int>,greater<int> >q; 14 priority_queue< int,vector<int>,less<int> >p; 15 for(int i=0; i<m; i++) 16 { 17 scanf("%d",&c); 18 q.push(c); 19 } 20 for(int j=1; j<n; j++) 21 { 22 for(int k=0; k<m; k++) 23 { 24 scanf("%d",&a[k]); 25 } 26 while(!q.empty()) 27 { 28 int mm=q.top(); 29 q.pop(); 30 for(int h=0; h<m; h++) 31 { 32 if(p.size()==m&&p.top()>mm+a[h]) 33 { 34 p.pop(); 35 p.push(mm+a[h]); 36 } 37 else if(p.size()<m) 38 { 39 p.push(mm+a[h]); 40 } 41 } 42 } 43 while(!p.empty()) 44 { 45 q.push(p.top()); 46 p.pop(); 47 } 48 } 49 int mark=1; 50 for(int i=0;i<m;i++) 51 { 52 if(mark) 53 { 54 printf("%d",q.top()); 55 mark=0; 56 } 57 else printf(" %d",q.top()); 58 q.pop(); 59 } 60 printf("\n"); 61 } 62 return 0; 63 }
我用堆写的时间:3969MS
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio> 2 #include<algorithm> 3 #include<queue> 4 #define MAXN 2005 5 long long a[5000005],b[5000005]; 6 using namespace std; 7 int len=0,len1=0; 8 void up1(int n) 9 { 10 a[++len]=n; 11 int p=len; 12 int q=p/2; 13 long long m=a[p]; 14 while(q>=1&&m<a[q]) 15 { 16 a[p]=a[q]; 17 p=q; 18 q=p/2; 19 } 20 a[p]=m; 21 } 22 void up2(int n) 23 { 24 b[++len1]=n; 25 int p=len1; 26 int q=p/2; 27 long long m=b[p]; 28 while(q>=1&&m>b[q]) 29 { 30 b[p]=b[q]; 31 p=q; 32 q=p/2; 33 } 34 b[p]=m; 35 } 36 void down1(int p) 37 { 38 a[1]=a[len--]; 39 int q=p*2; 40 long long m=a[p]; 41 while(q<=len) 42 { 43 if(q<len&&a[q]>a[q+1]) 44 q++; 45 if(a[q]>=m) break; 46 else 47 { 48 a[p]=a[q]; 49 p=q; 50 q=p*2; 51 } 52 } 53 a[p]=m; 54 } 55 int pop1() 56 { 57 long long r=a[1]; 58 return r; 59 } 60 int pop2() 61 { 62 long long r=b[1]; 63 return r; 64 } 65 void down2(int p) 66 { 67 b[1]=b[len1--]; 68 int q=p*2; 69 long long m=b[p]; 70 while(q<=len1) 71 { 72 if(q<len1&&b[q]<b[q+1]) 73 q++; 74 if(b[q]<=m) break; 75 else 76 { 77 b[p]=b[q]; 78 p=q; 79 q=p*2; 80 } 81 } 82 b[p]=m; 83 } 84 85 86 int main() 87 { 88 int t,n,m,c; 89 scanf("%d",&t); 90 while(t--){ 91 scanf("%d%d",&n,&m); 92 int aa[MAXN]; 93 for(int i=0; i<m; i++) 94 { 95 scanf("%d",&c); 96 up1(c); 97 } 98 for(int j=1; j<n; j++) 99 { 100 for(int k=0; k<m; k++) 101 { 102 scanf("%d",&aa[k]); 103 } 104 while(len!=0) 105 { 106 int mm=pop1(); 107 down1(1); 108 for(int h=0; h<m; h++) 109 { 110 if(len1>=m&&b[1]>mm+aa[h]) 111 { 112 down2(1); 113 up2(mm+aa[h]); 114 } 115 else if(len1<m) 116 { 117 up2(mm+aa[h]); 118 } 119 } 120 } 121 while(len1!=0) 122 { 123 up1(b[1]); 124 down2(1); 125 } 126 } 127 int mark=1; 128 for(int i=0;i<m;i++) 129 { 130 if(mark) 131 { 132 printf("%lld",a[1]); 133 mark=0; 134 } 135 else printf(" %lld",a[1]); 136 down1(1); 137 } 138 printf("\n"); 139 } 140 return 0; 141 }