Codeforces #Round 522(div 3)

A. Restoring Three Numbers

time limit per test 1 second
memory limit per test 256 megabytes
input standard input
output standard output

Polycarp has guessed three positive integers a, b and c. 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+b, a+c, b+c and a+b+c.

You have to guess three numbers a, b and c using given numbers. Print
three guessed integers in any order.

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

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

Output
Print such positive integers a, b and c that four numbers
written on a board are values a+b, a+c, b+c and a+b+c written in some
order. Print a, b and c 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

做题的时候没有注意到positive numbers,也就是说a b c均为正数,a+b+c必然>a+b/a+c/b+c,所以只需要用a+b+c依次减去另外两项的和,即可得到a b c。
但是要注意!不是max(x1,x2,x2,x4)以后直接用Max-x1/x2/x3就可以,后面需要排除Max再相减,所以用Max求最大值不如直接用sort排序或者选用set读入数据。

数据规模是 [ 2, 10^9 ] ,所以使用int类型就可以。(因为只会出现减法,所以运算过程中不会越界)

#include <iostream>
#include <algorithm>
#include <set>
using namespace std;

int main()
{
    set <int> myset;

    for(int i = 1; i <= 4; i++)
    {
        int x;
        cin >> x;
        myset.insert(x);
    }

    set <int> :: iterator it;
    it = myset.end();
    it--; //.end()指向最后一位元素的再下一位 所以需要--才是真正的最后一位元素
    int totalsum = *it;
    myset.erase(*it);


    for(it = myset.begin();it != myset.find(totalsum);++it)
    {
        cout << totalsum - *it << endl;
    }

    return 0;
}

B. Make Them Equal

time limit per test 2 seconds
memory limit per test 256 megabytes
input standard input
output standard output

You are given a sequence a1,a2,…,an consisting of n integers. You can choose any non-negative integer D (i.e. D≥0), and for each ai you can:


1 add D (only once), i. e. perform ai:=ai+D,
2 or subtract D (only once), i. e. perform ai:=ai−D,
3 or leave the value of ai unchanged.


It is possible that after an operation the value ai becomes negative.Your goal is to choose such minimum non-negative integer D and perform changes in such a way, that all ai are equal (i.e. a1=a2=⋯=an). Print the required D or, if it is impossible to choose such value D, print -1.

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

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

The second line of the input contains n integers a1,a2,…,an (1≤ai≤100) — the sequence a.

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

If it is impossible to choose such value D, 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

标为黄色的部分是当初做题是没有仔细读懂,题目最开始已经说了,对sequence中的每个element只能操作一次,这一次可以是加、减、不变,如果每个element操作依次过后,不存在这样一个一致的正整数(如果有多个选取最小的),那么就输出-1。

多对题目给的例子操作几次,要发现以下几个重点:

  • 重要的不是n(the number of elements),因为n个元素里可能会有所重复,重复的元素不需要考虑,所以重要的是去重后的元素个数pure_n
    由去重可以想到采用set读入数据,利用set的不重复特性
  • 得到Pure_n后,怎样才能做到all obtained values become equal呢?要结合题目的反复强调的only once,又因为可以操作的是一个特定的数字,给定的操作又只有stay unchanged, add, subtract三种,框定在了加减法。加减特定数字 → 想到等差数列。也就是说只有当去重后的sequence是Arithmetic Progression时,才能达到题目要求的条件

当我们发现核心是判断去重序列是否是等差数列之后就可以开始具体讨论了,要注意几种特殊情况:不妨按照去重后个数来分类,3+个(3+和3的情况是不一样的!!这里一开始错了没想明白啊 泪),3个,2个,1个。

  • pure_n == 3 判断两两相减是否相等,一旦不相等break,检查循环变量,如果遍历说明是A.P.,未能遍历输出-1
  • pure_n > 3 注意这里和pure_n == 3的情况是不一样的!不是判断了是arithmetic progression就可以,一旦pure_n > 3,不管你是不是等差数列都没可能找到ai了。(一定要清醒,不要觉得像是 3 3 3 3 3 虽然大于3但是可以呀?注意我们这里是已经去重后的pure_n,不存在这种情况!)
  • pure_n == 2 因为使用set盛放元素,所以不需要判断大小,尾-首得到差。但是要注意,此时Max-min是公差d不错,但并不是本题所求的最小ai。max-min得到的公差d是只限于max - d,或者min + d,二者取一只进行一次操作。而本题对两个元素可分别进行only once的一次操作,我们可以进行两次操作。换言之我们可以同时进行max - d/2, min + d/2两次操作,而这时d/2才是本体所求的minimum non-negative integer!发现这一点后,由于题目要求的是integer,所以下一步就是判断d%2。若d%2 == 0,输出d/2;反之输出d。
  • pure_n==1 注意此时无需操作所以ai = 0
#include <iostream>
#include <set>
using namespace std;
int a, b, difference, difference1;
bool check = 1;
int main()
{
    set <int> myset;
    int n;
    cin >> n;
    for(int i = 0; i < n;i++)
    {
        int x;
        cin >> x;
        myset.insert(x);
    }

    int pure_n = myset.size();

    set <int> :: iterator iter;

    iter = myset.begin();
    a = *iter;
    iter++;
    b = *iter;
    difference1 = b - a;
    if(pure_n == 3)
    {
        for(iter = myset.begin();iter != myset.end();iter++)
        {
            if(iter != myset.begin())
            {
                difference = *iter - a;

                if(difference != difference1)
                    {
                        check = 0;
                        break;
                    }

                a = *iter;
            }
        }
        if(check == 0)
            cout << -1 << endl;//第一次发现"-1"直接写-1也不会报错可以成功输出……
        else
            cout << difference <<endl;
    }
    else if(pure_n > 3)
    {
        cout << -1 <<endl;
    }
    else if(pure_n == 2)
    {
        iter = myset.begin();
        a = *iter;
        iter++;
        b = *iter;

        if((b - a) % 2 == 0)
            cout << (b - a) / 2 << endl;
        else
            cout << (b - a) << endl;
    }

    else
    {
        cout << 0 << endl;
    }
    return 0;
}

C. Gourmet Cat

time limit per test 1 second
memory limit per test 256 megabytes
input standard input
output standard output

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:

a daily rations of fish food;
b daily rations of rabbit stew;
c 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 a, b and c (1≤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

Note
In the first example(2 1 1 - 4) the best day for start of the trip is Sunday. In this case, during Sunday and Monday the cat will eat fish food, during Tuesday — rabbit stew and during Wednesday — chicken stake. So, after four days of the trip all food will be eaten.

In the second example(3 2 2 - 7) Polycarp can start his trip in any day of the week. In any case there are food supplies only for one week in Polycarps backpack.

In the third example(1 100 1 - 3) Polycarp can start his trip in any day, excluding Wednesday, Saturday and Sunday. In this case, the cat will eat three different dishes in three days. Nevertheless that after three days of a trip there will be 99 portions of rabbit stew in a backpack, the cat cannot eat anything in fourth day of a trip.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值