sheep426 发表于 2005-7-20 18:20

[简单题]zju_2481

Unique Ascending Array

--------------------------------------------------------------------------------

Time limit: 1 Seconds   Memory limit: 32768K   
Total Submit: 1086   Accepted Submit: 501   

--------------------------------------------------------------------------------

Given an array of integers A, you are asked to decide the shortest array of integers B, such that the following two conditions hold.

For all integers 0 <= i < N, there exists an integer 0 <= j < M, such that A == B
For all integers 0 =< i < j < M, we have B < B
Notice that for each array A[] a unique array B[] exists.


Input

The input consists of several test cases. For each test case, an integer N (1 <= N <= 100) is given, followed by N integers A, A, ..., A in a line. A line containing only a zero indicates the end of input.


Output

For each test case in the input, output the array B in one line. There should be exactly one space between the numbers, and there should be no initial or trailing spaces.


Sample Input

8 1 2 3 4 5 6 7 8
8 8 7 6 5 4 3 2 1
8 1 3 2 3 1 2 3 1
0


Sample Output

1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3



Author: SHI, Xiaohan



很简单,不过我不过,会不会数据很大呢?

迷你大象 发表于 2005-7-20 19:12

int search(int array[],int number,int post){
        for(int i=post;i>=0;i--){
                if(number<array) continue;
                else if(number==array) {
                        return -1;
                }
                else if(number>array) {
                        return i+1;
                }
        }
        return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
        int count; // Size of A
        while(scanf(\"%d\",&count),count!=0){
                int b;
                int bi=-1;
                int location;
                int element;

                for(int i=0;i<count;i++){
                        scanf(\"%d\",&element); //读入一个元素
                        int location=search(b,element,bi);
                        if(location==-1)continue;

                        for(int j=bi+1;j>=location;j--){
                                b=b;
                        }
                        b=element;
                        bi++;
                }

                b=-1;

                int index=0;

                while(b!=-1){
                        printf(\"%d \",b);
                        index++;
                }
                printf(\"\\n\");
        }
}

sheep426 发表于 2005-7-20 19:51

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
        bool appear;
        ifstream cin(\"in.txt\");
        ofstream cout(\"out.txt\");
        unsigned long times,i,k,j,max;
        while(1)
        {
                cin >> times;
                max = 0;
                for(k = 0;k< 65536;k++)
                {
                        appear = false;
                }
                if(times == 0)
                {
                        break;
                }
                else
                {
                        for(j = 0;j < times;j++)
                        {
                                cin >> i;
                                appear = true;
                                if(i > max)
                                {
                                        max = i;
                                }
                        }
                }
                for(k = 0;k<=max;k++)
                {
                        if(k == max)
                        {
                                cout <<k<<endl;
                        }
                        else if(appear)
                        {
                                cout <<k<<\" \";
                        }
                }
               
        }
}
我就真的不知道错哪里了。。。。

小康 发表于 2005-7-20 22:01

这道题的意思说,在一个序列中,找出满足以下条件的序列B
序列B 的数字都要在A中出现.
序列B中的数字由小到大

可见,序列B是序列A中的数字的一个排序,但是不输出相同的数字

小康 发表于 2005-7-20 22:02

A了
#include<stdio.h>
int main()
{
        int shu,i,j,k,n;
        freopen(\"in.txt\",\"r\",stdin);
        while(scanf(\"%d\",&n),n)
        {
                for (i=0;i<n;i++) scanf(\"%d\",&shu);
                for (i=0;i<n;i++)//排序
                        for (j=i+1;j<n;j++)
                                if(shu<shu)
                                {
                                        k=shu;
                                        shu=shu;
                                        shu=k;
                                }
                printf(\"%d\",shu);
                for (i=1;i<n;i++) if(shu>shu)
                        printf(\" %d\",shu);
                printf(\"\\n\");


        }

        return 0;
}

剑乱 发表于 2005-7-21 18:54

小康的代码怎么比大象的少那么多???晕!!!!

海上飞洪 发表于 2005-7-23 20:41

不知道这样对不对?

#include<stdio.h>
main()
{
int a,b,i,j,k,n;
while(scanf(\"%d\",&n),n)
   {
    for(i=0;i<n;i++)
      scanf(\"%d\",&a);
    for(i=0;i<n;i++)
       {for(j=i+1;j<n;j++)
          if(a<a)
            {
             k=a;
             a=a;
             a=k;
            }
            b=a;
          }
printf(\"%d\",b);
for(i=1;i<n;i++)
   {
    if(b==b)continue;
    printf(\" %d\",b);
   }
printf(\"\\n\");
   }
}

小康 发表于 2005-7-24 08:09

两个程序基本都一样

海上飞洪 发表于 2005-7-24 08:58

你仔细看清楚了

海上飞洪 发表于 2005-7-24 08:59

你仔细看清楚了,老大

小康 发表于 2005-7-24 12:09

你个数组B好像不用也行吧,还是觉得思路程序都一样

海上飞洪 发表于 2005-7-25 01:16

嘻…………但它要求是有B数组的,不用就不符合题目要求了
小康是不是师兄来的?

小康 发表于 2005-7-25 08:55

你误解题目意思了.数组B,只是题目为了说明意思的.实际编程,你只要输出的答案完全正确就行了.程序要求内存越少,速度越快,答案对,不需要做出错出来.这样就AC

海上飞洪 发表于 2005-7-25 10:09

有道理,不愧是高手来的

小康 发表于 2005-7-25 19:45

你有兴趣加到做题群来一起做题
页: [1]
查看完整版本: [简单题]zju_2481