Sicily 1351. Multi-key Sorting

1351. Multi-key Sorting

Constraints

Time Limit: 10 secs, Memory Limit: 32 MB

Description

Consider a table with rows and columns. The columns are numbered from 1 to C. For simplicity's sake, the items in the table are strings consisting of lower case letters.

1

2

3

XX

 

1

2

3

XX

 

1

2

3

apple

red

sweet

 

banana

brown

rotten

 

apple

green

sour

apple

green

sour

 

apple

green

sour

 

apple

red

sweet

pear

green

sweet

 

pear

green

sweet

 

banana

brown

rotten

banana

yellow

sweet

 

apple

red

sweet

 

banana

yellow

sweet

banana

brown

rotten

 

banana

yellow

sweet

 

pear

green

sweet

Table 1                           Table 2                               Table 3

You are given the operation Sort(k) on such tables: Sort(k) sorts the rows of a table in the order of the values in column k (while the order of the columns does not change). The sort is stable, that is, rows that have equal values in column k, remain in their original order. For example, applying Sort(2) to Table 1 yields Table 2.

We are interested in sequences of such sort operations. These operations are successively applied to the same table. For example, applying the sequence Sort(2); Sort(1) to Table 1 yields Table 3.

Two sequences of sort operations are called equivalent if, for any table, they have the same effect. For example, Sort(2); Sort(2); Sort(1) is equivalent to Sort(2); Sort(1). However, it is not equivalent to Sort(1); Sort(2), because the effect on Table 1 is different.

Task

Given a sequence of sort operations, determine a shortest equivalent sequence.

Input

注意:输入包含多个测试数据。

The first line of the input contains two integers, C and NC (1 ≤ C ≤
 1 000 000
) is the number of columns and N (1 ≤ N ≤ 3 000 000) is the number of sort operations. The second line contains N integers, ki (1 ≤ ki ≤ C). It defines the sequence of sort operations Sort(k1); ...; Sort(kN).

Output

The first line of the output contains one integer, M, the length of the shortest sequence of sort operations equivalent to the input sequence (Subtask A). The second line contains exactly M integers, representing a shortest sequence (Subtask B). You can omit the second line if you solve only Subtask A.

Sample Input

4 61 2 1 2 3 3

Sample Output

31 2 3

// Problem#: 1351
// Submission#: 3286198
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <iomanip>
#include <algorithm>
#include <queue>
#include <functional>
using namespace std;

const int MAX_C = 1000005;
bool lastSort[MAX_C];
int sortCol[MAX_C];
char text[3000000 + 4 * MAX_C];
int C, N, M;

int main() {

    //std::ios::sync_with_stdio(false);

    while (scanf("%d%d\n", &C, &N) != EOF) {

        gets(text);

        M = 0;

        int endPos = 0, sum = 0, counter = 1;
        while (text[endPos++] != '\0');

        for (int i = endPos - 2; i >= 0; i--) {
            if (text[i] == ' ') {
                if (!lastSort[sum]) sortCol[M++] = sum;
                lastSort[sum] = true;
                sum = 0;
                counter = 1;
            } else {
                sum += counter * (text[i] - '0');
                counter *= 10;
            }
        }

        if (!lastSort[sum]) sortCol[M++] = sum;
        lastSort[sum] = true;

        printf("%d\n", M);
        for (int i = M - 1; i > 0; i--) {
            printf("%d ", sortCol[i]);
            lastSort[sortCol[i]] = false;
        }
        printf("%d\n", sortCol[0]);
        lastSort[sortCol[0]] = false;
    }

    return 0;
}                                 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值