【ACM训练赛】UPC2021寒假27场

问题 A: 成绩

题目描述

牛牛最近学习了 C++入门课程,这门课程的总成绩计算方法是:
总成绩 = 作业成绩 × 20% + 小测成绩 × 30% + 期末考试成绩 × 50%
牛牛想知道,这门课程自己最终能得到多少分。

输入

只有1行,包含三个非负整数A、B、C,分别表示牛牛的作业成绩、小测成绩和期末考试成绩。相邻两个数之间用一个空格隔开,三项成绩满分都是100分。

输出

只有1行,包含一个整数,即牛牛这门课程的总成绩,满分也是100分。

样例输入

100 100 80

样例输出

90

提示

牛牛的作业成绩是100 分,小测成绩是100 分,期末考试成绩是80 分,总成绩是100 × 20% + 100 × 30% + 80 × 50% = 20 + 30 + 40 = 90
对于30% 的数据, A = B = 0 。
对于另外30% 的数据, A = B = 100 。
对于100% 的数据,0 ≤ A 、 B 、 C ≤ 100 且A 、 B 、 C 都是 10 的整数倍。

水笔题

#include<cstdio>
using namespace std;
int main()
{
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    printf("%d", int(a * 0.2 + b * 0.3 +c * 0.5));
    return 0;
}

问题 B: 图书管理员

题目描述

图书馆中每本书都有一个图书编码,可以用于快速检索图书,这个图书编码是一个正整数。
每位借书的读者手中有一个需求码,这个需求码也是一个正整数。如果一本书的图书编码恰好以读者的需求码结尾,那么这本书就是这位读者所需要的。
小 D 刚刚当上图书馆的管理员,她知道图书馆里所有书的图书编码,她请你帮她写一个程序,对于每一位读者,求出他所需要的书中图书编码最小的那本书,如果没有他需要的书,请输出-1。

输入

第一行,包含两个正整数 n 和 q,以一个空格分开,分别代表图书馆里书的数量和读者的数量。
接下来的 n 行,每行包含一个正整数,代表图书馆里某本书的图书编码。
接下来的 q 行,每行包含两个正整数,以一个空格分开,第一个正整数代表图书馆里读者的需求码的长度,第二个正整数代表读者的需求码。

输出

输出有 q 行,每行包含一个整数,如果存在第 i 个读者所需要的书,则在第 i行输出第 i 个读者所需要的书中图书编码最小的那本书的图书编码,否则输出-1。

样例输入

5 5
2123
1123
23
24
24
2 23
3 123
3 124
2 12
2 12

样例输出

23
1123
-1
-1
-1

提示

第一位读者需要的书有 2123、1123、23,其中 23 是最小的图书编码。第二位读者需要的书有 2123、1123,其中 1123 是最小的图书编码。对于第三位,第四位和第五位读者,没有书的图书编码以他们的需求码结尾,即没有他们需要的书,输出-1。
对于 20%的数据,1 ≤ n ≤ 2。
另有 20%的数据,q = 1。
另有 20%的数据,所有读者的需求码的长度均为 1。
另有 20%的数据,所有的图书编码按从小到大的顺序给出。
对于 100%的数据,1 ≤ n ≤ 1,000,1 ≤ q ≤ 1,000,所有的图书编码和需求码均不超过 10,000,000。

都把位数告诉你了直接pow完了取模比较就可以

#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
int a[1005];
int main()
{
    int n, q;
    scanf("%d%d", &n, &q);
    for (int i = 1; i <= n;i++)
    {
        scanf("%d", &a[i]);
    }
    sort(a + 1, a + n + 1);
    for (int i = 1; i <= q;i++)
    {
        int l, xq;
        scanf("%d%d", &l, &xq);
        int mod = pow(10, l);
        int flag = 0;
        for (int i = 1; i <= n;i++)
        {
            if(a[i]%mod==xq)
            {
                printf("%d\n", a[i]);
                flag = 1;
                break;
            }
        }
        if(flag==0)
        {
            printf("-1\n");
        }
    }
    return 0;
}

问题 E: Shiritori

题目描述

You are given three strings A, B and C. Check whether they form a word chain.
More formally, determine whether both of the following are true:
The last character in A and the initial character in B are the same.
The last character in B and the initial character in C are the same.
If both are true, print YES. Otherwise, print NO.

Constraints
A, B and C are all composed of lowercase English letters (a - z).
1≤|A|,|B|,|C|≤10, where |A|, |B| and |C| are the lengths of A, B and C, respectively.

输入

Input is given from Standard Input in the following format:
A B C

输出

Print YES or NO.

样例输入

rng gorilla apple

样例输出

YES

提示

They form a word chain.

水笔题+1

#include<cstdio>
#include<iostream>
using namespace std;
int main()
{
    string a, b, c;
    cin >> a >> b >> c;
    if(a[a.size()-1]==b[0]&&b[b.size()-1]==c[0])
    {
        printf("YES");
    }
    else
    {
        printf("NO");
    }
    return 0;
}

问题 F: Choose Integers

题目描述

We ask you to select some number of positive integers, and calculate the sum of them.
It is allowed to select as many integers as you like, and as large integers as you wish. You have to follow these, however: each selected integer needs to be a multiple of A, and you need to select at least one integer.
Your objective is to make the sum congruent to C modulo B. Determine whether this is possible.
If the objective is achievable, print YES. Otherwise, print NO.

Constraints
1≤A≤100
1≤B≤100
0≤C<B

输入

Input is given from Standard Input in the following format:
A B C

输出

Print YES or NO.

样例输入

7 5 1

样例输出

YES

提示

For example, if you select 7 and 14, the sum 21 is congruent to 1 modulo 5.

就问你是否有a的倍数模b的情况下等于c

#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    for (long long i = 0; i <= 100000007; i += a)
    {
        if(i%b==c)
        {
            printf("YES");
            return 0;
        }
    }
    printf("NO");
    return 0;
}

问题 G: Sentou

题目描述

In a public bath, there is a shower which emits water for T seconds when the switch is pushed.
If the switch is pushed when the shower is already emitting water, from that moment it will be emitting water for T seconds. Note that it does not mean that the shower emits water for T additional seconds.
N people will push the switch while passing by the shower. The i-th person will push the switch ti seconds after the first person pushes it.
How long will the shower emit water in total?

Constraints
1≤N≤200,000
1≤T≤109
0=t1<t2<t3<,…,<tN−1<tN≤109
T and each ti are integers.

输入

Input is given from Standard Input in the following format:
N T
t1 t2 ... tN

输出

Assume that the shower will emit water for a total of X seconds. Print X.

样例输入

2 4
0 3

样例输出

7

提示

Three seconds after the first person pushes the water, the switch is pushed again and the shower emits water for four more seconds, for a total of seven seconds.

就看看每一个区间大还是T大,因为如果区间间隔小的话,中间水不会停,这时候防水持续时长就是\(a[i]-a[i-1]\)但如果大于\(T\)的时候就为\(T\)最后别忘了加上\(T\),最后一个人按下后还要再放\(T\)

#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
int a[200005];
int main()
{
    int n, T;
    scanf("%d%d", &n, &T);
    long long ans = 0;
    scanf("%d", &a[1]);
    for (int i = 2; i <= n;i++)
    {
        scanf("%d", &a[i]);
        ans += min(a[i] - a[i-1], T);
    }
    printf("%lld", ans + T);
    return 0;
}

问题 H: Simple Knapsack

题目描述

You have N items and a bag of strength W. The i-th item has a weight of wi and a value of vi.
You will select some of the items and put them in the bag. Here, the total weight of the selected items needs to be at most W.
Your objective is to maximize the total value of the selected items.

Constraints
1≤N≤100
1≤W≤109
1≤wi≤109
For each i=2,3,…,N, w1≤wi≤w1+3.
1≤vi≤107
W, each wi and vi are integers.

输入

Input is given from Standard Input in the following format:
N W
w1 v1
w2 v2
:
wN vN

输出

Print the maximum possible total value of the selected items.

样例输入

4 6
2 1
3 4
4 10
3 4

样例输出

11

提示

The first and third items should be selected.

大整数下的背包问题

数据范围极牛逼,普通01背包是肯定过不去的 这点我是写完01才知道的

最后还是baidu的答案

因为我们通过$ w1≤wi≤w1+3.$看出,相差不超过4

这时候我们只需要用\(w_i-w_1-1\)的数值作为标识符,再去枚举背包容量,我们设置一个sum,为所有标识符总和,即在循环下的最大值;

继续01背包的循环,在最后有个特判\(k*w_0+j<=tot\)时,更新ans

#include<cstdio>
#include<cmath>
#include<algorithm>
#include<iostream>
using namespace std;
long long w[105], v[105];
long long dp[1009][105];
int main()
{
    long long n, tot;
    scanf("%lld%lld", &n, &tot);
    scanf("%lld%lld", &w[1], &v[1]);
    w[0] = w[1] - 1;
    w[1] = 1;
    long long sum = 1, ans = 0;
    for (int i = 2; i <= n;i++)
    {
        scanf("%lld%lld", &w[i], &v[i]);
        w[i] -= w[0];
        sum += w[i];
    }
    for (int i = 1; i <= n; i++)
    {
        for (int k = i; k >= 1;k--)
        {
            for (long long j = sum; j >= w[i];j--)
            {
                dp[j][k] = max(dp[j][k], dp[j - w[i]][k - 1] + v[i]);
                if(k*w[0]+j<=tot)
                    ans = max(ans, dp[j][k]);
            }
        }
    }
    printf("%lld", ans);
    return 0;
}

问题 K: Square Pasture

题目描述

Farmer John has decided to update his farm to simplify its geometry. Previously, his cows grazed in two rectangular fenced-in pastures. Farmer John would like to replace these with a single square fenced-in pasture of minimum size that still covers all the regions of his farm that were previously enclosed by the former two fences.
Please help Farmer John figure out the minimum area he needs to make his new square pasture so that if he places it appropriately, it can still cover all the area formerly covered by the two older rectangular pastures. The square pasture should have its sides parallel to the x and y axes.

输入

The first line in the input file specifies one of the original rectangular pastures with four space-separated integers x1 y1 x2 y2, each in the range 0…10. The lower-left corner of the pasture is at the point (x1,y1), and the upper-right corner is at the point (x2,y2), where x2>x1 and y2>y1.
The second line of input has the same 4-integer format as the first line, and specifies the second original rectangular pasture. This pasture will not overlap or touch the first pasture.

输出

The output should consist of one line containing the minimum area required of a square pasture that would cover all the regions originally enclosed by the two rectangular pastures.

样例输入

6 6 8 8
1 8 4 9

样例输出

49

提示

In the example above, the first original rectangle has corners (6,6) and (8,8). The second has corners at (1,8) and (4,9). By drawing a square fence of side length 7 with corners (1,6) and (8,13), the original areas can still be enclosed; moreover, this is the best possible, since it is impossible to enclose the original areas with a square of side length only 6. Note that there are several different possible valid placements for the square of side length 7, as it could have been shifted vertically a bit.

水笔题,找到左上角和右上角的两个点,看看哪个边长大就可以了

#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
int maxx, maxy, minx, miny;
int a[104][104];
int main()
{
    for (int i = 1; i <= 2;i++)
    {
        for (int j = 1; j <= 4;j++)
        {
            cin >> a[i][j];
        }
    }
    maxx = max(a[1][3], a[2][3]);
    maxy = max(a[1][4], a[2][4]);
    minx = min(a[1][1], a[2][1]);
    miny = min(a[1][2], a[2][2]);
    int ans = max(maxy - miny, maxx - minx);
    printf("%d", ans * ans);
    return 0;
}

问题 L: Block Game

题目描述

Farmer John is trying to teach his cows to read by giving them a set of N spelling boards typically used with preschoolers (1≤N≤100). Each board has a word and an image on each side. For example, one side might have the word 'cat' along with a picture of a cat, and the other side might have the word 'dog' along with a picture of a dog. When the boards are lying on the ground, N words are therefore shown. By flipping over some of the boards, a different set of N words can be exposed.
To help the cows with their spelling, Farmer John wants to fashion a number of wooden blocks, each embossed with a single letter of the alphabet. He wants to make sufficiently many blocks of each letter so that no matter which set of Nwords is exposed on the upward-facing boards, the cows will be able to spell all of these words using the blocks. For example, if N=3 and the words 'box', 'cat', and 'car' were facing upward, the cows would need at least one 'b' block, one 'o' block, one 'x' block, two 'c' blocks, two 'a' blocks, one 't' block, and one 'r' block.

Please help the Farmer John determine the minimum number of blocks for each letter of the alphabet that he needs to provide, so that irrespective of which face of each board is showing, the cows can spell all N visible words.

输入

Line 1 contains the integer N.
The next N lines each contain 2 words separated by a space, giving the two words on opposite sides of a board. Each word is a string of at most 10 lowercase letters.

输出

Please output 26 lines. The first output line should contain a number specifying the number of copies of 'a' blocks needed. The next line should specify the number of 'b' blocks needed, and so on.

样例输入

3
fox box
dog cat
car bus

样例输出

2
2
2
1
0
1
1
0
0
0
0
0
0
0
2
0
0
1
1
1
1
0
0
1
0
0

提示

In this example, there are N=3 boards, giving 23=8 possibilities for the set of upward-facing words:

fox dog car
fox dog bus
fox cat car
fox cat bus
box dog car
box dog bus
box cat car
box cat bus
We need enough blocks for each letter of the alphabet so that we can spell all three words, irrespective of which of these eight scenarios occurs.

翻牌子,在每一组里面都是可以替换的,只要在输入每一组的时候枚举两边每一个字母的个数,再比较下选大的那一个加到ans里就可以

#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
    int ans[105];
    int n1[104], n2[105];
int main()
{
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n;i++)
    {
        string a, b;
        cin >> a >> b;
        for (int i = 0; i < a.size();i++)
        {
            n1[a[i] - 'a']++;
        }
        for (int i = 0; i < b.size();i++)
        {
            n2[b[i] - 'a']++;
        }
        for (int i = 0; i <26;i++)
        {
            ans[i] += max(n1[i], n2[i]);
            n1[i] = 0;
            n2[i] = 0;
        }
    }
    for (int i = 0; i <26;i++)
    {
        printf("%d\n", ans[i]);
    }
    return 0;
}

问题 M: The Cow-Signal

题目描述

Bessie and her cow friends are playing as their favorite cow superheroes. Of course, everyone knows that any self-respecting superhero needs a signal to call them to action. Bessie has drawn a special signal on a sheet of M×N paper (1≤M≤10,1≤N≤10), but this is too small, much too small! Bessie wants to amplify the signal so it is exactly K times bigger (1≤K≤10) in each direction.
The signal will consist only of the '.' and 'X' characters.

输入

The first line of input contains M, N, and K, separated by spaces.
The next M lines each contain a length-N string, collectively describing the picture of the signal.

输出

You should output K**M lines, each with K**N characters, giving a picture of the enlarged signal.

样例输入

5 4 2
XXX.
X..X
XXX.
X..X
XXX.

样例输出

XXXXXX..
XXXXXX..
XX....XX
XX....XX
XXXXXX..
XXXXXX..
XX....XX
XX....XX
XXXXXX..
XXXXXX..

题目让干啥就干啥就完了

#include<cstdio>
using namespace std;
char a[104][104];
int main()
{
    int n, m, k;
    scanf("%d %d %d", &n, &m, &k);
    getchar();
    for (int i = 1; i <= n;i++)
    {
        for (int j = 1; j <= m;j++)
        {
            scanf("%c", &a[i][j]);
        }
        getchar();
    }
    for (int i = 1; i <= n;i++)
    {
        for (int t1 = 1; t1 <= k;t1++)
        {
            for (int j = 1; j <= m;j++)
            {
                for (int t2 = 1; t2 <= k;t2++)
                {
                    printf("%c", a[i][j]);
                }
            }
            printf("\n");
        }
    }
    return 0;
}
posted @ 2021-02-22 22:21  melodit  阅读(390)  评论(0)    收藏  举报