zoukankan      html  css  js  c++  java
  • CF #552 div3

    A - Restoring Three Numbers CodeForces - 1154A

    Polycarp has guessed three positive integers aa, bb and cc. He keeps these numbers in secret, but he writes down four numbers on a board in arbitrary order — their pairwise sums (three numbers) and sum of all three numbers (one number). So, there are four numbers on a board in random order: a+ba+b, a+ca+c, b+cb+c and a+b+ca+b+c.

    You have to guess three numbers aa, bb and cc using given numbers. Print three guessed integers in any order.

    Pay attention that some given numbers aa, bb and cc can be equal (it is also possible that a=b=ca=b=c).

    Input

    The only line of the input contains four positive integers x1,x2,x3,x4x1,x2,x3,x4 (2xi1092≤xi≤109) — numbers written on a board in random order. It is guaranteed that the answer exists for the given number x1,x2,x3,x4x1,x2,x3,x4.

    Output

    Print such positive integers aa, bb and cc that four numbers written on a board are values a+ba+b, a+ca+c, b+cb+c and a+b+ca+b+c written in some order. Print aa, bb and cc in any order. If there are several answers, you can print any. It is guaranteed that the answer exists.

    Examples

    Input
    3 6 5 4
    
    Output
    2 1 3
    
    Input
    40 40 40 60
    
    Output
    20 20 20
    
    Input
    201 101 101 200
    
    Output
    1 100 100
    题意:给你四个值,代表a+b,a+c,b+c,a+b+c的值,然后让你找到a,b,c的值。
    解法:排完序之后,a+b+c肯定是最大的,那么减去前三个值也就是得到a,b,c的值了。
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    using namespace std;
    typedef long long LL;
    const int maxn=10;
    int x[maxn];
    int a,b,c;
    int main()
    {
        for(int i=0;i<4;i++)
            scanf("%d",&x[i]);
        sort(x,x+4);
        a=x[3]-x[0];
        b=x[3]-x[1];
        c=x[3]-x[2];
        printf("%d %d %d
    ",a,b,c);
        return 0;
    }

    B - Make Them Equal CodeForces - 1154B 

    You are given a sequence a1,a2,,ana1,a2,…,an consisting of nn integers.

    You can choose any non-negative integer DD (i.e. D0D≥0), and for each aiai you can:

    • add DD (only once), i. e. perform ai:=ai+Dai:=ai+D, or
    • subtract DD (only once), i. e. perform ai:=aiDai:=ai−D, or
    • leave the value of aiai unchanged.

    It is possible that after an operation the value aiai becomes negative.

    Your goal is to choose such minimum non-negative integer DD and perform changes in such a way, that all aiai are equal (i.e. a1=a2==ana1=a2=⋯=an).

    Print the required DD or, if it is impossible to choose such value DD, print -1.

    For example, for array [2,8][2,8] the value D=3D=3 is minimum possible because you can obtain the array [5,5][5,5] if you will add DD to 22 and subtract DD from 88. And for array [1,4,7,7][1,4,7,7] the value D=3D=3 is also minimum possible. You can add it to 11 and subtract it from 77 and obtain the array [4,4,4,4][4,4,4,4].

    Input

    The first line of the input contains one integer nn (1n1001≤n≤100) — the number of elements in aa.

    The second line of the input contains nn integers a1,a2,,ana1,a2,…,an (1ai1001≤ai≤100) — the sequence aa.

    Output

    Print one integer — the minimum non-negative integer value DD such that if you add this value to some aiai, subtract this value from some aiai and leave some aiai without changes, all obtained values become equal.

    If it is impossible to choose such value DD, print -1.

    Examples

    Input
    6
    1 4 4 7 4 1
    
    Output
    3
    
    Input
    5
    2 2 5 2 5
    
    Output
    3
    
    Input
    4
    1 3 3 7
    
    Output
    -1
    
    Input
    2
    2 8
    
    Output
    3
    题意:给你n个数,找到一个数D,对于这n个数的每一个有三种操作,ai+D,ai-D,ai。问最后能不能使得这n个数相等,可以的D中取最小值。
    解法:数组排序去重之后,判断剩下的数不重复的值有几个,超过三个的话就肯定不存在这个D值了,如果是三个的话只有是等比数列的时候才可以;如果是两个的话,看这两个数相加起来是否为偶数,是偶数就能使得一个数+D,一个数-D
    得到两个相等的数值,奇数的话只能把一个数变为另一个数;如果全相等的话就输出0即可。
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    using namespace std;
    typedef long long LL;
    const int maxn=105;
    int n;
    int a[maxn];
    int main()
    {
        scanf("%d",&n);
        for(int i=0;i<n;i++)
            scanf("%d",&a[i]);
        sort(a,a+n);
        int len=unique(a,a+n)-a;
        if(len==1)
            printf("0
    ");
        else if(len==2)
        {
            if((a[1]-a[0])%2 == 0)
                printf("%d
    ",(a[1]-a[0])/2);
            else if((a[1]-a[0])%2 == 1)
                printf("%d
    ",a[1]-a[0]);
        }
        else if(len==3&&a[2]-a[1]==a[1]-a[0])
            printf("%d
    ",a[1]-a[0]);
        else
            printf("-1
    ");
        return 0;
    }

    C - Gourmet Cat CodeForces - 1154C

    Polycarp has a cat and his cat is a real gourmet! Dependent on a day of the week he eats certain type of food:

    • on Mondays, Thursdays and Sundays he eats fish food;
    • on Tuesdays and Saturdays he eats rabbit stew;
    • on other days of week he eats chicken stake.

    Polycarp plans to go on a trip and already packed his backpack. His backpack contains:

    • aa daily rations of fish food;
    • bb daily rations of rabbit stew;
    • cc daily rations of chicken stakes.

    Polycarp has to choose such day of the week to start his trip that his cat can eat without additional food purchases as long as possible. Print the maximum number of days the cat can eat in a trip without additional food purchases, if Polycarp chooses the day of the week to start his trip optimally.

    Input

    The first line of the input contains three positive integers aa, bb and cc (1a,b,c71081≤a,b,c≤7⋅108) — the number of daily rations of fish food, rabbit stew and chicken stakes in Polycarps backpack correspondingly.

    Output

    Print the maximum number of days the cat can eat in a trip without additional food purchases, if Polycarp chooses the day of the week to start his trip optimally.

    Examples

    Input
    2 1 1
    
    Output
    4
    
    Input
    3 2 2
    
    Output
    7
    
    Input
    1 100 1
    
    Output
    3
    
    Input
    30 20 10
    
    Output
    39

    思路:一星期a有3次,b有2次,c有2次,所以依次相除,在选取最小表示最多坚持完整的星期有几个,之后枚举判断一星期之内(不包含7)的最多坚持天数相加即可。

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    using namespace std;
    typedef long long LL;
    const int maxn=105;
    int a,b,c;
    int ans=0;
    int main()
    {
        scanf("%d %d %d",&a,&b,&c);
        while(1)
        {
            if(a<=3||b<=2||c<=2)
                break;
            a-=3;
            b-=2;
            c-=2;
            ans+=7;
        }
        int temp=0;
        for(int i=1;i<=7;i++)
        {
            int t=i;
            int maxx=0;
            int aa=a,bb=b,cc=c;
            while(true)
            {
                if(aa<0||bb<0||cc<0)
                {
                    temp=max(temp,maxx);
                    break;
                }
                if(t%7==1||t%7==4||t%7==0)
                    aa--;
                if(t%7==2||t%7==6)
                    bb--;
                if(t%7==3||t%7==5)
                    cc--;
                t++;
                maxx++;
            }
        }
        printf("%d
    ",ans+temp-1);
        return 0;
    }
  • 相关阅读:
    PS后期合成,你和大神的差距就这5步!
    欧几里得算法:从证明等式gcd(m, n) = gcd(n, m mod n)对每一对正整数m, n都成立说开去
    谜题:过桥问题
    《世界是数字的》读书笔记第一部分_硬件篇
    完全偶图K(3,3)与完全图K5是否存在平面表示
    Dijkstar算法的数学原理
    LeetCode 141. Linked List Cycle
    LeetCode 155. Min Stack
    LeetCode 160. Intersection of Two Linked Lists
    LeetCode 165.Compare Version Numbers
  • 原文地址:https://www.cnblogs.com/jkzr/p/10729830.html
Copyright © 2011-2022 走看看