Gaby Ivanushka

Once upon a time there lived a tsar that has a daughter — Beautiful Vasilisa. There were many of the young men that wanted to marry her but she repelled all suitors. The tsar was tired of her jigs, he got angry and issued an order: "The first who solves my puzzle, will marry Vasilisa!" Ivanushka decided to try his fortune. He came to the tsar and the tsar told him: "This is a program for you. Input N numbers and the program will tell you who you should marry. I give you a day to think." Ivanuska looked at the program and got upset: there were unknown letters, omnifarious symbols. The time passed. Ivanushka has thought out nothing.
The program was as follows.
The C program The Pascal program
  1. #include <stdio.h>
  2. long c;
  3. long A[N];
  4.  
  5. long P(long l, long r)
  6. {
  7. long x=A[l],
  8. i=l-1,
  9. j=r+1,
  10. t;
  11. while(1)
  12. {
  13. do{--j; ++c;}
  14. while(A[j]>x);
  15. do{++i; ++c;}
  16. while(A[i]<x);
  17. if(i<j)
  18. {
  19. t=A[i];
  20. A[i]=A[j];
  21. A[j]=t;
  22. }
  23. else return j;
  24. }
  25. }
  26.  
  27. void Q(long l, long r)
  28. {
  29. long n;
  30. if(l<r)
  31. {
  32. n=P(l,r);
  33. Q(l,n);
  34. Q(n+1,r);
  35. }
  36. }
  37.  
  38. int main(void)
  39. {
  40. c=0;
  41. for(long i=0; i<N; ++i)
  42. scanf("%ld", &A[i]);
  43. Q(0,N-1);
  44. if(c==(N*N+3*N-4)/2)
  45. printf
  46. ("Beutiful Vasilisa");
  47. else printf
  48. ("Immortal Koshcei");
  49. return 0;
  50. }
  1. var A:array [1..N] of
  2. longint;
  3. c:longint;
  4. i:integer;
  5. function
  6. P(l,r:longint):longint;
  7. var i,j,t,x:longint;
  8. begin
  9. x:=A[l]; i:=l-1; j:=r+1;
  10. while true do
  11. begin
  12. repeat dec(j);inc(c)
  13. until A[j]<=x;
  14. repeat inc(i);inc(c)
  15. until A[i]>=x;
  16. if i<j then
  17. begin
  18. t:=A[i];
  19. A[i]:=A[j];
  20. A[j]:=t
  21. end
  22. else
  23. begin P:=j; exit end
  24. end
  25. end;
  26.  
  27. procedure Q(l,r:longint);
  28. var n:longint;
  29. begin
  30. if l<r then
  31. begin
  32. n:=P(l,r);
  33. Q(l,n);
  34. Q(n+1,r)
  35. end
  36. end;
  37.  
  38. begin
  39. c:=0;
  40. for i:=1 to N do
  41. read(A[i]);
  42. Q(1,N);
  43. if c=(N*N+3*N-4) div 2
  44. then
  45. writeln
  46. ('Beutiful Vasilisa')
  47. else writeln
  48. ('Immortal Koshcei');
  49. end.
Now you know this program. You may try to help Ivanushka.

Input

The first line of an input contains a positive number N ≤ 1000.

Output

You are to write to an output N numbers in one line. The tsar's program given those numbers should output a message "Beautiful Vasilisa" The numbers should be separated with a space. If several variants are possible choose any you like.

Example

input output
  1. 3
  1. 3 7 19

//代码就是快排。

输出一个等差数列即可

  1. #include <stdio.h>
  2. int main()
  3. {
  4. int n;
  5. while (scanf("%d",&n)!=EOF)
  6. {
  7. int i;
  8. for (i=;i<n;i++)
  9. printf("%d ",i);
  10. printf("%d\n",i);
  11. }
  12. }

Gaby Ivanushka(快排)的更多相关文章

  1. F#之旅4 - 小实践之快排

    参考文章:https://swlaschin.gitbooks.io/fsharpforfunandprofit/content/posts/fvsc-quicksort.html F#之旅4 - 小 ...

  2. 快排 快速排序 qsort quicksort C语言

    现在网上搜到的快排和我以前打的不太一样,感觉有点复杂,我用的快排是FreePascal里/demo/text/qsort.pp的风格,感觉特别简洁. #include<stdio.h> # ...

  3. iOS常见算法(二分法 冒泡 选择 快排)

    二分法: 平均时间复杂度:O(log2n) int halfFuntion(int a[], int length, int number)  { int start = 0; int end = l ...

  4. C++ 快排

    // 进行一轮快排并返回当前的中间数 int getMiddle( int* arr, int low, int high ) { auto swaparr = [&]( int i, int ...

  5. 先贴上代码:Random快排,快排的非递归实现

    设要排序的数组是A[0]……A[N-1],首先任意选取一个数据(通常选用数组的第一个数)作为主元,然后将所有比它小的数都放到它前面,所有比它大的数都放到它后面,这个过程称为一趟快速排序.值得注意的是, ...

  6. Java常见的几种排序算法-插入、选择、冒泡、快排、堆排等

    本文就是介绍一些常见的排序算法.排序是一个非常常见的应用场景,很多时候,我们需要根据自己需要排序的数据类型,来自定义排序算法,但是,在这里,我们只介绍这些基础排序算法,包括:插入排序.选择排序.冒泡排 ...

  7. ACM/ICPC 之 快排+归并排序-记录顺序对(TSH OJ-LightHouse(灯塔))

    TsingHua OJ 上不能使用<algorithm>头文件,因此需要手写快排(刚开始写的时候自己就出了很多问题....),另外本题需要在给横坐标排序后,需要记录纵坐标的顺序对的数量,因 ...

  8. 数组第K小数问题 及其对于 快排和堆排 的相关优化比较

    题目描述 给定一个整数数组a[0,...,n-1],求数组中第k小数 输入描述 首先输入数组长度n和k,其中1<=n<=5000, 1<=k<=n 然后输出n个整形元素,每个数 ...

  9. 结构体快排回顾(sort)

    一般来说,我做竞赛的时候排序一般用快排 很快很方便 普通sort(从小到大) sort(a,a+n); 直接贴一段代码吧,包含了vector,sort,结构体等简单东西综合 #include < ...

随机推荐

  1. 介绍下Shell中的${}、##和%%使用范例

    假设定义了一个变量为:代码如下:file=/dir1/dir2/dir3/my.file.txt可以用${ }分别替换得到不同的值:${file#*/}:删掉第一个 / 及其左边的字符串:dir1/d ...

  2. 【转】Linux 中清空或删除大文件内容的五种方法(truncate 命令清空文件)

    原文: http://www.jb51.net/article/100462.htm truncate -s 0 access.log -------------------------------- ...

  3. EffectiveJava(9)覆盖equals是总要覆盖hashCode

    覆盖equals是总要覆盖hashCode 通过散列函数将集合中不相等的实例均匀的分布在所有可能的散列值上 1.把某个非零的常数值保存在一个名为result的int类型变量中 2.对于对象中每个关键域 ...

  4. python项目导出所需要的依赖库

    使用pip freeze $ pip freeze > requirements.txt 这种方式是把整个环境中的包都列出来了,如果是虚拟环境可以使用. 通常情况下我们只需要导出当前项目的req ...

  5. C++11之function模板和bind函数适配器

    在C++98中,可以使用函数指针,调用函数,可以参考之前的一篇文章:类的成员函数指针和mem_fun适配器的用法.   简单的函数调用   对于函数: void foo(const string &a ...

  6. 安卓使用Socket发送中文,C语言服务端接收乱码问题解决方式

    今天用安卓通过Socket发送数据到电脑上使用C语言写的服务端,发送英文没有问题,可当把数据改变成中文时,服务端接收到的数据确是乱码. 突然想到.VS的预处理使用的是ANSI编码.而安卓网络数据都是U ...

  7. 64位windows2003 未在本地计算机上注册 microsoft.jet.oledb.4.0 提供程序

    64位windows2003系统 使用 mdb数据库时候出现如下错误. 可能用office的一些比较旧的程序时候会这样. 未在本地计算机上注册 microsoft.jet.oledb.4.0 提供程序 ...

  8. HBase 列族数量为什么越少越好

    http://blog.csdn.net/r1soft/article/details/63253985 http://www.cnblogs.com/nucdy/p/5965113.html

  9. 改变datagrid中指定单元格的值

    //自己设置编辑时显示的内容 $('#purchasegroupname'+index).html(name); //单元格真实内容 $('#material_datagrid').datagrid( ...

  10. openstack-计算节点安装(Node)

    感谢朋友支持本博客,欢迎共同探讨交流.因为能力和时间有限.错误之处在所难免,欢迎指正. 假设转载,请保留作者信息. 博客地址:http://blog.csdn.net/qq_21398167 原博文地 ...