Codeforces Round #491 (Div. 2)题解

A. If at first you don’t succeed…
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Each student eagerly awaits the day he would pass the exams successfully. Thus, Vasya was ready to celebrate, but, alas, he didn’t pass it. However, many of Vasya’s fellow students from the same group were more successful and celebrated after the exam.

Some of them celebrated in the BugDonalds restaurant, some of them — in the BeaverKing restaurant, the most successful ones were fast enough to celebrate in both of restaurants. Students which didn’t pass the exam didn’t celebrate in any of those restaurants and elected to stay home to prepare for their reexamination. However, this quickly bored Vasya and he started checking celebration photos on the Kilogramm. He found out that, in total, BugDonalds was visited by A students, BeaverKing — by B students and C students visited both restaurants. Vasya also knows that there are N students in his group.

Based on this info, Vasya wants to determine either if his data contradicts itself or, if it doesn’t, how many students in his group didn’t pass the exam. Can you help him so he won’t waste his valuable preparation time?

Input
The first line contains four integers — A, B, C and N (0≤A,B,C,N≤100).

Output
If a distribution of N students exists in which A students visited BugDonalds, B — BeaverKing, C — both of the restaurants and at least one student is left home (it is known that Vasya didn’t pass the exam and stayed at home), output one integer — amount of students (including Vasya) who did not pass the exam.

If such a distribution does not exist and Vasya made a mistake while determining the numbers A, B, C or N (as in samples 2 and 3), output −1.

Examples
inputCopy
10 10 5 20
outputCopy
5
inputCopy
2 2 0 4
outputCopy
-1
inputCopy
2 2 2 1
outputCopy
-1
Note
The first sample describes following situation: 5 only visited BugDonalds, 5 students only visited BeaverKing, 5 visited both of them and 5 students (including Vasya) didn’t pass the exam.

In the second sample 2 students only visited BugDonalds and 2 only visited BeaverKing, but that means all 4 students in group passed the exam which contradicts the fact that Vasya didn’t pass meaning that this situation is impossible.

The third sample describes a situation where 2 students visited BugDonalds but the group has only 1 which makes it clearly impossible.
思路: 加减判断就行

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
 
using namespace std;
 
#define endl '\n'
 
typedef long long ll;
 
 
 
int main()
{
    int a, b, c, n;
 
    cin >> a >> b >> c >> n;
 
    if (a + b - c >= n || c > a || c > b)
        cout << "-1" << endl;
    else
        cout << (n - (a + b - c)) << endl;
 
    // getchar(); getchar();
 
    return 0;
}

B. Getting an A
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Translator’s note: in Russia’s most widespread grading system, there are four grades: 5, 4, 3, 2, the higher the better, roughly corresponding to A, B, C and F respectively in American grading system.

The term is coming to an end and students start thinking about their grades. Today, a professor told his students that the grades for his course would be given out automatically — he would calculate the simple average (arithmetic mean) of all grades given out for lab works this term and round to the nearest integer. The rounding would be done in favour of the student — 4.5 would be rounded up to 5 (as in example 3), but 4.4 would be rounded down to 4.

This does not bode well for Vasya who didn’t think those lab works would influence anything, so he may receive a grade worse than 5 (maybe even the dreaded 2). However, the professor allowed him to redo some of his works of Vasya’s choosing to increase his average grade. Vasya wants to redo as as few lab works as possible in order to get 5 for the course. Of course, Vasya will get 5 for the lab works he chooses to redo.

Help Vasya — calculate the minimum amount of lab works Vasya has to redo.

Input
The first line contains a single integer n — the number of Vasya’s grades (1≤n≤100).

The second line contains n integers from 2 to 5 — Vasya’s grades for his lab works.

Output
Output a single integer — the minimum amount of lab works that Vasya has to redo. It can be shown that Vasya can always redo enough lab works to get a 5.

Examples
inputCopy
3
4 4 4
outputCopy
2
inputCopy
4
5 4 5 5
outputCopy
0
inputCopy
4
5 3 3 5
outputCopy
1
Note
In the first sample, it is enough to redo two lab works to make two 4s into 5s.

In the second sample, Vasya’s average is already 4.75 so he doesn’t have to redo anything to get a 5.

In the second sample Vasya has to redo one lab work to get rid of one of the 3s, that will make the average exactly 4.5 so the final grade would be 5.

思路: 排序后从小到大逐个用5替代,好像卡了精度。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
 
using namespace std;
 
#define endl '\n'
 
typedef long long ll;
 
#define eps 1e-8
 
ll a[101];
 
int main()
{
    int n;
    cin >> n;
    double sum = 0;
    for (int i = 0; i < n; ++i)
    {
        cin >> a[i];
        sum += a[i] *1.0;
    }
    sort(a, a + n);
    double average = sum * 1.0 / n;
 
    if (average - 4.5 > eps || fabs(average - 4.5) < eps)
    { 
        cout << "0" << endl;
    	return 0; 
	} 
	ll ans = 0;
    for (int i = 0; i < n; ++i)
    {
            sum -= a[i];
            sum += 5;
            average = sum * 1.0 / n;
            ans++;
        
            if (average - 4.5 > eps || fabs(average - 4.5) < eps)
        {
            cout << ans << endl;
            return 0;
        }
    }
 
 
    // getchar(); getchar();
 
    return 0;
}

C. Candies
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
After passing a test, Vasya got himself a box of n candies. He decided to eat an equal amount of candies each morning until there are no more candies. However, Petya also noticed the box and decided to get some candies for himself.

This means the process of eating candies is the following: in the beginning Vasya chooses a single integer k, same for all days. After that, in the morning he eats k candies from the box (if there are less than k candies in the box, he eats them all), then in the evening Petya eats 10% of the candies remaining in the box. If there are still candies left in the box, the process repeats — next day Vasya eats k candies again, and Petya — 10% of the candies left in a box, and so on.

If the amount of candies in the box is not divisible by 10, Petya rounds the amount he takes from the box down. For example, if there were 97 candies in the box, Petya would eat only 9 of them. In particular, if there are less than 10 candies in a box, Petya won’t eat any at all.

Your task is to find out the minimal amount of k that can be chosen by Vasya so that he would eat at least half of the n candies he initially got. Note that the number k must be integer.

Input
The first line contains a single integer n (1≤n≤1018) — the initial amount of candies in the box.

Output
Output a single integer — the minimal amount of k that would allow Vasya to eat at least half of candies he got.

Example
inputCopy
68
outputCopy
3
Note
In the sample, the amount of candies, with k=3, would change in the following way (Vasya eats first):

68→65→59→56→51→48→44→41→37→34→31→28→26→23→21→18→17→14→13→10→9→6→6→3→3→0.

In total, Vasya would eat 39 candies, while Petya — 29.
思路: 二分

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 105;
typedef long long ll;
ll n, ans;
bool check(ll k)
{
    ll x = n, cnt = 0;
    while (x > 0)
    {
        cnt += min(x, k);
        x -= k;
        x -= x/10;
    }
    return cnt >= (n+1)/2;
}
int main()
{
    scanf("%lld", &n);
    ll l = 1, r = (n+1)/2, m = (l+r)/2;
    while (l < r)
    {
        if (check(m)) ans = r = m;
        else l = m+1;
        m = (l+r)/2;
    }
    if (ans == 0) ans = 1;
    printf("%lld\n", ans);
    return 0;
}
发布了161 篇原创文章 · 获赞 7 · 访问量 7095

猜你喜欢

转载自blog.csdn.net/weixin_43778744/article/details/104059880