2020 年 “聯想杯”全國高校程序設計在線邀請賽暨第三屆上海理工大學程序設計競賽

菜雞如我 ABCDHL

A. Archmage

單點時限: 2.0 sec

內存限制: 256 MB

Archmage (AM for short) is a hero unit in ⅢWatercraft Ⅲ.

He has n mana at most and two powerful skills which enable him to show great strength in battle:

  1. Use x mana to summon a Water Element.
  2. Brilliance Aura restores y mana for AM in a second, and it is the only way to restore mana. Since his mana cannot exceed the upper limit, the superfluous mana will dissipate. i.e., suppose you have p mana before the restoration, your mana will become min(p+y,n).

Since the power of an Archmage is tremendous, the upper limit of mana n is always greater than or equal to x+y.

Every second, Archmage will summon exactly one Water Element (if the mana is enough, i.e., his mana won’t be less than 0 after that) or do nothing. Then no matter what he did before, he will restore y mana.

Archmage has n mana at the end of the second 0, and the game starts at the beginning of the second 1.

He wants to know how many Water Elements he can summon at most before the end of the second m.

輸入格式

The input consists of multiple test cases.

The first line contains a single integer t(1≤t≤105), indicating the number of test cases.

Each of the next t lines contains 4 integers n,m,x,y(1≤m,x,y≤109,x+y≤n≤2×109).

輸出格式

For each test case, output the answer in a line.

樣例

input

3
2 2 1 1
4 4 2 1
6 10 4 2

output

2
3
6

提示

In test case 1, Archmage can cast spells every second, so the answer is 2.

In test case 2, here’s a way for Archmage to cast spells 3 times.

Second 1: Archmage cast spells, and there is 4−x+y=3 mana left.

Second 2: Archmage cast spells, and there is 3−x+y=2 mana left.

Second 3: Archmage cast spells, and there is 2−x+y=1 mana left.

Second 4: Archmage doesn’t have enough mana so he can do nothing, and there is 1+y=2 mana left.

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> p;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;

template<typename T = int>
inline const T read()
{
    T x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = (x << 3) + (x << 1) + ch - '0'; ch = getchar(); }
    return x * f;
}

template<typename T>
inline void write(T x)
{
    if (x < 0) { putchar('-'); x = -x; }
    if (x > 9) write(x / 10);
    putchar(x % 10 + '0');
}

int main()
{
    ll n, m, x, y, t;
    cin >> t;
    while (t--)
    {
        cin >> n >> m >> x >> y;
        if (y >= x) cout << m << endl;
        else
        {
            ll p = n + y * (m - 1);
            cout << min(m, p / x) << endl;
        }
    }
    return 0;
}

B. Bamboo Leaf Rhapsody

單點時限: 1.0 sec

內存限制: 256 MB

Do you know who will fulfill people’s wishes on Tanabata?

It’s α Lyra and α Aquila.

By the way, Earth is 25 light-years away from α Lyra and 16 light-years away from α Aquila. That is to say, it will take 25 or 16 years for the messages sent from Earth to reach these two constellations. That’s for granted. Do you understand?

——The Boredom of Haruhi Suzumiya, 2004


This is the 16th year since the publication of the novel. Apart from the short stories, the author has not updated the series for nine years.

Setsuna is a big fan of the novel. She makes a wish that the author would update as soon as possible, and she will send this message to the stars.

The universe can be seen as a three-dimensional rectangular coordinate system in which the earth is the origin(i.e., the coordinate of the earth is (0,0,0)).

Suppose there are n stars, and the coordinate of the i-th one is (xi,yi,zi). Any one of them can receive her message.

The speed of the message is exactly 1 unit per year in the universe, and the direction can be arbitrary.

Setsuna wants to know the shortest time for the message to reach at least one of the stars after she sends it.

You can assume that Einstein’s theory of relativity doesn’t work in Setsuna’s world.

輸入格式

The first line contains one integer n(1≤n≤1000), indicating the number of the stars.

The i-th of the next n lines contains three integers xi,yi,zi(−1000≤xi,yi,zi≤1000), indicating the coordinate of the i-th star.

輸出格式

Output the shortest time in years, the value should be rounded to three decimal places.

樣例

input

3
0 1 1
2 0 0
0 -2 0

output

1.414

提示

The first star can receive the message in 2 years.

The second and the third star can receive the message in 2 years.

So the answer is 2 years, and it should be rounded to 1.414 .

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> p;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;

template<typename T = int>
inline const T read()
{
    T x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = (x << 3) + (x << 1) + ch - '0'; ch = getchar(); }
    return x * f;
}

template<typename T>
inline void write(T x)
{
    if (x < 0) { putchar('-'); x = -x; }
    if (x > 9) write(x / 10);
    putchar(x % 10 + '0');
}

int main()
{
    int n = read();
    double mn = 1e9;
    while (n--)
    {
        int x = read(), y = read(), z = read();
        double dis = sqrt(x * x + y * y + z * z);
        mn = min(mn, dis);
    }
    printf("%.3f\n", mn);
    return 0;
}

C. Cheat Sheet

單點時限: 1.0 sec

內存限制: 256 MB

University of Shanghai for Science and Technology starts a course called Film Appreciation of Black Album recently. To be the best “Blackologist” in the university, Setsuna is actively preparing for the exam.

The examination of the course is open book; that is to say, you can only take one single-sided cheat sheet to the exam. The cheat sheet can write n characters at most.

Setsuna has m keywords that she wants to write on the cheat sheet. Her memory is not very good, so there may be some duplicate keywords. Each keyword consists of several visible characters(visible characters refer to characters with ASCII code between 33 and 126 inclusive).

For both readability and neatness, keywords written on the cheat sheet should be separated by at least one space and must be different from each other.

Setsuna wants to know how many distinct keywords she can write down on the cheat sheet at most.

Uppercase and lowercase letters are considered different characters.

輸入格式

The first line contains two integers n,m(1≤n,m≤1000).

The second line contains m keywords separated by exactly one space. The length of each keyword is no more than 100. It is guaranteed that keyword only consists of visible characters.

輸出格式

Output one integer indicating the answer.

樣例

input

40 5
myworld lusto KR12138 oneman233 SetsunaQAQ

output

4

input

7 2
^_^ ^_^

output

1

提示

In sample 1, it takes 42 characters to write all the words down. So Setsuna can write down at most four.

In sample 2, there is only one keyword.

老 🎒 客了,其實沒必要因爲 W === 1。

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> p;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
unordered_map<string, bool> mp;
const int maxn = 1e3 + 10;
int c[maxn], dp[maxn];

template<typename T = int>
inline const T read()
{
    T x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = (x << 3) + (x << 1) + ch - '0'; ch = getchar(); }
    return x * f;
}

template<typename T>
inline void write(T x)
{
    if (x < 0) { putchar('-'); x = -x; }
    if (x > 9) write(x / 10);
    putchar(x % 10 + '0');
}

int main()
{
    int n = read(), m = read();
    int cnt = 0;
    while (m--)
    {
        string str;
        cin >> str;
        if (mp[str]) continue;
        c[++cnt] = (int)str.length() + 1;
        mp[str] = true;
    }
    for (int i = 1; i <= cnt; i++)
        for (int j = n + 1; j >= c[i]; j--)
            dp[j] = max(dp[j], dp[j - c[i]] + 1);
    write(dp[n + 1]);
    return 0;
}

D. Disaster Recovery

單點時限: 1.0 sec

內存限制: 256 MB

On the magical land Byteland, there is an ancient kingdom called Fibonacci Kingdom.

There are n cities in the kingdom(numbered from 1 to n), and Fibi residents in the i-th city.

Fibi denotes the i-th number of Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 1 and 1. That is, Fib0=1,Fib1=1 and Fibn=Fibn−1+Fibn−2 for n>1. The beginning of the sequence is thus: 1,1,2,3,5,8,13,21,34,55,89,144,⋯.

Unfortunately, one day, an earthquake occurred.

The earthquake destroyed the whole kingdom’s transportation system.

To deliver disaster relief supplies, the king decided to reconstruct the road.

m plans have been proposed, and the i-th plan is to reconstruct the bidirectional road linking xi and yi, denoted as (xi,yi).

The magic is that it takes exactly Fibxi+Fibyi bitcoins to reconstruct the road (xi,yi).

The king wants to choose some plans to implement, but the rules below must be followed:

  1. After the reconstruction, the cities are mutually reachable (i.e., there is at least one path connecting any two cities).
  2. Minimize the bitcoin consumption.
  3. In the premise of the least bitcoin consumption, the largest degree D among all the cities should be minimized(suppose di is the degree of i-th city, D=max1≤i≤ndi), because the residents of the kingdom are not good at looking for ways. The definition of the degree of a city is the number of roads directly connected to the city.

Setsuna, the savior of the kingdom, has easily calculated the minimum consumption of bitcoin, but she desperately wants to know the minimum D she can get after reconstruction.

It’s guaranteed that the answer always exists.

輸入格式

The first line contains two integers n,m(2≤n≤105,n−1≤m≤min(n×(n−1)2,2×105)).

The i-th of the next m lines contains two integers xi,yi(1≤xi,yi≤n,xi≠yi).

輸出格式

Output one integer which indicates the minimum D Setsuna can get.

樣例

input

5 6
2 1
2 4
1 3
3 4
4 5
1 5

output

3

提示

The graph of the sample is as follows.

T9_p1

In this situation, bitcoin consumption is 3+4+7+9=23. The degree of city 1 is 3; the degree of city 2 is 2; the degree of city 3,4,5 is 1.

It can be proved that such solution is the best, so the answer is 3.

因爲斐波那契數列任意一對數和不相同,所以這裏的 MST 邊權和唯一。

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> p;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const int maxn = 1e5 + 10;
int deg[maxn], pre[maxn];
vector<p> edge;

template<typename T = int>
inline const T read()
{
    T x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = (x << 3) + (x << 1) + ch - '0'; ch = getchar(); }
    return x * f;
}

template<typename T>
inline void write(T x)
{
    if (x < 0) { putchar('-'); x = -x; }
    if (x > 9) write(x / 10);
    putchar(x % 10 + '0');
}

bool cmp(const p& a, const p& b)
{
    return a.second == b.second ? a.first < b.first : a.second < b.second;
}

int Find(int id) { return pre[id] == id ? id : pre[id] = Find(pre[id]); }

void Union(int a, int b) { pre[Find(b)] = Find(a); }

int main()
{
    int n = read(), m = read();
    for (int i = 1; i <= n; i++) pre[i] = i;
    for (int i = 1; i <= m; i++)
    {
        int u = read(), v = read();
        if (u > v) swap(u, v);
        edge.push_back(p(u, v));
    }
    sort(edge.begin(), edge.end(), cmp);
    for (p e : edge)
    {
        int u = e.first, v = e.second;
        if (Find(u) != Find(v))
        {
            Union(u, v);
            deg[u]++;
            deg[v]++;
        }
    }
    int res = 0;
    for (int i = 1; i <= n; i++) res = max(res, deg[i]);
    write(res);
    return 0;
}

H. Hay Mower

單點時限: 2.0 sec

內存限制: 256 MB

Are you tired of city life? Have you ever had illusions of pastoral peace? The clean atmosphere, the closeness to nature and the gentle pace of living, all made Setsuna yearn for the pastoral life more.

In order to experience the simple pastoral life, Setsuna has moved to Star Valley and started her farming journey.

Soon, she discovers the problem: overgrown weeds are harming her farm. In Chinese, we call it “Sheng Cao”. She realized that weeding should be put at the top priority.

T12_p1

The farm can be described as an n×m matrix. The growth rate of weed in row i and column j is denoted as ai,j, indicating that the weed will grow ai,j units every beginning of the moment. At the end of moment 0, there is no weed on the farm.

Setsuna will use mower k times, where the i-th use occurs at the end of moment ti. Each use of the mower completely removes weeds in a row or column.

Setsuna wonders how many units of weed she will remove.

The answer might be very large, so please output the desired answer modulo 998244353.

輸入格式

The first line contains three integers n,m,k(1≤n,m≤500,1≤k≤3×105).

The next n lines contains m integers each, where the j-th integer of the i-th line is ai,j(0≤ai,j≤1018).

The i-th of the next k lines contains one character and two integers.

  • r xi ti - Setsuna clears the weeds in row xi at the end of moment ti.

  • c yi ti - Setsuna clears the weeds in column yi at the end of moment ti.

It is guaranteed that 1≤xi≤n,1≤yi≤m,1≤ti≤1018 hold for 1≤i≤k and ti is strictly increasing.

輸出格式

Output one integer indicating the answer modulo 998244353.

樣例

input

2 2 3
1 2
3 4
r 1 5
c 2 6
r 1 7

output

45

input

3 4 1
1 2 3 4
5 6 7 8
9 10 11 12
r 1 1000000000000000000

output

172998509

提示

Sample 1:

At the end of moment 0, the farm looks like

[0000]

At the end of moment 5, Setsuna has cleared row 1 and 15 units of weed has been cut, so the farm looks like

[001520]

At the end of moment 6, Setsuna has cleared column 2 and 26 units of weed has been cut, so the farm looks like

[10180]

At the end of moment 7, Setsuna has cleared row 1 and 4 units of weed has been cut, so the farm looks like

[00214]

So the answer is 15+26+4=45 units.

水題,需要注意數據範圍。

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef __int128 lll;
typedef pair<int, int> p;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const int maxn = 5e2 + 10;
const int maxm = 5e2 + 10;
const int mod = 998244353;
ll val[maxn][maxm];
ll rlast[maxn], clast[maxm];

template<typename T = ll>
inline const T read()
{
    T x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = (x << 3) + (x << 1) + ch - '0'; ch = getchar(); }
    return x * f;
}

template<typename T>
inline void write(T x)
{
    if (x < 0) { putchar('-'); x = -x; }
    if (x > 9) write(x / 10);
    putchar(x % 10 + '0');
}

int main()
{
    ll n = read(), m = read(), k = read(), res = 0;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            val[i][j] = read() % mod;
    while (k--)
    {
        string op;
        cin >> op;
        ll p = read(), t = read();
        if (op == "r")
        {
            for (int i = 1; i <= m; i++)
            {
                ll dt = min(t - rlast[p], t - clast[i]) % mod;
                res = (res + val[p][i] * dt) % mod;
            }
            rlast[p] = t;
        }
        else
        {
            for (int i = 1; i <= n; i++)
            {
                ll dt = min(t - rlast[i], t - clast[p]) % mod;
                res = (res + val[i][p] * dt) % mod;
            }
            clast[p] = t;
        }
    }
    write(res);
    return 0;
}

L. Lottery Tickets

單點時限: 1.0 sec

內存限制: 256 MB

Setsuna is almost out of money! Therefore, she always thinks about ways to get rich overnight.

She finds that buying lottery tickets is a wonderful choice, and she decides to use her lucky number as the lottery number.

Setsuna uses some unique methods to pick her lucky numbers. She will first select n magic cards. Each card is written with a digit between 0 and 9 inclusively. Then she will use some cards to construct a number. She believes that the largest number she can construct which can be divided by 4 is her lucky number.

It is unnecessary to use all the cards, and it is possible that such lucky number does not exist.

Because she is daydreaming of getting rich, only you can help her find this lucky number.

輸入格式

The input consists of multiple test cases.

The first line contains a single integer t(1≤t≤3×105), indicating the number of test cases.

Each of the next t lines contains 10 integers c0,c1,⋯,c9(0≤ci≤105,∑i=09ci>0), where ci indicates the number of cards written with number i.

It is guaranteed that the sum of ∑i=09ci of all test cases does not exceed 3×105.

輸出格式

For each test case given in the input, print the lucky number(without leading zero) or −1 if such lucky number does not exist.

樣例

input

6
0 1 1 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0
1 0 0 2 0 0 0 0 0 0
2 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
0 1 1 1 1 1 1 1 1 1

output

12
4
0
98765432100
9876543120
987654312
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> p;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
vector<p> tails;
const int maxn = 3e5 + 10;
int cnt, v[10], res[maxn];

template<typename T = int>
inline const T read()
{
    T x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = (x << 3) + (x << 1) + ch - '0'; ch = getchar(); }
    return x * f;
}

template<typename T>
inline void write(T x)
{
    if (x < 0) { putchar('-'); x = -x; }
    if (x > 9) write(x / 10);
    putchar(x % 10 + '0');
}

bool cmp(const p& a, const p& b)
{
    int a1 = a.first, a2 = a.second;
    int b1 = b.first, b2 = b.second;
    int mxa = max(a1, a2), mxb = max(b1, b2);
    return mxa == mxb ? min(a1, a2) < min(b1, b2) : mxa < mxb;
}

void getTail()
{
    for (int i = 0; i < 100; i++)
    {
        if (i % 4 == 0)
        {
            int a = i / 10, b = i % 10;
            if (a >= b || (a + b * 10) % 4)
                tails.push_back(p(a, b));
        }
    }
    sort(tails.begin(), tails.end(), cmp);
}

bool solve()
{
    for (p tail : tails)
    {
        int a = tail.first, b = tail.second;
        if (a == b && v[a] < 2) continue;
        if (a != b && (!v[a] || !v[b])) continue;
        v[a]--;
        v[b]--;
        for (int i = 9; i >= 0; i--)
        {
            while (v[i])
            {
                res[cnt++] = i;
                v[i]--;
            }
        }
        res[cnt++] = a;
        res[cnt++] = b;
        return true;
    }
    return false;
}

int main()
{
    getTail();
    int t = read();
    while (t--)
    {
        cnt = 0;
        for (int i = 0; i < 10; i++) v[i] = read();
        if (solve())
        {
            int i = 0;
            while (!res[i] && i < cnt - 1) i++;
            while (i < cnt) write(res[i++]);
        }
        else
        {
            if (v[8]) write(8);
            else if (v[4]) write(4);
            else if (v[0]) write(0);
            else write(-1);
        }
        putchar(10);
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章