【ACM训练赛】UPC2021寒假23场

问题 A: 记数问题

题目描述

试计算在区间 1 到 n 的所有整数中,数字 x (0 ≤ x ≤ 9)共出现了多少次?例如,在 1到 11 中,即在 1、2、3、4、5、6、7、8、9、10、11 中,数字 1 出现了 4 次。

输入

输入共 1 行,包含 2 个整数 n、x,之间用一个空格隔开。1≤n≤1,000,000 ,0≤x≤9

输出

输出共 1 行,包含一个整数,表示 x 出现的次数。

样例输入 [Copy](javascript:CopyToClipboard($('#sampleinput').text()))

11 1

样例输出 [Copy](javascript:CopyToClipboard($('#sampleoutput').text()))

4

水题直接暴力求解

#include<cstdio>
using namespace std;
int main()
{
    int n, x;
    scanf("%d%d", &n, &x);
    int ans = 0;
    for (int i = 1; i <= n;i++)
    {
        int t = i;
        while (t)
        {
            if(t%10==x)
            {
                ans++;
            }
            t /= 10;
        }
        
    }
    printf("%d", ans);
    return 0;
}

问题 B: 车站分级

题目描述

一条单向的铁路线上,依次有编号为 1, 2, …, n 的 n 个火车站。每个火车站都有一个级别,最低为 1 级。现有若干趟车次在这条线路上行驶,每一趟都满足如下要求:如果这趟车次停靠了火车 站 x,则始发站、终点站之间所有级别大于等于火车站 x 的都必须停靠。(注意:起始站和终点站自然也算作事先已知需要停靠的站点)

例如,下表是 5 趟车次的运行情况。其中,前 4 趟车次均满足要求,而第 5 趟车次由于停靠了 3 号火车站(2 级)却未停靠途经的 6号火车站(亦为 2 级)而不满足要求。

现有 m 趟车次的运行情况(全部满足要求) ,试推算这 n 个火车站至少分为几个不同的级别。

img

输入

第一行包含 2 个正整数 n, m,用一个空格隔开。

第 i + 1 行(1 ≤ i ≤ m)中,首先是一个正整数 si (2 ≤ si≤ n),表示第 i 趟车次有 si 个停靠站;接下来有 si 个正整数,表示所有停靠站的编号,从小到大排列。每两个数之间用一个空格隔开。输入保证所有的车次都满足要求。

输出

输出只有一行,包含一个正整数,即 n 个火车站最少划分的级别数。

样例输入 [Copy](javascript:CopyToClipboard($('#sampleinput').text()))

9 2
4 1 3 5 6
3 3 5 6

样例输出 [Copy](javascript:CopyToClipboard($('#sampleoutput').text()))

2

不是很会

问题 C: 小朋友的数字

题目描述

有n个小朋友排成一列。每个小朋友手上都有一个数字,这个数字可正可负。规定每个小朋友的特征值等于排在他前面(包括他本人)的小朋友中连续若干个(最少有一个)小朋友手上的数字之和的最大值。

作为这些小朋友的老师,你需要给每个小朋友一个分数,分数是这样规定的:第一个小朋友的分数是他的特征值,其它小朋友的分数为排在他前面的所有小朋友中(不包括他本人),小朋友分数加上其特征值的最大值。

请计算所有小朋友分数的最大值,输出时保持最大值的符号,将其绝对值对p取模后输出。

输入

第一行包含两个正整数n、p,之间用一个空格隔开。
第二行包含n个数,每两个整数之间用一个空格隔开,表示每个小朋友手上的数字。

输出

输出只有一行,包含一个整数,表示最大分数对p取模的结果。

样例输入 [Copy](javascript:CopyToClipboard($('#sampleinput').text()))

5 997
1 2 3 4 5

样例输出 [Copy](javascript:CopyToClipboard($('#sampleoutput').text()))

21

提示

样例1小朋友的特征值分别为1、3、6、10、15,分数分别为1、2、5、11、21,最大值21对997的模是21。

对于 100%的数据,1 ≤ n ≤ 1,000,000,1 ≤ p ≤ 10^9,其他数字的绝对值均不超过 10^9。

也不是很会

问题 D: 表达式求值

题目描述

给定一个只包含加法和乘法的算术表达式,请你编程计算表达式的值。

输入

输入仅有一行,为需要你计算的表达式,表达式中只包含数字、加法运算符“+”和乘法运算符“”,且没有括号,所有参与运算的数字均为 0 到 2^31-1 之间的整数。输入数据保证这一行只有 0~ 9、+、这 12 种字符。

输出

输出只有一行,包含一个整数, 表示这个表达式的值。 注意: 当答案长度多于 4 位时,请只输出最后 4 位,前导 0 不输出。

样例输入 [Copy](javascript:CopyToClipboard($('#sampleinput').text()))

1+1000000003*1

样例输出 [Copy](javascript:CopyToClipboard($('#sampleoutput').text()))

4

纯模拟题,一个temp存当前累计的数字,然后碰到加号加起来,碰到乘号更新temp

#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iostream>
using namespace std;
int main()
{
    long long temp=0, ans=0;
    char c1=0,c2=0;
    long long a1, a2;
    cin >> a1;
    temp = a1;
    while(cin>>c2>>a2)
    {
        if(c2=='+')
        {
            ans = (temp + ans) % 10000;
            temp = a2;
        }
        if(c2=='*')
        {
            temp = (temp * a2) % 10000;
        }
        c1 = c2;
    }
    ans += temp;
    printf("%lld",ans% 10000);
    return 0;
}

问题 E: Restaurant

题目描述

Snuke has a favorite restaurant.
The price of any meal served at the restaurant is 800 yen (the currency of Japan), and each time a customer orders 15 meals, the restaurant pays 200 yen back to the customer.
So far, Snuke has ordered N meals at the restaurant. Let the amount of money Snuke has paid to the restaurant be x yen, and let the amount of money the restaurant has paid back to Snuke be y yen. Find x−y.

Constraints
1≤N≤100

输入

The input is given from Standard Input in the following format:
N

输出

Print the answer.

样例输入 [Copy](javascript:CopyToClipboard($('#sampleinput').text()))

20

样例输出 [Copy](javascript:CopyToClipboard($('#sampleoutput').text()))

15800

提示

So far, Snuke has paid 16000 yen, and the restaurant has paid back 200 yen. Thus, the answer is 15800.

#include<cstdio>
using namespace std;
int main()
{
    int n;
    scanf("%d", &n);
    printf("%d", (n % 15 * 800) + (n / 15) * (15 * 800 - 200));
    return 0;
}

问题 F: Training Camp

题目描述

Snuke loves working out. He is now exercising N times.
Before he starts exercising, his power is 1. After he exercises for the i-th time, his power gets multiplied by i.
Find Snuke's power after he exercises N times. Since the answer can be extremely large, print the answer modulo 109+7.

Constraints
1≤N≤105

输入

The input is given from Standard Input in the following format:
N

输出

Print the answer modulo 109+7.

样例输入 [Copy](javascript:CopyToClipboard($('#sampleinput').text()))

3

样例输出 [Copy](javascript:CopyToClipboard($('#sampleoutput').text()))

6

提示

After Snuke exercises for the first time, his power gets multiplied by 1 and becomes 1.
After Snuke exercises for the second time, his power gets multiplied by 2 and becomes 2.
After Snuke exercises for the third time, his power gets multiplied by 3 and becomes 6.

#include<cstdio>
using namespace std;
int main()
{
    int n;
    scanf("%d", &n);
    long long ans = 1;
    for (int i = 1; i <= n;i++)
    {
        ans = (ans * i) % 1000000007;
    }
    printf("%lld", ans);
    return 0;
}

问题 G: Scc Puzzle

题目描述

Snuke loves puzzles.
Today, he is working on a puzzle using S- and c-shaped pieces. In this puzzle, you can combine two c-shaped pieces into one S-shaped piece, as shown in the figure below:

img

Snuke decided to create as many Scc groups as possible by putting together one S-shaped piece and two c-shaped pieces.
Find the maximum number of Scc groups that can be created when Snuke has N S-shaped pieces and M c-shaped pieces.

Constraints
1≤N,M≤1012

输入

The input is given from Standard Input in the following format:
N M

输出

Print the answer.

样例输入 [Copy](javascript:CopyToClipboard($('#sampleinput').text()))

1 6

样例输出 [Copy](javascript:CopyToClipboard($('#sampleoutput').text()))

2

额这个也挺水,有匹配的直接匹配掉,剩下的除以4就是另外的方案数,加上就好了

#include<cstdio>
using namespace std;
int main()
{
    long long n, m;
    scanf("%lld%lld", &n, &m);
    long long ans = 0;
    if(m>=2*n)
    {
        m -= 2 * n;
        ans = n + m / 4;
    }
    else
    {
        ans = m / 2;
    }
    printf("%lld", ans);
    return 0;
}

问题 K: Teleportation II

题目描述

One of the farming chores Farmer John dislikes the most is hauling around lots of cow manure. In order to streamline this process, he comes up with a brilliant invention: the manure teleporter! Instead of hauling manure between two points in a cart behind his tractor, he can use the manure teleporter to instantly transport manure from one location to another.
Farmer John's farm is built along a single long straight road, so any location on his farm can be described simply using its position along this road (effectively a point on the number line). A teleporter is described by two numbers x and y, where manure brought to location x can be instantly transported to location y, or vice versa.

Farmer John wants to transport manure from location a to location b, and he has built a teleporter that might be helpful during this process (of course, he doesn't need to use the teleporter if it doesn't help). Please help him determine the minimum amount of total distance he needs to haul the manure using his tractor.

输入

The first and only line of input contains four space-separated integers: a and b, describing the start and end locations, followed by x and y, describing the teleporter. All positions are integers in the range 0…100, and they are not necessarily distinct from each-other.

输出

Print a single integer giving the minimum distance Farmer John needs to haul manure in his tractor.

样例输入 [Copy](javascript:CopyToClipboard($('#sampleinput').text()))

3 10 8 2

样例输出 [Copy](javascript:CopyToClipboard($('#sampleoutput').text()))

3

提示

In this example, the best strategy is to haul the manure from position 3 to position 2, teleport it to position 8, then haul it to position 10. The total distance requiring the tractor is therefore 1 + 2 = 3.

就判断直接过去还是传送过去距离谁小就可以

#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
    int a, b, x, y;
    scanf("%d%d%d%d", &a, &b, &x, &y);
    if(a>b)
        swap(a, b);
    if(x>y)
        swap(x, y);
    int ans = min(b - a, abs(a - x) + abs(b - y));
    printf("%d", ans);
    return 0;
}

问题 L: Hoofball

题目描述

In preparation for the upcoming hoofball tournament, Farmer John is drilling his N cows (conveniently numbered 1…N, where 1≤N≤100) in passing the ball. The cows are all standing along a very long line on one side of the barn, with cow ii standing xi units away from the barn (1≤xi≤1000). Each cow is standing at a distinct location.
At the begiNing of the drill, Farmer John will pass several balls to different cows. When cow i receives a ball, either from Farmer John or from another cow, she will pass the ball to the cow nearest her (and if multiple cows are the same distance from her, she will pass the ball to the cow farthest to the left among these). So that all cows get at least a little bit of practice passing, Farmer John wants to make sure that every cow will hold a ball at least once. Help him figure out the minimum number of balls he needs to distribute initially to ensure this can happen, assuming he hands the balls to an appropriate initial set of cows.

输入

The first line of input contains N. The second line contains N space-separated integers, where the ith integer is xi.

输出

Please output the minimum number of balls Farmer John must initially pass to the cows, so that every cow can hold a ball at least once.

样例输入 [Copy](javascript:CopyToClipboard($('#sampleinput').text()))

5
7 1 3 11 4

样例输出 [Copy](javascript:CopyToClipboard($('#sampleoutput').text()))

2

提示

In the above example, Farmer John should pass a ball to the cow at x=1 and pass a ball to the cow at x=11. The cow at x=1 will pass her ball to the cow at x=3, after which this ball will oscillate between the cow at x=3 and the cow at x=4. The cow at x=11 will pass her ball to the cow at x=7, who will pass the ball to the cow at x=4, after which this ball will also cycle between the cow at x=3 and the cow at x=4. In this way, all cows will be passed a ball at least once (possibly by Farmer John, possibly by another cow).

It can be seen that there is no single cow to whom Farmer John could initially pass a ball so that every cow would eventually be passed a ball.

因为是传给最近的而且一样的话传左边,所以肯定有像题目中给的3,4循环的情况,这个时候我们分情况

先排序

对于循环,我们看是否能从左右都传进来,如果是只可以从一侧传过来,这个时候可以从一侧直接跳过这个循环也就是说只需要传一次。但如果可以从两侧传过来就需要从两侧分别传了,也就是传两次

对于第1个人,这个时候也可能会有1,2陷入循环的可能

对于最后一个,这时候也可能有\(n-1\)\(n\)循环的可能,但是我们是根据前一个数判断的循环,这个时候我们只需要看\(n-1\)的那个情况就好了;

对于首尾的两个循环,一定可以从一侧传进来,这时候要特殊处理一下

#include<cstdio>
#include<algorithm>
using namespace std;
int a[1006];
int main()
{
    int n;
    scanf("%d", &n);
    int ans = 0;
    for (int i = 1; i <= n;i++)
    {
        scanf("%d", &a[i]);
    }
    sort(a + 1, a + n + 1);
    for (int i = 1; i <= n - 1;i++)
    {
        int flag = 0;
        if(a[i+1]-a[i]<=a[i+2]-a[i+1]&&i==1)
        {
            flag = 1;
        }
        else if(a[i+1]-a[i]<a[i]-a[i-1]&&i==n-1)
        {
            flag = 1;
        }
        else if(a[i+1]-a[i]<=a[i+2]-a[i+1]&&a[i+1]-a[i]<a[i]-a[i-1])
        {
            flag = 1;
        }
        if(flag==1)
        {
            ans++;
            if((i==2||a[i]-a[i-1]<a[i-1]-a[i-2])&&(i==n-2||a[i+2]-a[i+1]<=a[i+3]-a[i+2]))
                ans++;
        }
    }
    printf("%d", ans);
    return 0;
}
posted @ 2021-02-19 22:32  melodit  阅读(177)  评论(0)    收藏  举报