2020-10-26训练赛错题集
A
Problem Statement
Given are strings S and T. Consider changing S to T by repeating the operation below. Find the minimum number of operations required to do so.
Operation: Choose one character of S and replace it with a different character.
Constraints
- S and T have lengths between 11 and 2×10^5 (inclusive).
- S and T consists of lowercase English letters.
- S and T have equal lengths.
Input
Input is given from Standard Input in the following format:
S T
Output
Print the answer.
Sample Input 1 Copy
cupofcoffee cupofhottea
Sample Output 1 Copy
4
We can achieve the objective in four operations, such as the following:
- First, replace the sixth character
cwithh. - Second, replace the eighth character
fwitht. - Third, replace the ninth character
fwitht. - Fourth, replace the eleventh character
ewitha.
Sample Input 2 Copy
abcde bcdea
Sample Output 2 Copy
5
Sample Input 3 Copy
apple apple
Sample Output 3 Copy
0
No operations may be needed to achieve the objective.
即看有几个不一样的字符。
#include<bits/stdc++.h> using namespace std; int cnt; int main() { string a,b; int cnt=0; cin>>a>>b; for(int i=0;i<a.size();i++) { if(a[i]!=b[i]) cnt++; } cout<<cnt; return 0; }
B
Problem Statement
We have two desks: A and B. Desk A has a vertical stack of N books on it, and Desk B similarly has M books on it.
It takes us Ai minutes to read the i-th book from the top on Desk A (1≤i≤N) and Bi minutes to read the i-th book from the top on Desk B (1≤i≤M).
Consider the following action:
- Choose a desk with a book remaining, read the topmost book on that desk, and remove it from the desk.
How many books can we read at most by repeating this action so that it takes us at most K minutes in total? We ignore the time it takes to do anything other than reading.
Constraints
- 1≤N,M≤200000
- 1≤K≤10^9
- 1≤Ai,Bi≤10^9
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M K A1 A2 …… AN B1 B2 …… BM
Output
Print an integer representing the maximum number of books that can be read.
Sample Input 1
3 4 240 60 90 120 80 150 80 150
Sample Output 1
3
In this case, it takes us 60, 90, 120 minutes to read the 1-st, 2-nd, 3-rd books from the top on Desk A, and 80, 150, 80, 150 minutes to read the 1-st, 2-nd, 3-rd, 4-th books from the top on Desk B, respectively.
We can read three books in 230 minutes, as shown below, and this is the maximum number of books we can read within 240 minutes.
- Read the topmost book on Desk A in 60 minutes, and remove that book from the desk.
- Read the topmost book on Desk B in 80 minutes, and remove that book from the desk.
- Read the topmost book on Desk A in 90 minutes, and remove that book from the desk.
Sample Input 2
3 4 730 60 90 120 80 150 80 150
Sample Output 2
7
Sample Input 3
5 4 1 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
Sample Output 3
0
Watch out for integer overflows.
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N=200010; ll a[N],b[N]; int n,m; ll k; int main() { ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); int res=0; cin>>n>>m>>k; for(int i=1;i<=n;i++) { cin>>a[i]; a[i]+=a[i-1]; } for(int i=1;i<=m;i++) { cin>>b[i]; b[i]+=b[i-1]; } for(int i=0,j=m;i<=n;i++)//这里i一定要从0开始循环要不然会漏掉不在a数组中选数的情况 { while(j&&a[i]>k-b[j]) j--; if(a[i]<=k-b[j]) res=max(res,i+j); } cout<<res<<endl; return 0; }
C
Problem StatementFor a positive integer X, let f(X) be the number of positive divisors of X.
Given a positive integer N, find ∑NK=1K×f(K).
Constraints- 1≤N≤107
Input is given from Standard Input in the following format:
N
Output
Print the value ∑NK=1K×f(K).
Sample Input 14Sample Output 1
23
We have f(1)=1, f(2)=2, f(3)=2, and f(4)=3, so the answer is 1×1+2×2+3×2+4×3=23.
Sample Input 2100Sample Output 2
26879Sample Input 3
10000000Sample Output 3
838627288460105
Watch out for overflows.
#include<iostream> #include<algorithm> #include<cstdio> using namespace std; typedef long long ll; int n; int main() { ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); cin>>n; ll res=0; for(int i=1;i<=n;i++) for(int j=i;j<=n;j+=i) res+=j;//相当于乘k cout<<res<<endl; return 0; }
整理一个找约数个数的函数
int n; ll d[maxn], minn[maxn], prime[maxn]; bool is[maxn]; int tot; void init() { d[1] = 1; for (int i = 2; i <= n; i++) { if (is[i] == 0) prime[++tot] = i, d[i] = 2, minn[i] = 1; for (re int j = 1; prime[j] * i <= n && j <= tot; j++) { is[i * prime[j]] = 1; if (i % prime[j] == 0) { minn[i * prime[j]] = minn[i] + 1; d[i * prime[j]] = d[i] / (minn[i] + 1) * (minn[prime[j] * i] + 1); break; } minn[i * prime[j]] = 1; d[i * prime[j]] = d[i] * 2; } } } int main() { scanf("%d", &n); init(); ll res = 0; for (int i = 1; i <= n; i++) { res += i * d[i]; } printf("%lld", res); }
D
Count the pairs of length-N sequences consisting of integers between 1 and M (inclusive), A1,A2,⋯,AN and B1,B2,⋯,BN, that satisfy all of the following conditions:
- Ai≠Bi, for every i such that 1≤i≤N.
- Ai≠Aj and Bi≠Bj, for every (i,j) such that 1≤i<j≤N.
Since the count can be enormous, print it modulo (109+7).
Constraints- 1≤N≤M≤5×105
- All values in input are integers.
Input is given from Standard Input in the following format:
N MOutput
Print the count modulo (109+7).
Sample Input 12 2Sample Output 1
2
A1=1,A2=2,B1=2,B2=1 and A1=2,A2=1,B1=1,B2=2 satisfy the conditions.
Sample Input 22 3Sample Output 2
18Sample Input 3
141421 356237Sample Output 3
881613484

E
Problem Statement
There are N piles of stones. The i-th pile has Ai stones.
Aoki and Takahashi are about to use them to play the following game:
- Starting with Aoki, the two players alternately do the following operation:
- Operation: Choose one pile of stones, and remove one or more stones from it.
- When a player is unable to do the operation, he loses, and the other player wins.
When the two players play optimally, there are two possibilities in this game: the player who moves first always wins, or the player who moves second always wins, only depending on the initial number of stones in each pile.
In such a situation, Takahashi, the second player to act, is trying to guarantee his win by moving at least zero and at most (A1−1) stones from the 1-st pile to the 2-nd pile before the game begins.
If this is possible, print the minimum number of stones to move to guarantee his victory; otherwise, print -1 instead.
Constraints
- 2≤N≤300
- 1≤Ai≤10^12
Input
Input is given from Standard Input in the following format:
N A1 …… AN
Output
Print the minimum number of stones to move to guarantee Takahashi's win; otherwise, print -1 instead.
Sample Input 1
2 5 3
Sample Output 1
1
Without moving stones, if Aoki first removes 2 stones from the 1-st pile, Takahashi cannot win in any way.
If Takahashi moves 1 stone from the 1-st pile to the 2-nd before the game begins so that both piles have 4 stones, Takahashi can always win by properly choosing his actions.
Sample Input 2
2 3 5
Sample Output 2
-1
It is not allowed to move stones from the 2-nd pile to the 1-st.
Sample Input 3
3 1 1 2
Sample Output 3
-1
It is not allowed to move all stones from the 1-st pile.
Sample Input 4
8 10 9 8 7 6 5 4 3
Sample Output 4
3
Sample Input 5
3 4294967297 8589934593 12884901890
Sample Output 5
1
Watch out for overflows.
咕咕咕
I
Problem Statement
Takahashi loves the number 7 and multiples of K.
Where is the first occurrence of a multiple of K in the sequence 7,77,777,…7,77,777,…? (Also see Output and Sample Input/Output below.)
If the sequence contains no multiples of K, print -1 instead.
Constraints
- 1≤K≤10^6
- K is an integer.
Input
Input is given from Standard Input in the following format:
K
Output
Print an integer representing the position of the first occurrence of a multiple of K. (For example, if the first occurrence is the fourth element of the sequence, print 4.)
Sample Input 1
101
Sample Output 1
4
None of 7, 77, and 777 is a multiple of 101, but 7777 is.
Sample Input 2
2
Sample Output 2
-1
All elements in the sequence are odd numbers; there are no multiples of 2.
Sample Input 3
999983
Sample Output 3
999982
#include <cstdio>
int k;
bool v[1000010];
int main()
{
scanf("%d",&k);
int x=7%k;
for(int i=1;;i++){
if(v[x])
{
printf("-1");
return 0;
}
if(!x)
{
printf("%d",i);
return 0;
}
v[x]=true;
x=x*10+7;
x%=k;
}
}
Problem Statement
An altar enshrines N stones arranged in a row from left to right. The color of the i-th stone from the left (1≤i≤N) is given to you as a character ci; R stands for red and W stands for white.
You can do the following two kinds of operations any number of times in any order:
- Choose two stones (not necessarily adjacent) and swap them.
- Choose one stone and change its color (from red to white and vice versa).
According to a fortune-teller, a white stone placed to the immediate left of a red stone will bring a disaster. At least how many operations are needed to reach a situation without such a white stone?
Constraints
- 2≤N≤200000
- cici is
RorW.
Input
Input is given from Standard Input in the following format:
N c1c2...cN
Output
Print an integer representing the minimum number of operations needed.
Sample Input 1
4 WWRR
Sample Output 1
2
For example, the two operations below will achieve the objective.
- Swap the 1-st and 3-rd stones from the left, resulting in
RWWR. - Change the color of the 4-th stone from the left, resulting in
RWWW.
Sample Input 2
2 RR
Sample Output 2
0
It can be the case that no operation is needed.
Sample Input 3
8 WRWWRWRR
Sample Output 3
3
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
int n;
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>n;
string a;
cin>>a;
ll num1=0,num2=0;
for(int i=0;i<n;i++){
if(a[i]=='R') num1++;
}
for(int i=num1;i<n;i++){
if(a[i]=='R') num2++;
}cout<<num2<<endl;
return 0;
}
K
Problem Statement
We have N logs of lengths A1,A2,⋯AN
We can cut these logs at most K times in total. When a log of length L is cut at a point whose distance from an end of the log is t(0<t<L), it becomes two logs of lengths t and L−t
Find the shortest possible length of the longest log after at most K cuts, and print it after rounding up to an integer.
Constraints
- 1≤N≤2×10^5
- 0≤K≤10^9
- 1≤Ai≤10^9
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K A1 A2 ⋯⋯ AN
Output
Print an integer representing the answer.
Sample Input 1
2 3 7 9
Sample Output 1
4
- First, we will cut the log of length 7 at a point whose distance from an end of the log is 3.5, resulting in two logs of length 3.5 each.
- Next, we will cut the log of length 9 at a point whose distance from an end of the log is 3, resulting in two logs of length 3 and 6.
- Lastly, we will cut the log of length 6 at a point whose distance from an end of the log is 3.3, resulting in two logs of length 3.3 and 2.7.
In this case, the longest length of a log will be 3.5, which is the shortest possible result. After rounding up to an integer, the output should be 4.
Sample Input 2
3 0 3 4 5
Sample Output 2
5
Sample Input 3
10 10 158260522 877914575 602436426 24979445 861648772 623690081 433933447 476190629 262703497 211047202
Sample Output 3
292638192
#include <cstdio> #define maxn 200010 #define ll long long int n,a[maxn];ll m; ll check(int x){ ll re=0; for(int i=1;i<=n;i++)re+=a[i]/x+(a[i]%x>0)-1; return re; } int main() { scanf("%d %lld",&n,&m); for(int i=1;i<=n;i++)scanf("%d",&a[i]); int l=1,r=1e9,mid,ans; while(l<=r){ mid=l+r>>1; if(check(mid)<=m)ans=mid,r=mid-1; else l=mid+1; } printf("%d",ans); }
莫队板子
https://gene-liu.blog.csdn.net/article/details/100556943?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.channel_param

浙公网安备 33010602011771号