[八中测试]AtCoder ABC093

A - abc of ABC

Problem Statement

You are given a string S of length 3 consisting of a, b and c. Determine if S can be obtained by permuting abc.

Constraints

|S|=3
S consists of a, b and c.
Input
Input is given from Standard Input in the following format:

S

Output

If S can be obtained by permuting abc, print Yes; otherwise, print No.

Sample Input 1

bac

Sample Output 1

Yes

Swapping the first and second characters in bac results in abc.

Sample Input 2

bab

Sample Output 2

No

Sample Input 3

abc

Sample Output 3

Yes

Sample Input 4

aaa

Sample Output 4

No

【解析】

第一题应该很好理解,也应该很快可以完成,就是一个暴力判定,就完。

AC代码:

#include<cstdio>
#define N 6
char s[N];
bool f[N];
int main()
{
    scanf("%s",s);
    for(int i=0;i<3;i++)
    f[int(s[i]-97)]=1;
    if(f[0]&&f[1]&&f[2]) printf("Yes\n");
    else printf("No\n");
}
----------------------------------------------------------------

B - Small and Large Integers

Problem Statement

Print all the integers that satisfies the following in ascending order:

Among the integers between A and B (inclusive), it is either within the K smallest integers or within the K largest integers.

Constraints

1≤A≤B≤109
1≤K≤100
All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B K

Output

Print all the integers that satisfies the condition above in ascending order.

Sample Input 1

3 8 2

Sample Output 1

3
4
7
8

3 is the first smallest integer among the integers between 3 and 8.
4 is the second smallest integer among the integers between 3 and 8.
7 is the second largest integer among the integers between 3 and 8.
8 is the first largest integer among the integers between 3 and 8.

Sample Input 2

4 8 3

Sample Output 2

4
5
6
7
8

Sample Input 3

2 9 100

Sample Output 3

2
3
4
5
6
7
8
9

【解析】

这道题应该也很容易完成,就一个顺序输出和逆序输出的问题吧。
AC代码:

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int A,B,K;
int cnt=0;
int a[106];
int main()
{
    scanf("%d %d %d",&A,&B,&K);
    for(int i=A;i<=min(A+K-1,B);i++)
    {
    printf("%d\n",i);
    }
    for(int i=B;i>=max(B-K+1,A+K);i--)
    {
        a[++cnt]=i;
    }
    sort(a+1,a+1+cnt);
    for(int i=1;i<=cnt;i++)
    printf("%d\n",a[i]);
}
----------------------------------------------------------------

C - Same Integers

Problem Statement

You are given three integers A, B and C. Find the minimum number of operations required to make A, B and C all equal by repeatedly performing the following two kinds of operations in any order:

Choose two among A, B and C, then increase both by 1.
Choose one among A, B and C, then increase it by 2.
It can be proved that we can always make A, B and C all equal by repeatedly performing these operations.

Constraints

0≤A,B,C≤50
All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C

Output

Print the minimum number of operations required to make A, B and C all equal.

Sample Input 1

2 5 4

Sample Output 1

2

We can make A, B and C all equal by the following operations:

Increase A and C by 1. Now, A, B, C are 3, 5, 5, respectively.
Increase A by 2. Now, A, B, C are 5, 5, 5, respectively.

Sample Input 2

2 6 3

Sample Output 2

5

Sample Input 3

31 41 5

Sample Output 3

23

【解析】

这道题看看数据,暴力模拟应该是没有问题的,所以,何乐而不为呢?
方法一:【暴力模拟】

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[5];
int cnt=0;
int main()
{
    for(int i=1;i<=3;i++)
        scanf("%d",&a[i]);
    int cnt=0;
    int f=1;
    while(f)
    {
        if(a[1]==a[2]&&a[2]==a[3]) break;
        sort(a+1,a+1+3);
        while(a[1]+2<=a[3])a[1]+=2,cnt++;
        if(a[1]==a[2]&&a[2]==a[3]) break;
        while(a[2]+2<=a[3])a[2]+=2,cnt++;
        if(a[1]==a[2]&&a[2]==a[3]) break;
        if(a[2]==a[3]&&a[1]!=a[2]) a[1]+=2,a[2]+=1,a[3]+=1,cnt+=2;\
        if(a[1]==a[2]&&a[2]==a[3]) break;
        if((a[1]+1<=a[3]&&a[2]+1<=a[3]))a[1]+=1,a[2]+=1,cnt++;
    }
    printf("%d",cnt);
}

当然,还有一种数学方法,可以进行探究。

----------------------------------------------------------------

E - Tozan and Gezan

Problem Statement

You are given sequences A and B consisting of non-negative integers. The lengths of both A and B are N, and the sums of the elements in A and B are equal. The i-th element in A is Ai, and the i-th element in B is Bi.

Tozan and Gezan repeats the following sequence of operations:

If A and B are equal sequences, terminate the process.
Otherwise, first Tozan chooses a positive element in A and decrease it by 1.
Then, Gezan chooses a positive element in B and decrease it by 1.
Then, give one candy to Takahashi, their pet.
Tozan wants the number of candies given to Takahashi until the process is terminated to be as large as possible, while Gezan wants it to be as small as possible. Find the number of candies given to Takahashi when both of them perform the operations optimally.

Constraints

1≤N≤2×105
0≤Ai,Bi≤109(1≤i≤N)
The sums of the elements in A and B are equal.
All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A1 B1
:
AN BN

Output

Print the number of candies given to Takahashi when both Tozan and Gezan perform the operations optimally.

Sample Input 1

2
1 2
3 2

Sample Output 1

2

When both Tozan and Gezan perform the operations optimally, the process will proceed as follows:

Tozan decreases A1 by 1.
Gezan decreases B1 by 1.
One candy is given to Takahashi.
Tozan decreases A2 by 1.
Gezan decreases B1 by 1.
One candy is given to Takahashi.
As A and B are equal, the process is terminated.

Sample Input 2

3
8 3
0 1
4 8

Sample Output 2

9

Sample Input 3

1
1 1

Sample Output 3

0

【解析】

题意:给出A B 2个序列,他们的序列总和相同,2个人T和G,双方轮流选择序列中一个正整数,进行减一,

T想尽可能的使步数大,G想尽可能的使步数小,计算使得2个序列完全相同的步数

当所有的Bi=Ai的时候输出0

G:为了使得步数尽可能的小,当Bi>Ai的时候只要减少Bi即可,当Bi<Ai的时候,不动即可

T:为了使得步数尽可能的大,当Bi>Ai的时候,Ai必定最后为0,当Bi<Ai的时候,记录最小的Bi为Bk(k为符合条件的最小值的下标),让G误以为所有的A都是要趋向于Bk,实则是为了变为0,除非T不得不移动Ak(也就是其他的A都为0)

综上得:结果为sum-minb
AC代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define INF 0x7fffffff
#define N 200007
int n;
long long cnt,a,b,minb=INF;
bool f=0;
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%lld %lld",&a,&b);
        cnt+=b;
        if(b<a&&minb>b)
        minb=b;
        if(b!=a) f=1; 
    }
    if(f)printf("%lld\n",cnt-minb);
    else printf("0\n");
}

猜你喜欢

转载自blog.csdn.net/qq_37862149/article/details/79895693