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
- 1≤X≤109
- 1≤t≤109
- 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
100 17
Sample Output 1
83
17 out of the initial 100 grams of sand will be consumed, resulting in 83 grams.
Sample Input 2
48 58
Sample Output 2
0
All 48 grams of sand will be gone, resulting in 0 grams.
Sample Input 3
1000000000 1000000000
Sample Output 3
0
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
atcoder
Sample Output 1
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
aaaa
Sample Output 2
aa
Sample Input 3
z
Sample Output 3
z
Sample Input 4
fukuokayamaguchi
Sample Output 4
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 - Together
Time limit : 2sec / Memory limit : 256MB
Score : 300 points
Problem Statement
You are given an integer sequence of length N, a1,a2,…,aN.
For each 1≤i≤N, 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
- 1≤N≤105
- 0≤ai<105(1≤i≤N)
- 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
7 3 1 4 1 5 9 2
Sample Output 1
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
10 0 1 2 3 4 5 6 7 8 9
Sample Output 2
3
Sample Input 3
1 99999
Sample Output 3
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 pi≠i for all 1≤i≤N. Find the minimum required number of operations to achieve this.
Constraints
- 2≤N≤105
- 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
5 1 4 3 5 2
Sample Output 1
2
Swap 1 and 4, then swap 1 and 3. p 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
2 1 2
Sample Output 2
1
Swapping 1 and 2 satisfies the condition.
Sample Input 3
2 2 1
Sample Output 3
0
The condition is already satisfied initially.
Sample Input 4
9 1 2 4 9 5 8 7 3 6
Sample Output 4
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)