codeforces712

There are n integers b1, b2, …, bn written in a row. For all i from 1 to n, values ai are defined by the crows performing the following procedure:

The crow sets ai initially 0.
The crow then adds bi to ai, subtracts bi + 1, adds the bi + 2 number, and so on until the n’th number. Thus, ai = bi - bi + 1 + bi + 2 - bi + 3….
Memory gives you the values a1, a2, …, an, and he now wants you to find the initial numbers b1, b2, …, bn written in the row? Can you do it?

Input
The first line of the input contains a single integer n (2 ≤ n ≤ 100 000) — the number of integers written in the row.

The next line contains n, the i’th of which is ai ( - 109 ≤ ai ≤ 109) — the value of the i’th number.

Output
Print n integers corresponding to the sequence b1, b2, …, bn. It’s guaranteed that the answer is unique and fits in 32-bit integer type.

Example
Input
5
6 -4 8 -2 3
Output
2 4 6 1 3
Input
5
3 -2 -1 5 6
Output
1 -3 4 11 6
Note
In the first sample test, the crows report the numbers 6, - 4, 8, - 2, and 3 when he starts at indices 1, 2, 3, 4 and 5 respectively. It is easy to check that the sequence 2 4 6 1 3 satisfies the reports. For example, 6 = 2 - 4 + 6 - 1 + 3, and  - 4 = 4 - 6 + 1 - 3.

In the second sample test, the sequence 1,  - 3, 4, 11, 6 satisfies the reports. For example, 5 = 11 - 6 and 6 = 6.

本题水题,直接推一发。

#include<bits/stdc++.h>
using namespace std;
#define N 1000001
int a[N];
int n;
int b[N];
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i];
    b[n]=a[n];
    for(int i=n-1;i>=1;i--)
    {
        b[i]=a[i]+a[i+1];
    }
    for(int i=1;i<=n;i++) cout<<b[i]<<" ";
}

Memory is performing a walk on the two-dimensional plane, starting at the origin. He is given a string s with his directions for motion:

An ‘L’ indicates he should move one unit left.
An ‘R’ indicates he should move one unit right.
A ‘U’ indicates he should move one unit up.
A ‘D’ indicates he should move one unit down.
But now Memory wants to end at the origin. To do this, he has a special trident. This trident can replace any character in s with any of ‘L’, ‘R’, ‘U’, or ‘D’. However, because he doesn’t want to wear out the trident, he wants to make the minimum number of edits possible. Please tell Memory what is the minimum number of changes he needs to make to produce a string that, when walked, will end at the origin, or if there is no such string.

Input
The first and only line contains the string s (1 ≤ |s| ≤ 100 000) — the instructions Memory is given.

Output
If there is a string satisfying the conditions, output a single integer — the minimum number of edits required. In case it’s not possible to change the sequence in such a way that it will bring Memory to to the origin, output -1.

Example
Input
RRU
Output
-1
Input
UDUR
Output
1
Input
RUUR
Output
2
Note
In the first sample test, Memory is told to walk right, then right, then up. It is easy to see that it is impossible to edit these instructions to form a valid walk.

In the second sample test, Memory is told to walk up, then down, then up, then right. One possible solution is to change s to “LDUR”. This string uses 1 edit, which is the minimum possible. It also ends at the origin.

B题就是记录对立方向的差的和,为奇数不可能,为偶数除以2输出,即可。

using namespace std;
#include<bits/stdc++.h>
#define N 1000001
char it[N];
int n,m;
int dir[128];
int main()
{
    scanf("%s",it+1);
    int len=strlen(it+1);
    if(len%2==1){puts("-1");return 0;}
    for(int i=1;i<=len;i++) dir[it[i]]++;
    int l=dir['L'],r=dir['R'],u=dir['U'],d=dir['D'];
    int k=abs(l-r)+abs(u-d);
    cout<<k/2<<endl;
}

Memory is now interested in the de-evolution of objects, specifically triangles. He starts with an equilateral triangle of side length x, and he wishes to perform operations to obtain an equilateral triangle of side length y.

In a single second, he can modify the length of a single side of the current triangle such that it remains a non-degenerate triangle (triangle of positive area). At any moment of time, the length of each side should be integer.

What is the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y?

Input
The first and only line contains two integers x and y (3 ≤ y < x ≤ 100 000) — the starting and ending equilateral triangle side lengths respectively.

Output
Print a single integer — the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y if he starts with the equilateral triangle of side length x.

Example
Input
6 3
Output
4
Input
8 5
Output
3
Input
22 4
Output
6
Note
In the first sample test, Memory starts with an equilateral triangle of side length 6 and wants one of side length 3. Denote a triangle with sides a, b, and c as (a, b, c). Then, Memory can do .

In the second sample test, Memory can do .

In the third sample test, Memory can do:

.

三角形的贪心,使每次最大的边最大,只要有一条到了目标,其他两条两次就能到

using namespace std;
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
int x[4];
int y[4];
int st,en;
int step;
int MAX(int xx,int yy,int zz)
{
    return max(max(xx,yy),zz);
}
int main()
{
    cin>>st>>en;
    if(st<en) swap(st,en);
    if(st==en){puts("0");return 0;}
    if(st<en*2) {puts("3");return 0;}
    x[1]=en;x[2]=en;x[3]=en;
    while(MAX(x[1],x[2],x[3])<st)
    {
        step++;
        int t1=x[1],no=1;
        for(int i=2;i<=3;i++)
        {
            if(x[i]<t1)
            {
                t1=x[i];no=i;
            }
        }
        x[no]=x[1]+x[2]+x[3]-x[no]-1;
    }
    cout<<step+2<<endl;
}

Memory and his friend Lexa are competing to get higher score in one popular computer game. Memory starts with score a and Lexa starts with score b. In a single turn, both Memory and Lexa get some integer in the range [ - k;k] (i.e. one integer among  - k,  - k + 1,  - k + 2, …,  - 2,  - 1, 0, 1, 2, …, k - 1, k) and add them to their current scores. The game has exactly t turns. Memory and Lexa, however, are not good at this game, so they both always get a random integer at their turn.

Memory wonders how many possible games exist such that he ends with a strictly higher score than Lexa. Two games are considered to be different if in at least one turn at least one player gets different score. There are (2k + 1)2t games in total. Since the answer can be very large, you should print it modulo 109 + 7. Please solve this problem for Memory.

Input
The first and only line of input contains the four integers a, b, k, and t (1 ≤ a, b ≤ 100, 1 ≤ k ≤ 1000, 1 ≤ t ≤ 100) — the amount Memory and Lexa start with, the number k, and the number of turns respectively.

Output
Print the number of possible games satisfying the conditions modulo 1 000 000 007 (109 + 7) in one line.

Example
Input
1 2 2 1
Output
6
Input
1 1 1 2
Output
31
Input
2 12 3 1
Output
0
Note
In the first sample test, Memory starts with 1 and Lexa starts with 2. If Lexa picks  - 2, Memory can pick 0, 1, or 2 to win. If Lexa picks  - 1, Memory can pick 1 or 2 to win. If Lexa picks 0, Memory can pick 2 to win. If Lexa picks 1 or 2, Memory cannot win. Thus, there are 3 + 2 + 1 = 6 possible games in which Memory wins.

前缀和优化DP,下次遇到一段连一起的,一定要考虑这种算法!
dp用k次取x的方案数

using namespace std;
#include<bits/stdc++.h>
#define LL long long
#define N 200666
LL dance[101][N+5];
LL res;
LL S[N+5];
LL a,b,k,t;
const LL mod=1e9+7;
#define F(i,a,b) for(int i=a;i<=b;i++)
int fire;
int main()
{
    cin>>a>>b>>k>>t;
    dance[0][0]=1;
    fire=0;
    F(i,1,t)
    {
        fire^=1;
        S[0]=dance[fire^1][0]%mod+mod;
        S[0]%=mod;
        F(j,1,N)
        {
            S[j]=S[j-1]+dance[fire^1][j]+mod;
            S[j]=S[j]%mod;
        }
        dance[fire][0]=dance[fire^1][0]+mod;
        dance[fire][0]%=mod;
        F(j,1,N)
        {
            int kk=j-2*k-1;
            LL it=0;
            if(kk<0) it=0;else it=S[kk];
            dance[fire][j]=S[j]-it+mod;
            dance[fire][j]=dance[fire][j]%mod;
        }
    }
//  for(int i=1;i<=t;i++){cout<<endl;for(int j=1;j<=20;j++)cout<<dance[i][j]<<endl;}
    S[0]=dance[fire][0]+mod;S[0]%=mod;
    F(i,1,N)
    {
        S[i]=S[i-1]+dance[fire][i]+mod;
        S[i]%=mod;
    }
    LL hh=b-a;
    F(i,0,N)
    {
        LL fi=i-hh-1;LL it;
        LL dd=1;
        if(fi<0) it=0;else it=S[fi]%mod;
        res+=dd*it*dance[fire][i];
        res=res%mod;
    }
    cout<<res;
}

There are n casinos lined in a row. If Memory plays at casino i, he has probability pi to win and move to the casino on the right (i + 1) or exit the row (if i = n), and a probability 1 - pi to lose and move to the casino on the left (i - 1) or also exit the row (if i = 1).

We say that Memory dominates on the interval i… j if he completes a walk such that,

He starts on casino i.
He never looses in casino i.
He finishes his walk by winning in casino j.
Note that Memory can still walk left of the 1-st casino and right of the casino n and that always finishes the process.

Now Memory has some requests, in one of the following forms:

1 i a b: Set .
2 l r: Print the probability that Memory will dominate on the interval l… r, i.e. compute the probability that Memory will first leave the segment l… r after winning at casino r, if she starts in casino l.
It is guaranteed that at any moment of time p is a non-decreasing sequence, i.e. pi ≤ pi + 1 for all i from 1 to n - 1.

Please help Memory by answering all his requests!

Input
The first line of the input contains two integers n and q(1 ≤ n, q ≤ 100 000), — number of casinos and number of requests respectively.

The next n lines each contain integers ai and bi (1 ≤ ai < bi ≤ 109) — is the probability pi of winning in casino i.

The next q lines each contain queries of one of the types specified above (1 ≤ a < b ≤ 109, 1 ≤ i ≤ n, 1 ≤ l ≤ r ≤ n).

It’s guaranteed that there will be at least one query of type 2, i.e. the output will be non-empty. Additionally, it is guaranteed that p forms a non-decreasing sequence at all times.

Output
Print a real number for every request of type 2 — the probability that boy will “dominate” on that interval. Your answer will be considered correct if its absolute error does not exceed 10 - 4.

Namely: let’s assume that one of your answers is a, and the corresponding answer of the jury is b. The checker program will consider your answer correct if |a - b| ≤ 10 - 4.

Example
Input
3 13
1 3
1 2
2 3
2 1 1
2 1 2
2 1 3
2 2 2
2 2 3
2 3 3
1 2 2 3
2 1 1
2 1 2
2 1 3
2 2 2
2 2 3
2 3 3
Output
0.3333333333
0.2000000000
0.1666666667
0.5000000000
0.4000000000
0.6666666667
0.3333333333
0.2500000000
0.2222222222
0.6666666667
0.5714285714
0.6666666667

一道期望的题,转化区间转化的很好,Annox给我的解很好,很易懂。
这里写图片描述

这里写图片描述
大致思路就是这样的

//把l当成1号节点的方法实在是太妙了 
using namespace std;
#include<cstdio>
#include<iostream>
#include<cstring>
#define N 400001
#define lc no<<1
#define rc no<<1|1
#define fire_dancer
#define getmid int mid=(L[no]+R[no])>>1
double fire[N],dance[N];
int L[N],R[N],n,q;
double p[N];
int x,y,u,ty;
double despacito,Timsei;
#define eps 1e-8
void pushup(int no)
{
    fire[no]=fire[lc]*fire[rc]+eps;
    dance[no]=dance[lc]+fire[lc]*dance[rc]+eps;
}
void build(int no,int l,int r)
{
    L[no]=l;R[no]=r;getmid;
    if(l==r)
    {
        scanf("%d%d",&x,&y);
        p[mid]=(double)x/double(y)+eps;
        fire[no]=(1-p[mid])/p[mid]+eps;
        dance[no]=fire[no]+eps;
        return;
    }
    build(lc,l,mid);build(rc,mid+1,r);
    pushup(no);
}
void change(int no,int num)
{
//  cout<<L[no]<<" "<<R[no]<<endl;
    if(L[no]==R[no])
    {
        double pp=double(x)/double(y)+eps;
        fire[no]=(1-pp)/pp+eps;
        dance[no]=fire[no]+eps;
        return;
    }
    getmid;
    if(num>mid) change(rc,num);
    else change(lc,num);
    pushup(no);
}
void query(int no,int l,int r)
{
    if(L[no]==l&&R[no]==r)
    {
        Timsei+=dance[no]*despacito+eps;
        despacito*=fire[no]+eps;
        return;
    }
    getmid;
    if(l>mid) query(rc,l,r);
    else if(r<=mid) query(lc,l,r);
    else
    {
        query(lc,l,mid);query(rc,mid+1,r);return ;
    }
}
int main()
{
    cin>>n>>q;
    build(1,1,n);
    while(q--)
    {
        scanf("%d",&ty);
        if(ty==1)
        {
            scanf("%d%d%d",&u,&x,&y);
            change(1,u);
        }
        else
        {
            scanf("%d%d",&x,&y);
            despacito=1;Timsei=0;
            query(1,x,y);
            if(Timsei>1e15){puts("0.000000000");continue;}
            else 
            {
                double ans=1.0/(1+Timsei)+eps;
                printf("%.10lf\n",(ans));
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值