• Tinkoff Challenge


    A. Oleg and shares
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Oleg the bank client checks share prices every day. There are n share prices he is interested in. Today he observed that each second exactly one of these prices decreases by k rubles (note that each second exactly one price changes, but at different seconds different prices can change). Prices can become negative. Oleg found this process interesting, and he asked Igor the financial analyst, what is the minimum time needed for all n prices to become equal, or it is impossible at all? Igor is busy right now, so he asked you to help Oleg. Can you answer this question?

    Input

    The first line contains two integers n and k (1 ≤ n ≤ 105, 1 ≤ k ≤ 109) — the number of share prices, and the amount of rubles some price decreases each second.

    The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the initial prices.

    Output

    Print the only line containing the minimum number of seconds needed for prices to become equal, of «-1» if it is impossible.

    Examples
    input
    3 3
    12 9 15
    output
    3
    input
    2 2
    10 9
    output
    -1
    input
    4 1
    1 1000000000 1000000000 1000000000
    output
    2999999997
    Note

    Consider the first example.

    Suppose the third price decreases in the first second and become equal 12 rubles, then the first price decreases and becomes equal 9rubles, and in the third second the third price decreases again and becomes equal 9 rubles. In this case all prices become equal 9 rubles in 3 seconds.

    There could be other possibilities, but this minimizes the time needed for all prices to become equal. Thus the answer is 3.

    In the second example we can notice that parity of first and second price is different and never changes within described process. Thus prices never can become equal.

    In the third example following scenario can take place: firstly, the second price drops, then the third price, and then fourth price. It happens 999999999 times, and, since in one second only one price can drop, the whole process takes 999999999 * 3 = 2999999997 seconds. We can note that this is the minimum possible time.

    你有n支股票,分别有不同的价格,每次会下降k,问下降到最小值所需最小时间。注意要用long long,否则溢出,样例给你还是很良心的

    #include<bits/stdc++.h>
    using namespace std;
    const int N=1e5+10;
    int a[N];
    typedef long long LL;
    int main()
    {
       int n,k;
       cin>>n>>k;
       int mi=1<<30;
       for(int i=1;i<=n;i++){
        cin>>a[i];
        mi=min(mi,a[i]);}
        int f=1;
        long long s=0;
        for(int i=1;i<=n;i++){
            int w=a[i]-mi;
            if(w%k){
            f=0;break;}
            s+=w/k;
        }
        if(f)
            printf("%lld
    ",s);
        else
            puts("-1");
    
       return 0;
    }
    B. Igor and his way to work
    time limit per test
    3 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Woken up by the alarm clock Igor the financial analyst hurried up to the work. He ate his breakfast and sat in his car. Sadly, when he opened his GPS navigator, he found that some of the roads in Bankopolis, the city where he lives, are closed due to road works. Moreover, Igor has some problems with the steering wheel, so he can make no more than two turns on his way to his office in bank.

    Bankopolis looks like a grid of n rows and m columns. Igor should find a way from his home to the bank that has no more than two turns and doesn't contain cells with road works, or determine that it is impossible and he should work from home. A turn is a change in movement direction. Igor's car can only move to the left, to the right, upwards and downwards. Initially Igor can choose any direction. Igor is still sleepy, so you should help him.

    Input

    The first line contains two integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in the grid.

    Each of the next n lines contains m characters denoting the corresponding row of the grid. The following characters can occur:

    • "." — an empty cell;
    • "*" — a cell with road works;
    • "S" — the cell where Igor's home is located;
    • "T" — the cell where Igor's office is located.

    It is guaranteed that "S" and "T" appear exactly once each.

    Output

    In the only line print "YES" if there is a path between Igor's home and Igor's office with no more than two turns, and "NO" otherwise.

    Examples
    input
    5 5
    ..S..
    ****.
    T....
    ****.
    .....
    output
    YES
    input
    5 5
    S....
    ****.
    .....
    .****
    ..T..
    output
    NO
    Note

    The first sample is shown on the following picture:

    In the second sample it is impossible to reach Igor's office using less that 4 turns, thus there exists no path using no more than 2 turns. The path using exactly 4 turns is shown on this picture:

    暴力搜索,从S到T拐两个弯内就输出YES,否则NO,所以需要记忆下现在拐的弯

    #include <bits/stdc++.h>
    using namespace std;
    char str[1005][1005];
    int vis[1005][5005];
    int n, m;
    int dx[] = {1,-1,0,0};
    int dy[] = {0,0,1,-1};
    
    void dfs(int x, int y, int d, int pre)
    {
        if(x<0||x>=n||y<0||y>=m) return ;
        if(str[x][y] == '*') return ;
        vis[x][y] = 1;
        if(d==1) {
            for(int i = 0; i < 4; i++) dfs(x+dx[i],y+dy[i],0,i);
        }
        dfs(x+dx[pre],y+dy[pre],d,pre);
    }
    bool ans_dfs(int x,int y,int pre)
    {
        if(x<0||x>=n||y<0||y>=m) return false;
        if(str[x][y]=='*') return false;
        if(vis[x][y]==1) return true;
        return ans_dfs(x+dx[pre],y+dy[pre],pre);
    }
    int main()
    {
        memset(vis,0,sizeof(vis));
        int x1,x2,y1,y2;
        scanf("%d%d",&n,&m);
        for(int i = 0;i < n; i++)
            scanf("%s",str[i]);
        for(int i = 0;i < n; i++) {
            for(int j = 0;j < m; j++) {
                if(str[i][j]=='S') {
                    x1 = i;
                    y1 = j;
                }
                if(str[i][j]=='T') {
                    x2 = i;
                    y2 = j;
                }
            }
        }
        for(int i = 0;i < 4; i++) {
            dfs(x1,y1,1,i);
        }
        bool f = false;
        for(int i = 0;i < 4; i++) {
            if(ans_dfs(x2,y2,i)) {
                f = true;
                break;
            }
        }
        if(f) puts("YES");
        else puts("NO");
    return 0;
    }
  • 相关阅读:
    centos7搭建nexus
    centos7搭建ftp
    tomcat笔记
    mysql跨服务器触发器
    IntelliJ IDEA笔记
    node.js笔记
    myeclipse笔记
    术语
    centos7安装mysql
    js跨域访问资源
  • 原文地址:https://www.cnblogs.com/BobHuang/p/6945718.html
Copyright © 2020-2023  润新知