In-place Sorting ( 2021年度训练联盟热身训练赛第五场 )

本文介绍了一种通过翻转数字6和9实现序列非递减排序的算法。该算法适用于特定条件下的序列排序问题,通过对输入序列中6和9的巧妙替换,达到排序的目的。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

链接:https://ac.nowcoder.com/acm/contest/16741/H
来源:牛客网

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述

Woe is you – for your algorithms class you have to write a sorting algorithm, but you missed the relevant lecture! The subject was in-place sorting algorithms, which you deduce must be algorithms that leave each input number in its place and yet somehow also sort the sequence.

Of course you cannot change any of the numbers either, then the result would just be a difffferent sequence. But then it hits you: if you flflip a 6 upside-down, it becomes a 9, and vice versa! Certainly no one can complain about this since you changed none of the digits! The deadline to hand in the exercise is in fifive hours. Try to implement this sorting algorithm before then!

输入描述:

The input consists of:
• A line with an integer n (2 ≤ n ≤ 10 000), the number of integers in the input sequence.
• n lines, the ith of which contains a positive integer xi (1 ≤ xi ≤ 1018), the ith number of the sequence.

输出描述:

If the sequence cannot be sorted non-decreasingly by flipping some number 6 or 9 in input 1, output “not possible”. Otherwise, output “possible” followed by the sorted sequence - each number on its own line.
If there is more than one valid solution, please output the smallest sequence.

示例1
输入
4
9
7
7
9

输出
possible
6
7
7
9

示例2
输入
4
97
96
66
160

输出
possible
67
69
69
160

示例3
输入
3
80
97
79

输出
impossible

示例4

输入
2
197
166

输出
possible
167
169

题意:

给一个n,后面跟了n行数字,数字中的6和9可以进行互换,即6变为9,9变为6,判断是否能把给出的n个数字经过这些变换变为从小到大的顺序,如果可以,输出possible,并输出变化后的结果,尽可能小的,如果不能,则输出impossible。

思路:

一点点去模拟吧,主要的细节都注释在代码上了

代码:

#include<stdio.h>
#include<math.h>
#include<string>
#include<string.h>
#include<algorithm>
using namespace std;
int n;
struct node
{
    char a[20];
    int l;
} s[10010];
int main()
{
    scanf("%d",&n);
    for(int i=1; i<=n; i++)
    {
        scanf("%s",s[i].a);
        s[i].l=strlen(s[i].a);
    }
    for(int i=0; i<s[1].l; i++)//把第一个变为最小
    {
        if(s[1].a[i]=='9')
            s[1].a[i]='6';
    }
    int flag=0;
    for(int i=2; i<=n; i++)//字符串长度也可以理解为是数字的位数
    {
        if(s[i].l<s[i-1].l)//如果当前的字符串小于前一个字符串的长度,那么将永远也不会大于前一个数
        {
            flag=1;
            break;
        }
        if(s[i].l>s[i-1].l)//如果当前长度大于前一个,说明比前一个大,那么我们可以将这个数变为最小,即所有6都变为9,依然保证比前一个数大
        {
            for(int j=0; j<s[i].l; j++)
            {
                if(s[i].a[j]=='9')
                    s[i].a[j]='6';
            }
            continue;
        }
        if(s[i].l==s[i-1].l)//如果两个字符串的长度相等
        {
            for(int j=0; j<s[i].l; j++)//先将这个字符串变为最大值
            {
                if(s[i].a[j]=='6')
                    s[i].a[j]='9';
            }
            if(strcmp(s[i].a,s[i-1].a)<0)//判断是否大于前一个字符串,如果变为最大仍小于前一个字符串,说明不可能实现
            {
                flag=1;
                break;
            }
            else
            {
                for(int k=0; k<s[i].l; k++)//如果变为最大值后大于前一个值,那么就考虑把这个数变小
                {
                    if(s[i].a[k]=='9')
                        s[i].a[k]='6';//一位一位的变
                    if(strcmp(s[i].a,s[i-1].a)<0)//如果变化后小于前一个值了,就变回原来的样子
                        s[i].a[k]='9';
                }
            }
        }
    }
    if(flag==1)
        printf("impossible\n");
    else
    {
        printf("possible\n");
        for(int i=1;i<=n;i++)
            printf("%s\n",s[i].a);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值