zoukankan      html  css  js  c++  java
  • Kattis -Bus Numbers

    Bus Numbers

    Your favourite public transport company LS (we cannot use their real name here, so we permuted the letters) wants to change signs on all bus stops. Some bus stops have quite a few buses that stop there and listing all the buses takes space. However, if for example buses 141, 142, 143 stop there, we can write 141–143 instead and this saves space. Note that just for two buses this does not save space.

    You are given a list of buses that stop at a bus stop. What is the shortest representation of this list?

    Input

    The first line of the input contains one integer N,1N1000N,1≤N≤1000, the number of buses that stop at a bus stop. Then next line contains a list of NN space separated integers between 1 and 10001000, which denote the bus numbers. All numbers are distinct.

    Output

    Print the shortest representation of the list of bus numbers. Use the format as in the example, separate numbers with single spaces and output them in sorted order.

    Sample Input 1Sample Output 1
    6
    180 141 174 143 142 175
    
    141-143 174 175 180

    题意

    将给出的数字按照升序排好,然后是连续的就连起来直接输出

    代码

    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    #include<algorithm>
    using namespace std;
    #pragma warning(disable:4996);
    int a[1005],n;
    int main() {
        while (~scanf("%d", &n))
        {
            for (int i = 0; i < n; i++)
            {
                scanf("%d", &a[i]);
            }
            sort(a, a + n);
            int key = 0;
            printf("%d", a[0]);
            if (a[0] + 1 == a[1])key++;
            for (int i = 1; i < n; i++)
            {    
                if (key == 0 && a[i] + 1 == a[i + 1])
                {
                    printf(" %d", a[i]);
                    key++;
                }
                else if (key &&a[i] + 1 == a[i + 1])
                {
                    key++;
                    continue;
                }
                else if (a[i + 1]!= a[i] + 1)
                {
                    if (key>1)
                    {
                        key = 0;
                        printf("-%d", a[i]);
                    }
                    else
                    {
                        key = 0;
                        printf(" %d", a[i]);
                    }
                }
            }puts("");
        }
        return 0;
    }
  • 相关阅读:
    洛谷 P2677 超级书架 2 题解
    洛谷 P3957 跳房子 二分+DP检验+单调队列优化
    BZOJ 1030 AC自动机+DP
    SPOJ-EPALIN 字符串哈希 回文
    URAL-1989 树状数组+字符串哈希
    POJ-2774 字符串哈希+二分
    CCF 201709-5 除法(线段树)
    CCF CSP个人题解汇总
    CCF CSP历年一二题代码汇总
    CCF 201803-4 棋局评估 (对抗搜索)
  • 原文地址:https://www.cnblogs.com/zhien-aa/p/6285884.html
Copyright © 2011-2022 走看看