算法笔记 上机训练实战指南 第13章 专题扩展 学习笔记

13.3 快乐模拟

PAT B1050 螺旋矩阵 (25分)

本题要求将给定的 N 个正整数按非递增的顺序,填入“螺旋矩阵”。所谓“螺旋矩阵”,是指从左上角第 1 个格子开始,按顺时针螺旋方向填充。要求矩阵的规模为 m 行 n 列,满足条件:m×n 等于 N;mn;且 mn 取所有可能值中的最小值。

输入格式:

输入在第 1 行中给出一个正整数 N,第 2 行给出 N 个待填充的正整数。所有数字不超过 1,相邻数字以空格分隔。

输出格式:

输出螺旋矩阵。每行 n 个数字,共 m 行。相邻数字以 1 个空格分隔,行末不得有多余空格。

输入样例:

12
37 76 20 98 76 42 53 95 60 81 58 93

输出样例:

98 95 93
42 37 81
53 20 76
58 60 76
#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,m,N;
    scanf("%d",&N);
    for(int i=0;i<N;i++){
        scanf("%d",&a[i]);
    }
    if(N == 1){
        printf("%d",a[0]);
        return 0;
    }
    m = (int)ceil(sqrt(1.0*N));
    while(N % m !=0){
        m++;
    }
    sort(a,a+N,cmp);
    n = N/m;
    int 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++];
            i--;
        }
        U++,D--,L++,R--;
        i++,j++;
        if(now == N-1){
            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;
}

 

PAT A1017 Queueing at Bank (25分)

Suppose a bank has K windows open for service. There is a yellow line in front of the windows which devides the waiting area into two parts. All the customers have to wait in line behind the yellow line, until it is his/her turn to be served and there is a window available. It is assumed that no window can be occupied by a single customer for more than 1 hour.

Now given the arriving time T and the processing time P of each customer, you are supposed to tell the average waiting time of all the customers.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 numbers: N (≤) - the total number of customers, and K (≤) - the number of windows. Then N lines follow, each contains 2 times: HH:MM:SS - the arriving time, and P - the processing time in minutes of a customer. Here HH is in the range [00, 23], MM and SS are both in [00, 59]. It is assumed that no two customers arrives at the same time.

Notice that the bank opens from 08:00 to 17:00. Anyone arrives early will have to wait in line till 08:00, and anyone comes too late (at or after 17:00:01) will not be served nor counted into the average.

Output Specification:

For each test case, print in one line the average waiting time of all the customers, in minutes and accurate up to 1 decimal place.

Sample Input:

7 3
07:55:00 16
17:00:01 2
07:59:59 15
08:01:00 60
08:00:00 30
08:00:02 2
08:03:00 10

Sample Output:

8.2
#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 = endTime[j];
                idx = j;
            }
        }
        if(endTime[idx] <= custom[i].comeTime){
            endTime[idx] = custom[i].comeTime + custom[i].serveTime;
        }else{
            totTime = totTime + (endTime[idx] - custom[i].comeTime);
            endTime[idx] = endTime[idx] + custom[i].serveTime;
        }
    }
    if(custom.size() == 0)
        printf("0.0");
    else
        printf("%.1f",totTime/60.0/custom.size());
    return 0;
}

PAT A1014 Waiting in Line (30分)

Suppose a bank has N windows open for service. There is a yellow line in front of the windows which devides the waiting area into two parts. The rules for the customers to wait in line are:

  • The space inside the yellow line in front of each window is enough to contain a line with M customers. Hence when all the N lines are full, all the customers after (and including) the (st one will have to wait in a line behind the yellow line.
  • Each customer will choose the shortest line to wait in when crossing the yellow line. If there are two or more lines with the same length, the customer will always choose the window with the smallest number.
  • Customeri​​ will take Ti​​ minutes to have his/her transaction processed.
  • The first N customers are assumed to be served at 8:00am.

Now given the processing time of each customer, you are supposed to tell the exact time at which a customer has his/her business done.

For example, suppose that a bank has 2 windows and each window may have 2 custmers waiting inside the yellow line. There are 5 customers waiting with transactions taking 1, 2, 6, 4 and 3 minutes, respectively. At 08:00 in the morning, customer1​​ is served at window1​​ while customer2​​ is served at window2​​. Customer3​​ will wait in front of window1​​ and customer4​​ will wait in front of window2​​. Customer5​​ will wait behind the yellow line.

At 08:01, customer1​​ is done and customer5​​ enters the line in front of window1​​ since that line seems shorter now. Customer2​​ will leave at 08:02, customer4​​ at 08:06, customer3​​ at 08:07, and finally customer5​​ at 08:10.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 4 positive integers: N (≤, number of windows), M (≤, the maximum capacity of each line inside the yellow line), K (≤, number of customers), and Q (≤, number of customer queries).

The next line contains K positive integers, which are the processing time of the K customers.

The last line contains Q positive integers, which represent the customers who are asking about the time they can have their transactions done. The customers are numbered from 1 to K.

Output Specification:

For each of the Q customers, print in one line the time at which his/her transaction is finished, in the format HH:MM where HH is in [08, 17] and MM is in [00, 59]. Note that since the bank is closed everyday after 17:00, for those customers who cannot be served before 17:00, you must output Sorry instead.

Sample Input:

2 2 7 5
1 2 6 4 3 534 2
3 4 5 6 7

Sample Output:

08:07
08:06
08:10
17:00
Sorry
#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
const int maxNode = 1111;
int n,m,k,query,q;
int convertToMinute(int h,int m){
    return h * 60 + m;
}
struct Window{
    int endTime,popTime;
    queue<int> q;
}window[20];
int ans[maxNode],needTime[maxNode];
int main(){
    int inIndex = 0;
    scanf("%d%d%d%d",&n,&m,&k,&query);
    for(int i=0;i<k;i++){
        scanf("%d",&needTime[i]);
    }
    for(int i=0;i<n;i++){
        window[i].popTime = window[i].endTime = convertToMinute(8,0);
    }
    for(int i = 0; i < min(n * m, k);i++){
        window[inIndex % n].q.push(inIndex);
        window[inIndex % n].endTime += needTime[inIndex];
        if(inIndex < n)
            window[inIndex].popTime = needTime[inIndex];
        ans[inIndex] = window[inIndex % n].endTime;
        inIndex++; 
    }
    for(;inIndex < k;inIndex++){
        int idx = -1,minPopTime = 1 << 30;
        for(int i = 0; i < n ;i++){
            if(window[i].popTime < minPopTime){
                idx = i;
                minPopTime = window[i].popTime;
            }
        }
        
        Window& W = window[idx];
        W.q.pop();
        W.q.push(inIndex);
        W.endTime += needTime[inIndex];
        W.popTime += needTime[W.q.front()];
        ans[inIndex] = W.endTime;
    }
    for(int i = 0; i < query;i++){
        scanf("%d",&q);
        if(ans[q - 1] - needTime[q - 1] >= convertToMinute(17,0)){
            printf("Sorry\n");
        }else{
            printf("%02d:%02d\n",ans[q-1]/60,ans[q-1]%60);
        }
    }
    return 0;
}

 

 






posted @ 2020-02-12 14:39  程序员小应  阅读(356)  评论(0编辑  收藏  举报