Problem Statement
Snuke has N integers: 1,2,[ldots],N. He will choose K of them and give those to Takahashi.
How many ways are there to choose K consecutive integers?
Constraints
Input
Input is given from Standard Input in the following format:
N KOutput
Print the answer.
Sample Input 1
3 2Sample Output 1
2There are two ways to choose two consecutive integers: (1,2) and (2,3).
Sample Input 2
13 3Sample Output 2
11AC
#include<iostream>
using namespace std;
int main()
{
int n,k;
while(cin>>n>>k)
{
cout<<(n-k)+1<<endl;
}
return 0;
}Problem Statement
For an integer N, we will choose a permutation {P1,P2,…,P**N} of {1,2,…,N}.
Then, for each i=1,2,…,N, let M**i be the remainder when i is divided by P**i.
Find the maximum possible value of M1+M2+[cdots]+M**N.
Constraints
Input
Input is given from Standard Input in the following format:
NOutput
Print the maximum possible value of M1+M2+[cdots]+M**N.
Sample Input 1
2Sample Output 1
1When the permutation {P1,P2}={2,1} is chosen, M1+M2=1+0=1.
Sample Input 2
13Sample Output 2
78Sample Input 3
1Sample Output 3
0AC
#include<iostream>
using namespace std;
int main(){
long long int n;
cin>>n;
long long int sum = 0 ;
for(long long int i=1;i<=n-1;i++)
sum+=i;
cout<<sum<<endl;
return 0;
}Problem Statement
There are N monsters, numbered 1,2,…,N.
Initially, the health of Monster i is A**i.
Below, a monster with at least 1 health is called alive.
Until there is only one alive monster, the following is repeated:
Find the minimum possible final health of the last monster alive.
Constraints
Input
Input is given from Standard Input in the following format:
N
A1 A2 … ANOutput
Print the minimum possible final health of the last monster alive.
Sample Input 1
4
2 10 8 40Sample Output 1
2When only the first monster keeps on attacking, the final health of the last monster will be 2, which is minimum.
Sample Input 2
4
5 13 8 1000000000Sample Output 2
1Sample Input 3
3
1000000000 1000000000 1000000000Sample Output 3
1000000000AC
#include <cstdio>
#include <algorithm>
using namespace std;
int a[(int)1e5 + 10];
int main() {
int n;
while(~scanf("%d", &n))
{
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
int begin = 0;
int ans;
int flag = 0;
while (true) {
sort(a + begin, a + n);
while (a[begin] == 0) begin++;
if (begin == n - 1) break;
for (int i = begin + 1; i < n; i++) {
a[i] %= a[begin];
if (a[i] == 1) {
ans = 1;
flag = 1;
break;
}
}
if(flag)
break;
}
if(!flag)
ans = a[n-1];
printf("%d\n", ans);
}
return 0;
}Problem Statement
Takahashi is going to buy N items one by one.
The price of the i-th item he buys is A**i yen (the currency of Japan).
He has M discount tickets, and he can use any number of them when buying an item.
If Y tickets are used when buying an item priced X yen, he can get the item for X/2^Y
(rounded down to the nearest integer) yen
What is the minimum amount of money required to buy all the items?
Constraints
Input
Input is given from Standard Input in the following format:
N M
A1 A2 … ANOutput
Print the minimum amount of money required to buy all the items.
Sample Input 1
3 3
2 13 8Sample Output 1
9We can buy all the items for 9 yen, as follows:
Sample Input 2
4 4
1 9 3 5Sample Output 2
6Sample Input 3
1 100000
1000000000Sample Output 3
0We can buy the item priced 1000000000 yen for 0 yen with 100000 tickets.
Sample Input 4
10 1
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000Sample Output 4
9500000000AC
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
int main(){
int m,n;
while(cin>>n>>m)
{
priority_queue<int>q;
int t;
for(int i=0;i<n;i++)
{
cin>>t;
q.push(t);
}
long long sum=0;
while(m)
{
t=q.top()/2;
q.pop();
q.push(t);
m--;
}
while(!q.empty())
{
sum+=q.top();
q.pop();
}
cout<<sum<<endl;
}
return 0;
}Problem Statement
There are N squares arranged in a row from left to right.
The height of the i-th square from the left is H**i.
You will land on a square of your choice, then repeat moving to the adjacent square on the right as long as the height of the next square is not greater than that of the current square.
Find the maximum number of times you can move.
Constraints
Input
Input is given from Standard Input in the following format:
N
H1 H2 … HNOutput
Print the maximum number of times you can move.
Sample Input 1
5
10 4 8 7 3Sample Output 1
2By landing on the third square from the left, you can move to the right twice.
Sample Input 2
7
4 4 5 6 6 5 5Sample Output 2
3By landing on the fourth square from the left, you can move to the right three times.
Sample Input 3
4
1 2 3 4Sample Output 3
0AC
#include<iostream>
using namespace std;
int a[100000009];
int main()
{
int t;
while(cin>>t)
{
int con=0,num=0;
for(int i=1;i<=t;++i)
{
cin>>a[i];
}
int k = a[1];
for(int i =2;i<=t;++i)
{
if(k>=a[i])
{
num++;
k=a[i];
}
if(k<a[i])
{
con = max(num,con);
num = 0;
k = a[i];
}
}
con = max(con,num);
cout<<con<<endl;
}
}Problem Statement
Snuke has N strings. The i-th string is s**i.
Let us concatenate these strings into one string after arranging them in some order. Find the maximum possible number of occurrences of AB in the resulting string.
Constraints
Input
Input is given from Standard Input in the following format:
N
s1
\vdots
s_NOutput
Print the answer.
Sample Input 1
3
ABCA
XBAZ
BADSample Output 1
2For example, if we concatenate ABCA, BAD and XBAZ in this order, the resulting string ABCABADXBAZ has two occurrences of AB.
Sample Input 2
9
BEWPVCRWH
ZZNQYIJX
BAVREA
PA
HJMYITEOX
BCJHMRMNK
BP
QVFABZ
PRGKSPUNASample Output 2
4Sample Input 3
7
RABYBBE
JOZ
BMHQUVA
BPA
ISU
MCMABAOBHZ
SZMEHMASample Output 3
4AC
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
char a[1100];
int main()
{
long long int t;
while(cin>>t)
{
int head=0,tail=0,mid=0,con=0,ans=0;
for(int i=0;i<t;++i)
{
scanf("%s",&a);
int len = strlen(a);
if(a[0]=='B'&&a[len-1]=='A')
con++;
if(a[0]=='B'&&a[len-1]!='A')
head++;
if(a[len-1]=='A'&&a[0]!='B')
tail++;
for(int j =1;j<len-1;++j)
{
if(a[j]=='B'&&a[j-1]=='A')
ans++;
}
}
int h=0,t=0;
if(con)
{
ans = ans+con-1;
if(head)
t = 1;
if(tail)
h = 1;
}
head = head+h;
tail = tail+t;
ans = ans+min(head,tail);
cout<<ans<<endl;
}
return 0;
}