zoukankan      html  css  js  c++  java
  • Codeforces Round #370 (Div. 2) A , B , C 水,水,贪心

    A. Memory and Crow
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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.

    Examples
    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 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.

     思路:a[i]+a[i+1];

    #include<bits/stdc++.h>
    using namespace std;
    #define ll long long
    #define pi (4*atan(1.0))
    const int N=1e5+10,M=4e6+10,inf=1e9+10,mod=1e9+7;
    const ll INF=1e18+10;
    ll a[N];
    int main()
    {
        int x;
        scanf("%d",&x);
        for(int i=1;i<=x;i++)
            scanf("%lld",&a[i]);
        for(int i=1;i<=x;i++)
        printf("%lld ",a[i]+a[i+1]);
        return 0;
    }
    B. Memory and Trident
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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.

    Examples
    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.

     题意:上下左右的走,问最少变几步可以回到原点;

    思路:奇数步,显然不能回到,ans=(abs(l-r)+abs(u-d))/ 2;

    #include<bits/stdc++.h>
    using namespace std;
    #define ll long long
    #define pi (4*atan(1.0))
    const int N=1e5+10,M=4e6+10,inf=1e9+10,mod=1e9+7;
    const ll INF=1e18+10;
    char a[N];
    int flag[10];
    int main()
    {
        int x;
        scanf("%s",a);
        x=strlen(a);
        if(x&1)
        {
            printf("-1
    ");
            return 0;
        }
        for(int i=0;i<x;i++)
        {
            if(a[i]=='U')
            flag[1]++;
            if(a[i]=='D')
            flag[2]++;
            if(a[i]=='L')
            flag[3]++;
            if(a[i]=='R')
            flag[4]++;
        }
        printf("%d
    ",(abs(flag[1]-flag[2])+abs(flag[3]-flag[4]))/2);
        return 0;
    }
    C. Memory and De-Evolution
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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.

    Examples
    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 ab, 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: 

    .

    题意:给你一个边长为x的等边三角形,可以改变一条边使其成为另一个三角形,求最少改变的次数,得到边长为y的等边三角形;

    思路:贪心,从y往上最大的改变;

    #include<bits/stdc++.h>
    using namespace std;
    #define ll long long
    #define pi (4*atan(1.0))
    const int N=1e5+10,M=4e6+10,inf=1e9+10,mod=1e9+7;
    const ll INF=1e18+10;
    int a[10];
    int main()
    {
        int x,y;
        scanf("%d%d",&x,&y);
        for(int i=1;i<=3;i++)a[i]=y;
        int ans=0;
        while(1)
        {
            if(a[1]==x&&a[2]==x&&a[3]==x)
            break;
            sort(a+1,a+4);
            a[1]=min(x,a[2]+a[3]-1);
            ans++;
        }
        cout<<ans<<endl;
        return 0;
    }
  • 相关阅读:
    选li第几天数据这种类型
    CSS背景 顶上 顶下之类详解
    php的数据循环 之li的3个类判断
    jq之ajax以及json数据传递
    css响应有media,js响应有这种
    表格高度超出部分滚动条的坑
    【转】IDEA快捷键功能说明及Eclipse对应操作
    【转】intellij idea使用指南—— 导入Eclipse的Web项目
    【转】 关于IDEA javax.servlet.http.HttpServletRequest; 不存在 解决方案
    struts2学习笔记(七)—— struts2的文件上传
  • 原文地址:https://www.cnblogs.com/jhz033/p/5864401.html
Copyright © 2011-2022 走看看