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
#include<iostream>
using namespace std;
int main()
{
long long int x,t;
cin>>x>>t;
if(x-t==0)
{
cout<<0<<endl;
}
if(x-t>0)
{
cout<<x-t<<endl;
}
if(x-t<0)
{
cout<<0<<endl;
}
return 0;
}
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
#include<iostream>
using namespace std;
char a[100010];
int main()
{
long long int n=0;
while(~scanf("%c",&a[n]))
{
n++;
}
for(int i=0;i<n;i=i+2)
{
cout<<a[i];
}
return 0;
}
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
#include <iostream>
#include<cstring>
#include <algorithm>
using namespace std;
int a[100001];
int main()
{
int n;
while(cin>>n)
{
int t;
memset(a,0,sizeof(a));
for(int i=1;i<=n;i++)
{
scanf("%d",&t);
a[t+1]++;
a[t-1]++;
a[t]++;
}
sort(a,a+100001);
cout<<a[100000]<<endl;
}
return 0;
}
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
#include<iostream>
using namespace std;
int a[100010];
int main()
{
int n,k=0,temp,c=0;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
if(a[i]!=i)
k++;
}
for(int i=1;i<=n;i++)
{
if(a[i]==i)
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
c++;
}
}
if(k==n)
cout<<0<<endl;
else
cout<<c<<endl;
}