CCF 201612-2 火车购票 (暴力)

问题描述
  请实现一个铁路购票系统的简单座位分配算法,来处理一节车厢的座位分配。
  假设一节车厢有20排、每一排5个座位。为方便起见,我们用1到100来给所有的座位编号,第一排是1到5号,第二排是6到10号,依次类推,第20排是96到100号。
  购票时,一个人可能购一张或多张票,最多不超过5张。如果这几张票可以安排在同一排编号相邻的座位,则应该安排在编号最小的相邻座位。否则应该安排在编号最小的几个空座位中(不考虑是否相邻)。
  假设初始时车票全部未被购买,现在给了一些购票指令,请你处理这些指令。
输入格式
  输入的第一行包含一个整数n,表示购票指令的数量。
  第二行包含n个整数,每个整数p在1到5之间,表示要购入的票数,相邻的两个数之间使用一个空格分隔。
输出格式
  输出n行,每行对应一条指令的处理结果。
  对于购票指令p,输出p张车票的编号,按从小到大排序。
样例输入
4
2 5 4 2
样例输出
1 2
6 7 8 9 10
11 12 13 14
3 4
样例说明
  1) 购2张票,得到座位1、2。
  2) 购5张票,得到座位6至10。
  3) 购4张票,得到座位11至14。
  4) 购2张票,得到座位3、4。
评测用例规模与约定
  对于所有评测用例,1 ≤ n ≤ 100,所有购票数量之和不超过100。
析:这个题并不难,完全可以用暴力解决,每次都去遍历1-100,看看是不是能够找到 p 个相邻的且在同一排的数,如果能就输出,如果不能,那么就去找不相邻的,
在比赛时,好像没有看到不相邻的情况,所以,只得了90分。遗憾啊。。
 
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
const int dr[] = {-1, 1, 0, 0};
const int dc[] = {0, 0, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
    return r >= 0 && r < n && c >= 0 && c < m;
}
bool vis[105];

void print(int s, int t){
    int cnt = 0;
    for(int i = s; i <= t; ++i, ++cnt){
        if(cnt)  putchar(' ');
        printf("%d", i+1);
        vis[i] = true;
    }
    printf("
");
}

int main(){
    while(scanf("%d", &n) == 1){
        memset(vis, false, sizeof vis);
        for(int i = 0; i < n; ++i){
            scanf("%d", &m);
            int cnt = 0;
            bool ok = false;
            for(int j = 0; j < 100; ++j){
                if(j % 5 == 0) cnt = 0;
                if(!vis[j])  ++cnt;
                if(cnt == m){ print(j-m+1, j);  ok = true;  break; }
            }
            if(!ok){
                cnt = 0;
                for(int j = 0; j < 100 && cnt < m; ++j) if(!vis[j]){
                    if(cnt)  putchar(' ');
                    printf("%d", j+1);
                    ++cnt;
                    vis[j] = true;
                }
            }
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/dwtfukgv/p/6086764.html