AtCoder Beginner Contest 072 ABCD C++&&Python3

A

A - Sandglass2


Time limit : 2sec / Memory limit : 256MB

Score : 100 points

Problem Statement

We have a sandglass that runs for X seconds. The sand drops from the upper bulb at a rate of 1 gram per second. That is, the upper bulb initially contains X grams of sand.

How many grams of sand will the upper bulb contains after t seconds?

Constraints

  • 1X109
  • 1t109
  • X and t are integers.

Input

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

X t

Output

Print the number of sand in the upper bulb after t second.


Sample Input 1

Copy
100 17

Sample Output 1

Copy
83

17 out of the initial 100 grams of sand will be consumed, resulting in 83 grams.


Sample Input 2

Copy
48 58

Sample Output 2

Copy
0

All 48 grams of sand will be gone, resulting in 0 grams.


Sample Input 3

Copy
1000000000 1000000000

Sample Output 3

Copy
0
题目大意:输入A,B,A-B小于0就输出0,其他就输出A-B

C++:

#include<bits/stdc++.h>
using namespace std;
int main(){
    int x,t;
    cin>>x>>t;
    if(x-t<0) cout<<0<<endl;
    else cout<<x-t<<endl;
}

Python:

a,b=map(int,input().split())
print(max(0,a-b))

B:

B - OddString


Time limit : 2sec / Memory limit : 256MB

Score : 200 points

Problem Statement

You are given a string s consisting of lowercase English letters. Extract all the characters in the odd-indexed positions and print the string obtained by concatenating them. Here, the leftmost character is assigned the index 1.

Constraints

  • Each character in s is a lowercase English letter.
  • 1|s|105

Input

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

s

Output

Print the string obtained by concatenating all the characters in the odd-numbered positions.


Sample Input 1

Copy
atcoder

Sample Output 1

Copy
acdr

Extract the first character a, the third character c, the fifth character d and the seventh character r to obtain acdr.


Sample Input 2

Copy
aaaa

Sample Output 2

Copy
aa

Sample Input 3

Copy
z

Sample Output 3

Copy
z

Sample Input 4

Copy
fukuokayamaguchi

Sample Output 4

Copy
fkoaaauh

题目大意:输出奇数位置字符

C++:

#include<bits/stdc++.h>
using namespace std;
int main(){
    char str[100010];
    cin>>str+1;
    int len=strlen(str+1);
    for(int i=1;i<=len;i+=2){
        cout<<str[i];
    }
}
Python:

print(input()[::2])


C:

C - Together


Time limit : 2sec / Memory limit : 256MB

Score : 300 points

Problem Statement

You are given an integer sequence of length Na1,a2,…,aN.

For each 1iN, you have three choices: add 1 to ai, subtract 1 from ai or do nothing.

After these operations, you select an integer X and count the number of i such that ai=X.

Maximize this count by making optimal choices.

Constraints

  • 1N105
  • 0ai<105(1iN)
  • ai is an integer.

Input

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

N
a1 a2 .. aN

Output

Print the maximum possible number of i such that ai=X.


Sample Input 1

Copy
7
3 1 4 1 5 9 2

Sample Output 1

Copy
4

For example, turn the sequence into 2,2,3,2,6,9,2 and select X=2 to obtain 4, the maximum possible count.


Sample Input 2

Copy
10
0 1 2 3 4 5 6 7 8 9

Sample Output 2

Copy
3

Sample Input 3

Copy
1
99999

Sample Output 3

Copy
1

题目大意:给你一串数字,对于每一个数字有三种操作,要么给这个数字+1,要么-1,要么不变,求改变后的数字串中出现最多次的数字
把每一种记录一下,输出最大值即可

C++:

#include<bits/stdc++.h>
using namespace std;
int num[1000010];
int book[1000010];
int main(){
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        scanf("%d",&num[i]);
        book[num[i]]++;
    }
    int tmp=0;
    int res=0;
    if(n==1) cout<<1<<endl;
    else{
        for(int i=1;i<1000010;i++){
            tmp=book[i]+book[i-1]+book[i+1];
            if(tmp>res) res=tmp;
        }
        cout<<res<<endl;
    }
}

这个不会用python写.....

D:

D - Derangement


Time limit : 2sec / Memory limit : 256MB

Score : 400 points

Problem Statement

You are given a permutation p1,p2,…,pN consisting of 1,2,..,N. You can perform the following operation any number of times (possibly zero):

Operation: Swap two adjacent elements in the permutation.

You want to have pii for all 1iN. Find the minimum required number of operations to achieve this.

Constraints

  • 2N105
  • p1,p2,..,pN is a permutation of 1,2,..,N.

Input

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

N
p1 p2 .. pN

Output

Print the minimum required number of operations


Sample Input 1

Copy
5
1 4 3 5 2

Sample Output 1

Copy
2

Swap 1 and 4, then swap 1 and 3p is now 4,3,1,5,2 and satisfies the condition. This is the minimum possible number, so the answer is 2.


Sample Input 2

Copy
2
1 2

Sample Output 2

Copy
1

Swapping 1 and 2 satisfies the condition.


Sample Input 3

Copy
2
2 1

Sample Output 3

Copy
0

The condition is already satisfied initially.


Sample Input 4

Copy
9
1 2 4 9 5 8 7 3 6

Sample Output 4

Copy
3

题目大意:给你一组数字,如果某个数字的下标等于这个数字,就将他与相邻的元素交换,求交换的最少次数

模拟一下就行了

C++:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll ans;
int num[100010];
int main(){
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        scanf("%d",&num[i]);
        if(num[i]==i) ans++;
    }
    if(!ans){
        cout<<0<<endl;
        return 0;
    }
    ans=0;
    for(int i=1;i<=n;i++){
        if(num[i]==i){
            ans++;
            swap(num[i],num[i+1]);
        }
    }
    cout<<ans<<endl;
}

Python:

n=int(input())
array=list(map(int,input().split()))
ans=0
flag=0
for i in range(len(array)):
    if i+1==array[i]:
        if flag==0:
            ans+=1
            if(i<len(array)-1 and array[i+1]==i+2):
                flag=1
        else:
            flag=0
print(ans)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值