欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Educational Codeforces Round 85 (Rated for Div. 2)题解

程序员文章站 2022-06-04 08:09:42
...

Educational Codeforces Round 85 (Rated for Div. 2)题解
写在前面: 没想到思路全对的情况下居然因为一个初始化错误卡了一个多小时没出来。。。

A. Level Statistics

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Polycarp has recently created a new level in this cool new game Berlio Maker 85 and uploaded it online. Now players from all over the world can try his level.

All levels in this game have two stats to them: the number of plays and the number of clears. So when a player attempts the level, the number of plays increases by 1. If he manages to finish the level successfully then the number of clears increases by 1 as well. Note that both of the statistics update at the same time (so if the player finishes the level successfully then the number of plays will increase at the same time as the number of clears).

Polycarp is very excited about his level, so he keeps peeking at the stats to know how hard his level turns out to be.

So he peeked at the stats n times and wrote down n pairs of integers — (p1,c1),(p2,c2),…,(pn,cn), where pi is the number of plays at the i-th moment of time and ci is the number of clears at the same moment of time. The stats are given in chronological order (i.e. the order of given pairs is exactly the same as Polycarp has written down).

Between two consecutive moments of time Polycarp peeked at the stats many players (but possibly zero) could attempt the level.

Finally, Polycarp wonders if he hasn’t messed up any records and all the pairs are correct. If there could exist such a sequence of plays (and clears, respectively) that the stats were exactly as Polycarp has written down, then he considers his records correct.

Help him to check the correctness of his records.

For your convenience you have to answer multiple independent test cases.

Input
The first line contains a single integer T (1≤T≤500) — the number of test cases.

The first line of each test case contains a single integer n (1≤n≤100) — the number of moments of time Polycarp peeked at the stats.

Each of the next n lines contains two integers pi and ci (0≤pi,ci≤1000) — the number of plays and the number of clears of the level at the i-th moment of time.

Note that the stats are given in chronological order.

Output
For each test case print a single line.

If there could exist such a sequence of plays (and clears, respectively) that the stats were exactly as Polycarp has written down, then print “YES”.

Otherwise, print “NO”.

You can print each letter in any case (upper or lower).

Example
inputCopy
6
3
0 0
1 1
1 2
2
1 0
1000 3
4
10 1
15 2
10 2
15 2
1
765 432
2
4 4
4 3
5
0 0
1 0
1 0
1 0
1 0
outputCopy
NO
YES
NO
YES
NO
YES
Note
In the first test case at the third moment of time the number of clears increased but the number of plays did not, that couldn’t have happened.

The second test case is a nice example of a Super Expert level.

In the third test case the number of plays decreased, which is impossible.

The fourth test case is probably an auto level with a single jump over the spike.

In the fifth test case the number of clears decreased, which is also impossible.

Nobody wanted to play the sixth test case; Polycarp’s mom attempted it to make him feel better, however, she couldn’t clear it.
思路:
简单判断,三个条件。

#include <bits/stdc++.h>
 
using namespace std;
 
#define endl '\n'
 
typedef long long ll;
 
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
 
        int f = 0;
 
        int p[1005] = {0}, c[1005] = {0};
 
        for (int i = 1; i <= n; ++i)
        {
            int x, y;
            cin >> p[i] >> c[i];
            if (p[i] < p[i - 1] || c[i] < c[i - 1] || (c[i] - c[i - 1] > p[i] - p[i - 1]))
                f = 1;
        }
        if (f)
            cout << "NO" << endl;
        else
            cout << "YES" << endl;
    }
 
    return 0;
}

B. Middle Class

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Many years ago Berland was a small country where only n people lived. Each person had some savings: the i-th one had ai burles.

The government considered a person as wealthy if he had at least x burles. To increase the number of wealthy people Berland decided to carry out several reforms. Each reform looked like that:

the government chooses some subset of people (maybe all of them);
the government takes all savings from the chosen people and redistributes the savings among the chosen people equally.
For example, consider the savings as list [5,1,2,1]: if the government chose the 1-st and the 3-rd persons then it, at first, will take all 5+2=7 burles and after that will return 3.5 burles to the chosen people. As a result, the savings will become [3.5,1,3.5,1].

A lot of data was lost from that time, so we don’t know how many reforms were implemented and to whom. All we can do is ask you to calculate the maximum possible number of wealthy people after several (maybe zero) reforms.

Input
The first line contains single integer T (1≤T≤1000) — the number of test cases.

Next 2T lines contain the test cases — two lines per test case. The first line contains two integers n and x (1≤n≤105, 1≤x≤109) — the number of people and the minimum amount of money to be considered as wealthy.

The second line contains n integers a1,a2,…,an (1≤ai≤109) — the initial savings of each person.

It’s guaranteed that the total sum of n doesn’t exceed 105.

Output
Print T integers — one per test case. For each test case print the maximum possible number of wealthy people after several (maybe zero) reforms.

Example
inputCopy
4
4 3
5 1 2 1
4 10
11 9 11 9
2 5
4 3
3 7
9 4 9
outputCopy
2
4
0
3
Note
The first test case is described in the statement.

In the second test case, the government, for example, could carry out two reforms: [11–––,9–,11,9]→[10,10,11–––,9–]→[10,10,10,10].

In the third test case, the government couldn’t make even one person wealthy.

In the fourth test case, the government could choose all people to carry out a reform: [9–,4–,9–]→[713,713,713].
思路:
排序取和求平均,大于等于要求即可。

#include <bits/stdc++.h>
 
using namespace std;
 
#define endl '\n'
 
typedef long long ll;
 
ll a[100004];
 
bool cmp(ll x, ll y)
{
    return x > y;
}
 
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        ll n, m;
 
        cin >> n >> m;
 
        for (int i = 0; i < n; ++i)
            cin >> a[i];
 
        sort(a, a + n, cmp);
 
        double sum = 0;
        ll cnt = 0;
 
        for (int i = 0; i < n; ++i)
        {
            sum += a[i];
            double avl = sum * 1.0 / (cnt + 1);
            if (avl >= m)
                cnt++;
            else
                break;
        }
        cout << cnt << endl;
    }
 
    return 0;
}

C. Circle of Monsters

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are playing another computer game, and now you have to slay n monsters. These monsters are standing in a circle, numbered clockwise from 1 to n. Initially, the i-th monster has ai health.

You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by 1 (deals 1 damage to it). Furthermore, when the health of some monster i becomes 0 or less than 0, it dies and explodes, dealing bi damage to the next monster (monster i+1, if i<n, or monster 1, if i=n). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on.

You have to calculate the minimum number of bullets you have to fire to kill all n monsters in the circle.

Input
The first line contains one integer T (1≤T≤150000) — the number of test cases.

Then the test cases follow, each test case begins with a line containing one integer n (2≤n≤300000) — the number of monsters. Then n lines follow, each containing two integers ai and bi (1≤ai,bi≤1012) — the parameters of the i-th monster in the circle.

It is guaranteed that the total number of monsters in all test cases does not exceed 300000.

Output
For each test case, print one integer — the minimum number of bullets you have to fire to kill all of the monsters.

Example
inputCopy
1
3
7 15
2 14
5 3
outputCopy
6
思路:
我把一个最主要的初始化位置搞错了。。。而且这题容易TLE,关了输入输出流和endl就过了。
每一个和上一位做差就是必须要的子弹数目,然后找最小的一位为起点。

#include <iostream>
 
using namespace std;
 
typedef long long ll;
#define endl '\n'
ll a[300004];
ll b[300004];
ll c[300004];
 
ll maxp = 999999999999;
 
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int t;
    cin >> t;
    while (t--)
    {
        ll n;
        cin >> n;
 
        int pos = 0;
 
        ll xs = 0;
 
        for (int i = 0; i < n; ++i)
        {
            cin >> a[i] >> b[i];
            if (i > 0 && b[i - 1] > a[i])
                b[i - 1] = a[i];
            if (i > 0)
            {
                c[i] = a[i] - b[i - 1];
            }
        }
 
        if (a[0] < b[n - 1]) b[n - 1] = a[0];
        c[0] = a[0] - b[n - 1];
        
         maxp = a[0] - c[0];
 
        for (int i = 0; i < n; ++i)
        {
            xs += c[i];
            if (maxp > a[i] - c[i])
            {
                maxp = a[i] - c[i];
                pos = i;
            }
        }
 
        cout << xs + a[pos] - c[pos] << endl;
    }
    return 0;
}
相关标签: Codeforces