火车购票(CCF)

传送门

题解:先考虑能否放下相邻的,能放下直接放,并加以标记,是个小模拟+标记法


#include<bits/stdc++.h>

using namespace std;

int book[25][10];
int cnt[25];

int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=20;i++){
        cnt[i]=5;
    }
    int temp;
    for(int i=0;i<n;i++){
        scanf("%d",&temp);
        for(int j=1;j<=20;j++){
            if(cnt[j]>=temp){
                for(int k=1;k<=5;k++){
                    if(!book[j][k]){
                        printf("%d ",(j-1)*5+k);
                        book[j][k]=1;
                        cnt[j]--;
                        temp--;
                        if(!temp){
                           break;
                        }
                    }
                }
            }
            if(!temp){
                break;
            }
        }
        if(temp){
            for(int j=1;j<=20;j++){
                if(cnt[j]){
                    for(int k=1;k<=5;k++){
                        if(!book[j][k]){
                            printf("%d ",(j-1)*5+k);
                            book[j][k]=1;
                            cnt[j]--;
                            temp--;
                            if(temp==0){
                                break;
                            }
                        }
                    }
                }
                if(temp==0){
                    break;
                }
            }
        }
        printf("\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/zhouzi2018/article/details/81943133