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(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≤105
- xi 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:
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
3-1 0 21 12
Sample Output 1
-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
31 -1 12 22 2
Sample Output 2
1.0-1.01.0
xi may not be distinct.
Sample Input 3
50 1 3 6 103 102 3 4
Sample Output 3
0.03.07.08.010.0
思路:对于兔子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;}
- AtCode:Rabbit Exercise(思维)
- [agc006c]Rabbit Exercise
- 【AGC006C】Rabbit Exercise
- Rabbit
- Rabbit
- rabbit
- rabbit
- hdu 4777 Rabbit Kingdom(离线树状数组&思维)
- HDOJ 1222 Wolf and Rabbit ( GCD 和思维 )
- Exercise
- exercise
- Exercise
- exercise
- rabbit mq研究(一)
- rabbit mq研究(二)
- Wolf and Rabbit(最大公约数)
- Wolf and Rabbit(gcd)
- Wolf and Rabbit(欧几里得)
- 杭电1728逃离迷宫 BFS 搜索
- 计划与想法
- 本地mongodb配置
- 数组or字符串
- Scala数据类型
- AtCode:Rabbit Exercise(思维)
- C++中typename和class的区别
- 备考PMP第八天
- 4. Median of Two Sorted Arrays LeetCode题解
- 实现一个整数计算器,程序可以接受三个参数,第一个参数“-a”选项执行加法,“-s”选项执行减法,“-m”选项执行乘法,“-d”选项执行除法,后面两个参数为操作数。 例如:输入t
- 抽象类和接口的区别
- BZOJ 4631 踩气球
- Linux 打开端口
- 算法竞赛入门经典(刘汝佳)习题4-1 UVa1589