《算法艺术与信息学竞赛》之 排序与检索 Ural1082-Gaby Ivanushka
来源:互联网 发布:苏州网络推广外包 编辑:程序博客网 时间:2024/05/16 15:10
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
#include <stdio.h> long c; long A[N]; long P(long l, long r) { long x=A[l], i=l-1, j=r+1, t; while(1) { do{--j; ++c;} while(A[j]>x); do{++i; ++c;} while(A[i]<x); if(i<j) { t=A[i]; A[i]=A[j]; A[j]=t; } else return j; } } void Q(long l, long r) { long n; if(l<r) { n=P(l,r); Q(l,n); Q(n+1,r); } } int main(void) { c=0; for(long i=0; i<N; ++i) scanf("%ld", &A[i]); Q(0,N-1); if(c==(N*N+3*N-4)/2) printf ("Beutiful Vasilisa"); else printf ("Immortal Koshcei"); return 0; }
var A:array [1..N] of longint; c:longint; i:integer; function P(l,r:longint):longint; var i,j,t,x:longint; begin x:=A[l]; i:=l-1; j:=r+1; while true do begin repeat dec(j);inc(c) until A[j]<=x; repeat inc(i);inc(c) until A[i]>=x; if i<j then begin t:=A[i]; A[i]:=A[j]; A[j]:=t end else begin P:=j; exit end end end; procedure Q(l,r:longint); var n:longint; begin if l<r then begin n:=P(l,r); Q(l,n); Q(n+1,r) end end; begin c:=0; for i:=1 to N do read(A[i]); Q(1,N); if c=(N*N+3*N-4) div 2 then writeln ('Beutiful Vasilisa') else writeln ('Immortal Koshcei'); 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.
发现一道大水题,这个一看就是个快排,题目所需要的c就是快排时两端指针移动次数(这里快排的基准数每次选择的都是首位)。
很容易想到这个题目要求的输入数据一定满足什么特殊的规律,连蒙带猜就想到了递增的数列,然而我只会看书上说的用数学归纳法证明。。。
定义F(N)=(N^2+3*N-4)/2;
有F(1)=0=(1^2+3*1-4)/2;
假设F(k)=(k^2+3*k-4)/2;
可得F(k+1)=1+(k+1)+F(1)+F(k)=[(k+1)^2+3*(k+1)-4]/2;
至于代码,本来不想发的,因为没啥东西,但是还是发出来凑一波字数吧(虽然我不知道我为什么要凑字数)。
#include<cstdio>using namespace std;int n;int main(){ scanf("%d",&n); for(int i=1;i<n;i++) printf("%d ",i); printf("%d\n",n); return 0;}
- 《算法艺术与信息学竞赛》之 排序与检索 Ural1082-Gaby Ivanushka
- Ural1082-Gaby Ivanushka
- 《算法艺术与信息学竞赛》之 排序与检索 排序 uva120
- 《算法艺术与信息学竞赛》之 排序与检索 二分 URAL 1184 电缆
- 《算法艺术与信息学竞赛》之 排序与检索 ZOJ1388 Exchanges
- 《算法艺术与信息学竞赛》之 贪心 例三 Mirror
- 《算法艺术与信息学竞赛》之 并查集 团伙
- 《算法艺术与信息学竞赛》之 递归与分治法 例一 折纸痕
- 《算法艺术与信息学竞赛》之 递归与分治法 例二 三色多边形
- 《算法艺术与信息学竞赛》之 递归与分治法 例三 聪明的学生
- 《算法艺术与信息学竞赛》之 递归与分治法 例四 丢失的数
- ACM 算法艺术与信息学竞赛 1.2.1 图书馆
- ACM 算法艺术与信息学竞赛 1.2.4 售货员
- ACM题目推荐--《算法艺术与信息学竞赛》
- 《算法艺术与信息学竞赛》题目-提交方式对照表
- ACM题目推荐--《算法艺术与信息学竞赛》
- 算法艺术与信息学竞赛的相关题目
- 算法艺术与信息学竞赛——习题解答
- C++图像批处理:读取文件夹中全部图像的方法
- Your iOS Distribution Certificate will expire in 30 days
- 探究android:largeHeap
- 图数据库:3 使用图进行数据建模
- layui数据表格
- 《算法艺术与信息学竞赛》之 排序与检索 Ural1082-Gaby Ivanushka
- CPM CPT CPC CPA CPS广告营销模式
- @EnableAutoCongiguration详解
- oracle怎么增加自增列(我的两种常用方法)
- 配置(8) Ubuntu14.04安装wxPython3.0.1.1
- https协议使用配置概述
- 在node.js 中使用async await
- Mac 终端下删除一个工程项目中的所有.svn文件
- Eclipse解决Cannot change version of project facet Dynamic Web Module to 3.0.