AtCode:Rabbit Exercise(思维)
2017-05-07 22:52
357 查看
C - Rabbit Exercise
Time limit : 2sec / Memory limit : 256MBScore : 800 points
Problem Statement
There are N rabbits ona 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(2≤aj≤N−1).
For this jump, either rabbit aj−1 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
3≤N≤105xi is
an integer.
|xi|≤109
1≤M≤105
2≤aj≤N−1
1≤K≤1018
Input
The input is given from Standard Input in the following format:N x1 x2 … xN M K a1 a2 … aM
Output
Print N lines. The i-thline 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
Copy3 -1 0 2 1 1 2
Sample Output 1
Copy-1.0 1.0 2.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
Copy3 1 -1 1 2 2 2 2
Sample Output 2
Copy1.0 -1.0 1.0
xi may
not be distinct.
Sample Input 3
Copy5 0 1 3 6 10 3 10 2 3 4
Sample Output 3
Copy0.0 3.0 7.0 8.0 10.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;
}
相关文章推荐
- HDOJ 1222 Wolf and Rabbit ( GCD 和思维 )
- 面向对象的思维方法
- 思维拓展-记孩子疫苗接种登记
- Codeforces 817D Imbalanced Array【思维】好题!
- 面向对象的思维方法
- rabbitmq HA
- codeforces Karen and Coffee (区间贡献 思维)
- 关于敏捷思维的一个连接&敏捷宣言理解
- 互联网思维-标签思维(1)
- Codeforces Round #419 (Div. 2) C.Karen and Game 思维
- 一些不良的管理思维习惯!!!
- 开放的智力4:失败者思维
- poj 1182 食物链(思维+并查集)
- 思维的态度决定人生的高度
- “富思维”的8个习惯,你有吗?
- Hdu 4283 You Are the One【区间Dp+思维】好题!
- 培养正确的思维方式
- 转变思维--使用Python生成Shell命令,批量执行程序
- OpenJ_Bailian - 2787 算24 【思维 + dfs】
- 用微软试题膨胀你的思维