AtCode:Rabbit Exercise(思维)

来源:互联网 发布:mysql中的update语句 编辑:程序博客网 时间:2024/06/05 07:41

C - Rabbit Exercise


Time limit : 2sec / Memory limit : 256MB

Score : 800 points

Problem Statement

There are N rabbits on a number line. The rabbits are conveniently numbered 1 through N. The coordinate of the initial position of rabbit i is xi.

The rabbits will now take exercise on the number line, by performing sets described below. A set consists of M jumps. The j-th jump of a set is performed by rabbit aj(2ajN1). For this jump, either rabbit aj1 or rabbit aj+1 is chosen with equal probability (let the chosen rabbit be rabbit x), then rabbit aj will jump to the symmetric point of its current position with respect to rabbit x.

The rabbits will perform K sets in succession. For each rabbit, find the expected value of the coordinate of its eventual position after K sets are performed.

Constraints

  • 3N105
  • xi is an integer.
  • |xi|109
  • 1M105
  • 2ajN1
  • 1K1018

Input

The input is given from Standard Input in the following format:

Nx1 x2  xNM Ka1 a2  aM

Output

Print N lines. The i-th line should contain the expected value of the coordinate of the eventual position of rabbit i after K sets are performed. The output is considered correct if the absolute or relative error is at most 10−9.


Sample Input 1

Copy
3-1 0 21 12

Sample Output 1

Copy
-1.01.02.0

Rabbit 2 will perform the jump. If rabbit 1 is chosen, the coordinate of the destination will be −2. If rabbit 3 is chosen, the coordinate of the destination will be 4. Thus, the expected value of the coordinate of the eventual position of rabbit 2 is 0.5×(−2)+0.5×4=1.0.


Sample Input 2

Copy
31 -1 12 22 2

Sample Output 2

Copy
1.0-1.01.0

xi may not be distinct.


Sample Input 3

Copy
50 1 3 6 103 102 3 4

Sample Output 3

Copy
0.03.07.08.010.0
题意:坐标轴上有N只兔子,有M个顺序操作,M[i]表示第M[i]只兔子要选择跳到左边相邻兔子的对称位置或跳到右边相邻兔子的对称位置,概率相等,这M个操作连续执行K次,输出最后从左到右每个兔子坐标的期望。

思路:对于兔子x[i],向左跳有2*x[i-1]-x[i],向右跳有2*x[i+1]-x[i],概率各位1/2,得期望x[i-1]+x[i+1]-x[i],令d[i] = x[i]-x[i-1],这条期望式子相当于swap(d[i], d[i+1]),那么连续执行K次,可参考快速幂的做法进行优化。O(NlogK)。

# include <stdio.h># include <algorithm>using namespace std;typedef long long LL;const int maxn = 1e5+3;int a[maxn], d[maxn], c[maxn], b[maxn], tmp[maxn];LL n, m, k, t;void qmod(){    while(k)    {        if(k&1)        {            for(int i=1; i<=n; ++i) tmp[i] = b[c[i]];            for(int i=1; i<=n; ++i) b[i] = tmp[i];        }        k >>= 1;        for(int i=1; i<=n; ++i) tmp[i] = c[c[i]];        for(int i=1; i<=n; ++i) c[i] = tmp[i];    }}int main(){    scanf("%lld",&n);    for(int i=1; i<=n; ++i)    {        scanf("%d",&a[i]);        d[i] = a[i]-a[i-1];        tmp[i] = b[i] = c[i] = i;    }    scanf("%lld%lld",&m,&k);    for(int i=1; i<=m; ++i)    {        scanf("%d",&t);        swap(c[t], c[t+1]);    }    qmod();    LL ans = 0;    for(int i=1; i<=n; ++i)    {        ans += (LL)d[b[i]];        printf("%lld.0\n",ans);    }    return 0;}



0 0
原创粉丝点击