GoroSort

                                                                              GoroSort

题目大意:给你一串数字,这串数字的顺序是打乱的,但是是1~n之间的不重复的数字,现在有一个人能够将一部分数字按住不动,而用另一只手将余下的这些数字的顺序改变,但是这种改变是没有规则但均等概率出现的重新排列。要求你求出在最坏的情况下至少要改变多少次顺序才能保证这串数字升序排列;
例如对于:1 3 2,假设摁住1不动,而将3 2的顺序变动的话可能会出现 1 3 2 和1 2 3两种情况。故在最坏的情况下至少需要两次才能使1 3 2按升序排列;

这道题是google code jam 2011的第四道题:

解题思路:

假设只有两个数字不在自己的位置上时,必然会存在将这2个数字位置调换之后就是正确的。而且我们也可以肯定,不满足升序排列的数字都是成对出现的,且在最坏的情况下对2个数字进行随机排列需要两次才能得到正确的排列,故我们可以知道,这个题其实就是找出不再自己位置上的数的个数即可;

当然这个只是我对这道题目的yy但是我觉得这样理解也可以,从全局思想出发,而忽略局部的细节。如果有感兴趣的同学也可以看看analysis:

The Solution

This problem is very mathematical in nature, requiring a lot of thought and only a little code to solve correctly. We put it as a problem on the Qualifying Round to give you something different to try without having to worry about time pressure or advancement. We hope you enjoyed it!

For an arbitrary array A, let n(A) be the number of elements that are not already in the correct position. It turns out that the answer you are looking for is simply n(A). Once you realize this, it is easy to calculate with a simple loop, but how do you prove it?


The Proof

Let's first show that the expected number of hits is never more than n(A). Suppose Goro always holds down only those elements that are already in position, and then he randomly permutes the rest. Let x(A) be the expected number of hits required for him to sort A using this strategy.

Lemma: x(A) = n(A) for all A.

We prove this by induction on n(A). If n(A) = 0, then the array is already sorted, and we're done. To set up the induction, let's suppose we have proven the lemma already for smaller values of n(A) and we are now trying to prove it for A. Let pt be the probability that exactly t elements are still out of position after the first hit, and let x't be the expected number of hits required in this case. We make three observations:

  • p0 * 0 + p1 * 1 + p2 * 2 + ... + pN * N = N - 1. In English, this is saying that theexpected number of elements that are out of position after the first hit is exactly N - 1, or equivalently, the expected number of elements that are put into position by the first hit is exactly 1. This follows from "linearity of expectation": Goro is permuting N elements; each one has probability exactly 1/N of ending up in the correct position, and hence, the expected number of elements that end up in the correct position is N * 1/N = 1.
  • x't = t for t ≤ N - 1. This is true by the inductive hypothesis.
  • x'N = x(A). If no elements are put into the correct position by the first hit, then we will just randomly permute them all again in the next step, so nothing has changed, and hence x'N = x(A).
Now let's write down a formula for x(A):

1 + p0 * x'0 + p1 * x'1 + ... + pN * x'N
= 1 + p0 * 0 + p1 * 1 + ... + pN * N + pN * (x(A) - N)
= N + pN * (x(A) - N),

which simplifies to (N - x(A)) * (1 - pN) = 0. Since pN < 1, we must have x(A) = N, and the lemma is proven.


To complete the proof, we need to calculate y(A), the expected number of hits required for Goro to sort A if he uses the (still unknown) optimal strategy. Since y(A) ≤ x(A) by definition, we have already proven y(A) ≤ n(A).

To conversely prove n(A) ≤ y(A), it would be nice to just extend the proof of the previous lemma. There is one big technical issue though: it is possible for n(A) to go up if Goro doesn't hold down enough elements, and so it is tricky to set up an induction on n(A). We'll resolve this by having a separate proof for this part, and this time use a slightly different induction hypothesis. We can then follow the previous argument very closely.

Lemma 2: Let K be a non-negative integer. Then for any k ≤ K, the statement y(A) = k is equivalent to the statement n(A) = k.

We will prove this by induction on K. Both y(A) = 0 and n(A) = 0 are equivalent to the array already being sorted, so the K = 0 case is clear. Now, let's suppose we have proven the lemma for K already, and are trying to prove it for K+1. Choose A such that y(A) is the smallest possible value larger than K and consider the optimal strategy for Goro. Let T be the number of elements that are either (a) not in the correct position in A, or (b) permuted when Goro hits the table. Define pi and x'i as we did before. Note that T ≥ n(A) ≥ K+1 by the inductive hypothesis.

As in the previous lemma, we can now prove the following:

  • p0 * 0 + p1 * 1 + p2 * 2 + ... pT * T ≥ T - 1.
  • x'i = i for i ≤ K. This follows directly from the inductive hypothesis.
  • x'i ≥ y(A) for i > K. By the inductive hypothesis, n(A') > K implies y(A') > K, which then implies y(A') ≥ y(A).

As before, we can now write y(A) as

1 + p0 * x'0 + p1 * x'1 + ... pT * x'T
≥ 1 + (p0 * 0 + p1 * 1 + ... + pT * T) + (pK+1 + pK+2 + ... + pT) * (y(A) - T)
≥ T + (pK+1 + pK+2 + ... + pT) * (y(A) - T)

which simplifies to (y(A) - T) * (1 - pK+1 - ... - pT) ≥ 0. The second term has to be positive (if not, then y(A) ≥ min(x'K+1, x'K+2, ...) + 1, which contradicts the third bullet point above is therefore impossible), so we must have y(A) ≥ T ≥ n(A) ≥ K+1. Equality holds only if n(A) = K+1. The first lemma guarantees y(A) ≤ x(A) ≤ K+1 in this case, and the proof is complete!


Comments

  • If you go over the proof carefully, you can see there are two things Goro needs to do in order to be optimal. (1) He needs to always hold down elements that are already in the correct position, and (2) he needs to ensure that for each element x that is permuted, the element in x's correct position is also permuted. This means he actually has some choice about what to do.
  • On a programming contest, of course you do not need to work through a formal proof to implement a correct solution. The best contestants can solve this kind of problem by looking at small examples to see a pattern, and then using intuitive reasoning to see what's going on without formalizing everything. Mastering this kind of reasoning is a difficult art though!
英语很渣,就不解释那么多了;

大概意思就是 假设N个数组,里面全部都是没有排序好的,那么拍一次,对于数组中任意的数字,拍一次,它落回正确位置的概率为1/N。假设,拍完一次,有I个数字落回了原来的位置,那么对于没有落回原来位置的数字肯定没有落在这I个数字的位置上,如果落在了这I个数字的上面,则这I个数字肯定就是错误的,因此概率为(N-I)/N,接下来,按住I个正确的,拍一次,落回原来位置的概率为1/N-I,两者相乘的概率依然为1/N,因此一个数组正确排序的期望为整个数组中没有正确排序的数字。

我的代码:

#include<stdio.h>
int main(){
int i,a,ans,t,n;
scanf("%d",&t);
int c=1;
while(t--){
   scanf("%d",&n);
   ans=0;
   for(i=1;i<=n;i++){
      scanf("%d",&a);
     if(a!=i)
      ans++;
  }
printf("Case #%d: %d.000000\n",c++,ans);
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
智慧校园整体解决方案是响应国家教育信息化政策,结合教育改革和技术创新的产物。该方案以物联网、大数据、人工智能和移动互联技术为基础,旨在打造一个安全、高效、互动且环保的教育环境。方案强调从数字化校园向智慧校园的转变,通过自动数据采集、智能分析和按需服务,实现校园业务的智能化管理。 方案的总体设计原则包括应用至上、分层设计和互联互通,确保系统能够满足不同用户角色的需求,并实现数据和资源的整合与共享。框架设计涵盖了校园安全、管理、教学、环境等多个方面,构建了一个全面的校园应用生态系统。这包括智慧安全系统、校园身份识别、智能排课及选课系统、智慧学习系统、精品录播教室方案等,以支持个性化学习和教学评估。 建设内容突出了智慧安全和智慧管理的重要性。智慧安全管理通过分布式录播系统和紧急预案一键启动功能,增强校园安全预警和事件响应能力。智慧管理系统则利用物联网技术,实现人员和设备的智能管理,提高校园运营效率。 智慧教学部分,方案提供了智慧学习系统和精品录播教室方案,支持专业级学习硬件和智能化网络管理,促进个性化学习和教学资源的高效利用。同时,教学质量评估中心和资源应用平台的建设,旨在提升教学评估的科学性和教育资源的共享性。 智慧环境建设则侧重于基于物联网的设备管理,通过智慧教室管理系统实现教室环境的智能控制和能效管理,打造绿色、节能的校园环境。电子班牌和校园信息发布系统的建设,将作为智慧校园的核心和入口,提供教务、一卡通、图书馆等系统的集成信息。 总体而言,智慧校园整体解决方案通过集成先进技术,不仅提升了校园的信息化水平,而且优化了教学和管理流程,为学生、教师和家长提供了更加便捷、个性化的教育体验。
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值