B - Consecutive Integers
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
All values in input are integers.
1≤K≤N≤50
Input
Input is given from Standard Input in the following format:
N K
Output
Print the answer.
Sample Input 1
3 2
Sample Output 1
2
There are two ways to choose two consecutive integers: (1,2) and (2,3).
Sample Input 2
13 3
Sample Output 2
11
题意:就是从1到n这个区间里边选取长度为k的区间,问能选取多少个。
题解:如果n==k结果是1否则结果就是n-k+1
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,m;
cin>>n>>m;
if(n==m)
{
cout<<1<<endl;
}
else
{
cout<<n-m+1<<endl;
}
}
C - ModSum
For an integer N, we will choose a permutation {P1,P2,…,PN} of {1,2,…,N}.
Then, for each i=1,2,…,N, let Mi be the remainder when i is divided by Pi.
Find the maximum possible value of M1+M2+[cdots]+MN.
Constraints
N is an integer satisfying 1≤N≤109.
Input
Input is given from Standard Input in the following format:
N
Output
Print the maximum possible value of M1+M2+[cdots]+MN.
Sample Input 1
2mutation {P1,P2}={2,1} is chosen, M1+M2=1+0=1.
Sample Input 2
13
Sample Output 2
78
Sample Output 1
1
When the per
Sample Input 3
1
Sample Output 3
0
题意:给你一个序列从1到N然后再生成一个序列是随机的序列一一对应,然后求第一个序列除以第二个序列求余数,然后求余数相加的最大值是多少。
题解:最优的排序就是1对应2结果是1 2对应3 结果是2 到最后的N对应1结果是0 因此合适n*(n-1)/2
#include <bits/stdc++.h>
using namespace std;
int main()
{
long long n;
cin>>n;
cout<<n*(n-1)/2<<endl;
}
F - Monsters Battle Royale
There are N monsters, numbered 1,2,…,N.
Initially, the health of Monster i is Ai.
Below, a monster with at least 1 health is called alive.
Until there is only one alive monster, the following is repeated:
- A random alive monster attacks another random alive monster.
- As a result, the health of the monster attacked is reduced by the amount equal to the current health of the monster attacking.
Find the minimum possible final health of the last monster alive.
Constraints
- All values in input are integers.
- 2≤N≤105
- 1≤Ai≤109
Input
Input is given from Standard Input in the following format:
N
A1 A2 … AN
Output
Print the minimum possible final health of the last monster alive.
Sample Input 1
4
2 10 8 40
Sample Output 1
2
When 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 1000000000
Sample Output 2
1
Sample Input 3
3
1000000000 1000000000 1000000000
Sample Output 3
1000000000
题意:就是每次可以有一个小怪兽进行攻击,攻击的那只怪兽掉血且掉血的血量为这只攻击的怪兽
题解:就是选取最小偶数,然后进行循环,使所有的数对这个偶数进行取余,知道所有数对这个数求余数都为0,那么这个数就是最后的结果。
#include <bits/stdc++.h>
using namespace std;
long long a[100000+10];
int main()
{
int n;
cin>>n;
long long flag=0,flag1=0,sum=1000000000,k,sum1;
for(long long i=0;i<n;i++)
{
cin>>a[i];
if(a[i]%2==0)
{
flag=1;
sum=min(sum,a[i]);
}
else
{
flag1=1;
sum1=min(sum1,a[i]);
}
}
long long aa=0;
while(aa!=n)
{
aa=0;
for(int i=0;i<n;i++)
{
if(a[i]%sum!=0)
{
k=a[i]%sum;
sum=k;
}
else
aa++;
}
}
cout<<sum<<endl;
}
Takahashi is going to buy N items one by one.
The price of the i-th item he buys is Ai 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 |
2Y |
(rounded down to the nearest integer) yen.
What is the minimum amount of money required to buy all the items?
Constraints
- All values in input are integers.
- 1≤N,M≤105
- 1≤Ai≤109
Input
Input is given from Standard Input in the following format:
N M
A1 A2 … AN
Output
Print the minimum amount of money required to buy all the items.
Sample Input 1
3 3
2 13 8
Sample Output 1
9
We can buy all the items for 9 yen, as follows:
- Buy the 1-st item for 2 yen without tickets.
- Buy the 2-nd item for 3 yen with 2 tickets.
- Buy the 3-rd item for 4 yen with 1 ticket.
Sample Input 2
4 4
1 9 3 5
Sample Output 2
6
Sample Input 3
1 100000
1000000000
Sample Output 3
0
We 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 1000000000
Sample Output 4
9500000000
题意:去买商品,有n个商品然后n个商品有不同的价值,有m张折扣卷,买商品可以用任意张折扣卷,然后用的张数跟价格的函数关系是价值/2的m次方,求如何花的钱最少
题解:我开始的想法是没错的,然后就是很憨批没有用优先队列我用数组然后最大的除以2让票的利用最大化,每次取出来最大的值然后除以2.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
priority_queue <int,vector<int>,less<int> >q;
ll n,m;
cin>>n>>m;
for(int i=0;i<n;i++)
{
ll x;
cin>>x;
q.push(x);
}
for(int i=0;i<m;i++)
{
ll a=q.top()/2;
q.pop();
q.push(a);
}
ll sum=0;
while(!q.empty())
{
sum+=q.top();
q.pop();
}
cout<<sum<<endl;
}
Takahashi has decided to hold fastest-finger-fast quiz games. Kizahashi, who is in charge of making the scoreboard, is struggling to write the program that manages the players' scores in a game, which proceeds as follows.
A game is played by N players, numbered 1 to N. At the beginning of a game, each player has K points.
When a player correctly answers a question, each of the other N−1 players receives minus one (−1) point. There is no other factor that affects the players' scores.
At the end of a game, the players with 0 points or lower are eliminated, and the remaining players survive.
In the last game, the players gave a total of Q correct answers, the i-th of which was given by Player Ai. For Kizahashi, write a program that determines whether each of the N players survived this game.
Constraints
- All values in input are integers.
- 2≤N≤105
- 1≤K≤109
- 1≤Q≤105
- 1≤Ai≤N (1≤i≤Q)
Input
Input is given from Standard Input in the following format:
N K Q
A1
A2
.
.
.
AQ
Output
Print N lines. The i-th line should contain Yes
if Player i survived the game, and No
otherwise.
Sample Input 1
6 3 4
3
1
3
2
Sample Output 1
No
No
Yes
No
No
No
In the beginning, the players' scores are (3,3,3,3,3,3).
- Player 3 correctly answers a question. The players' scores are now (2,2,3,2,2,2).
- Player 1 correctly answers a question. The players' scores are now (2,1,2,1,1,1).
- Player 3 correctly answers a question. The players' scores are now (1,0,2,0,0,0).
- Player 2 correctly answers a question. The players' scores are now (0,0,1,−1,−1,−1).
Players 1,2,4,5 and 6, who have 0 points or lower, are eliminated, and Player 3 survives this game.
Sample Input 2
6 5 4
3
1
3
2
Sample Output 2
Yes
Yes
Yes
Yes
Yes
Yes
Sample Input 3
10 13 15
3
1
4
1
5
9
2
6
5
3
5
8
9
7
9
Sample Output 3
No
No
No
No
Yes
No
No
No
Yes
No
题意:就是每个人都有初始的分数K,然后有q的问题每个问题有一个人可以不扣分其余的人全部扣分,输出每个人的结果,如果分数小于等于0那么输出No否则输出Yes
题解:开两个数组,一个是记录人的分数,一个是记录每个人能加分。假设全部都减去Q然后加上第二个数组的数值就行了。
小于等于0输出No否则输出Yes
#include <bits/stdc++.h>
using namespace std;
long long a[100000+10];
long long aa[100000+10];
int main()
{
int n,k,q;
cin>>n>>k>>q;
for(int i=1;i<=n;i++)
{
a[i]=k;
}
for(int i=1;i<=q;i++)
{
int m;
cin>>m;
aa[m]++;
}
for(int i=1;i<=n;i++)
{
if((a[i]-q+aa[i])>0)
{
cout<<"Yes"<<endl;
}
else
{
cout<<"No"<<endl;
}
}
}
There are N squares arranged in a row from left to right.
The height of the i-th square from the left is Hi.
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
- All values in input are integers.
- 1≤N≤105
- 1≤Hi≤109
Input
Input is given from Standard Input in the following format:
N
H1 H2 … HN
Output
Print the maximum number of times you can move.
Sample Input 1
5
10 4 8 7 3
Sample Output 1
2
By 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 5
Sample Output 2
3
By landing on the fourth square from the left, you can move to the right three times.
Sample Input 3
4
1 2 3 4
Sample Output 3
0
We have a sequence of N integers A~=~A0,~A1,~…,~AN−1.
Let B be a sequence of K×N integers obtained by concatenating K copies of A. For example, if A~=~1,~3,~2 and K~=~2, B~=~1,~3,~2,~1,~3,~2.
Find the inversion number of B, modulo 109+7.
Here the inversion number of B is defined as the number of ordered pairs of integers (i,~j)~(0≤i<j≤K×N−1) such that Bi>Bj.
Constraints
- All values in input are integers.
- 1≤N≤2000
- 1≤K≤109
- 1≤Ai≤2000
Input
Input is given from Standard Input in the following format:
N K
A0 A1 … AN−1
Output
Print the inversion number of B, modulo 109+7.
Sample Input 1
2 2
2 1
Sample Output 1
3
In this case, B~=~2,~1,~2,~1. We have:
- B0>B1
- B0>B3
- B2>B3
Thus, the inversion number of B is 3.
Sample Input 2
3 5
1 1 1
Sample Output 2
0
A may contain multiple occurrences of the same number.
Sample Input 3
10 998244353
10 9 8 7 5 6 3 4 2 1
Sample Output 3
185297239
Be sure to print the output modulo 109+7.
题意:就是给你一组一个串,然后复制K次,求每个数比这个数后边的数大的次数。
题解:就是先便利这个串的每个数,求这个数的之前比这个数小的和以及这个数之后比这个数小的和,然后之前的数的合乘以
(k-1)*k/2之后的和乘以(k+1)*k/2,然后这一题要把这个(k-1)*k/2这个数对1e9+7求余,要不然会13组的时候WA别问我咋知道的。
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll x=1e9+7;
ll a[20000],bq[20000],bl[20000];
int main()
{
ll n,k;
cin>>n>>k;
for(ll i=0;i<n;i++)
{
cin>>a[i];
}
sizeof(bq,0,sizeof(bq));
sizeof(bl,0,sizeof(bl));
for(int i=0;i<n;i++)
{
for(ll j=0;j<n;j++)
{
if(a[j]<a[i]&&i>j)
bq[i]++;
else if(a[j]<a[i]&&i<j)
bl[i]++;
}
}
ll sum1=(k-1)*k/2;
sum1=sum1%x;
long long sum3=0;
for(ll i=0;i<n;i++)
{
sum3+=bq[i]*sum1;
sum3=sum3%x;
sum3+=bl[i]*(sum1+k);
sum3=sum3%x;
}
cout<<sum3%x<<endl;
}
Snuke has N strings. The i-th string is si.
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
- 1≤N≤104
- 2≤|si|≤10
- si consists of uppercase English letters.
Input
Input is given from Standard Input in the following format:
N
s1
\vdots
s_N
Output
Print the answer.
Sample Input 1
3
ABCA
XBAZ
BAD
Sample Output 1
2
For 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
PRGKSPUNA
Sample Output 2
4
Sample Input 3
7
RABYBBE
JOZ
BMHQUVA
BPA
ISU
MCMABAOBHZ
SZMEHMA
Sample Output 3
4
题意:就是在给定的这几个串,然后随意拼接组成新的串里边求AB的最大的可能值。
题解:要记录第一个字符是B的串数量a1以及末尾是A的串数量a2,然后记录第一个字符是B且末尾字符是A的串的数量a3。
以及串中间出现AB的次数a4;
然后判断只要
a1!=a2!=a3!=0;
的话结果就是min(a1,a2)+a4;
如果a1=a2=a3,那么结果就是a3+a4-1;
代码如下
#include <bits/stdc++.h>
using namespace std;
char a[1000];
int main()
{
long long n,a1=0,a2=0,a3=0,a4=0;
cin>>n;
while(n--)
{
memset(a,'\0',sizeof(a));
cin>>a;
if(a[0]=='B')
{
a1++;
}
if(a[strlen(a)-1]=='A')
{
a2++;
}
if(a[strlen(a)-1]=='A'&&a[0]=='B')
{
a4++;
}
for(int i=0;i<strlen(a)-1;i++)
{
if(a[i]=='A'&&a[i+1]=='B')
{
a3++;
}
}
}
long long aa=min(a1,a2);
if(a1==a4&&a2==a4&&a4!=0)
{
cout<<aa+a3-1<<endl;
}
else
cout<<aa+a3<<endl;
}