螺旋矩阵问题#include<cstdio> #include<cmath> #include<algorithm> using namespace std; const int maxn=10010; int matrix[maxn][maxn],A[maxn]; bool cmp(int a,int b){ return a>b; } int main(){ int N; scanf("%d",&N); for(int i=0;i<N;i++){ scanf("%d",&A[i]); } if(N==1){ printf("%d",A[0]); return 0; } sort(A,A+N,cmp); int m=(int)cellsqrt(1.0*N); while(N%m!=0){ m++; } int n=N/m,i=1,j=1,now=0; int U=1,D=m,L=1,R=n; while(now<N){ while (now<N&&j<R) { matrix[i][j]=A[now++]; j++; } while(now<N&&i<D){ matrix[i][j]=A[now++]; i++; } while(now<N&&j>L){ matrix[i][j]=A[now++]; j--; } while(now<N&&i>U){ matrix[i][j]=A[now++]; } } for(int i=1;i<=m;i++){ for(int j=1;j<n;j++){ printf("%d",matrix[i][j]); if(j<n)printf(" "); else printf("\n"); } } return 0; }
Queueing at Bank
#include<cstdio> #include<vector> #include<algorithm> using namespace std; const int K=111; const int INF=1000000000; struct Customer{ int comeTime,serveTime; }newCustomer; vector<Customer>custom; int convertTime(int h,int m,int s){ return h*3600+m*60+s; } bool cmp(Customer a,Customer b){ return a.comeTime<b.comeTime; } int endTime[K]; int main(){ int c,w,totTime=0; int StTime=convertTime(8, 0, 0); int edTime=convertTime(17, 0, 0); scanf("%d%d",&c,&w); for(int i=0;i<w;i++) endTime[i]=StTime; for(int i=0;i<c;i++){ int h,m,s,serveTime; scanf("%d:%d:%d %d",&h,&m,&s,&serveTime); int comeTime=convertTime(h,m,s); if(comeTime>edTime)continue; newCustomer.comeTime=comeTime; newCustomer.serveTime=serveTime<=60?serveTime*60:3600; custom.push_back(newCustomer); } sort(custom.begin(),custom.end(),cmp); for(int i=0;i<custom.size();i++){ int idx=-1,minEndTime=INF; for(int j=0;j<w;j++){ if(endTime[j]<minEndTime){ minEndTime=minEndTime; idx=j; } } if(endTime[idx]<=custom[i].comeTime){ endTime[idx]=custom[i].comeTime+custom[i].serveTime; }else { totTime+=(endTime[idx]-custom[i].comeTime); endTime[idx]+=custom[i].serveTime; } } if(custom.size()==0)printf("0.0"); else printf("%.lf",totTime/60.0/custom.size()); return 0; }