[八中試験]AtCoder ABC 093


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

【解析】


第1題はよく理解すべきで、すぐに完成するべきで、1つの暴力の判定で、終わります.
ACコード:
#include
#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
"
); else printf("No
"
); }
----------------------------------------------------------------

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
#include
#include
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
"
,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
"
,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

【解析】


この問題はデータを見て、暴力シミュレーションは問題ないはずだ.だから、喜んでやらないのだろうか.方法1:【暴力シミュレーション】
#include
#include
#include
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、双方は順番にシーケンスの中の1つの正の整数を選んで、1を減らして、
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
#include
#include
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(bb)
minb=b;
if(b!=a) f=1; 
}
if(f)printf("%lld",cnt-minb);
else printf("0");
}