F - Reports
Meaning:
t group of samples, each sample gives you an n, and the next n numbers only contain 0 and 1 (0 means leaving school and 1 means leaving school)
You need to judge whether there is a recording error in these n actions.
Idea:
Out of school and out of school must appear next to each other, that is, when there are two identical actions, there will be errors.
code:
#include<iostream> using namespace std; int a[100]; int main() { ios::sync_with_stdio(false); int t; cin>>t; while(t--) { int n; cin>>n; bool flag=true; for(int i=1;i<=n;i++) cin>>a[i]; for(int i=1;i<n;i++) { if(a[i]==a[i+1]) { flag=false; break; } } if(flag) cout<<"YES"<<endl; else cout<<"NO"<<endl; } }
B - Express Mail Taking
Meaning:
t sets of samples. Each sample gives you an n, m, k, and then each number.
n represents a 1-n post office, m represents the number of mail you want to send, and the next m numbers are the corresponding post office addresses, and k does not appear in these addresses. k represents a special post office. Before you send mail, you should go to this post office first, and then send mail. You need to ask for the minimum distance to send all mail. (the initial point starts from 1 and finally returns to 1)
Idea:
Greedy simulation, when you list all cases where ai is greater than or less than k, you will find that initialization must be performed when you go back to 1 from 1 to K
2 * (k-1). After sort ing from small to large, if all ai is greater than k, i.e. go twice from each position to K, and finally from K to 1 (this is already in 2 * (k-1)), if all ai is less than k, go twice from each position to K
Except for the smallest a0, because from K to a1, back to K. This is twice from a1 to K, and then you go from K to a0, and then from a0 to 1, that is, k-1 (this is already in 2 * (k-1), so if a0 does not need to be calculated.
Finally, initialize the required distance to 2 * (k-1), calculate twice the distance from all positions to K, and judge whether a0 is less than K. if it is less than k, it is equivalent to that you have calculated more distance from a0 to K, subtract it, and it is not less than K. normal output can be obtained.
(note that it is useless to use scanf instead of cin to turn off ios)
code:
#include<iostream> #include<cstdio> #include<algorithm> using namespace std; int a[1000005]; int main() { int t; scanf("%d",&t); while(t--) { long long n,m,k,ans; scanf("%lld%lld%lld",&n,&m,&k); for(int i=0; i<m; i++) scanf("%d",&a[i]); sort(a,a+m); ans=2*(k-1); for(int i=0; i<m; i++) { ans+=2*abs(k-a[i]); } if(a[0]<k) { ans-=2*(k-a[0]); } printf("%lld\n",ans); } }
E - CCPC Training Class
Meaning:
Xiaohuo is helping his CCPC coach prepare a new CCPC training competition. He wants to generate high-quality data about the side chain tree (he doesn't need to know what the side chain tree is), but he has encountered some problems. Can you help him?
In this paragraph, we formally define the data quality problem. For some strings s=s1s2s3 ··· sn, we use s [l: r] to represent the substring from L to r. if l > r, s [l: r] is empty. We further define:
Represents the longest edge at the ith position, then define D(i) to represent the edge chain length at the ith position:
The quality W of the string is defined as the maximum value of D:
In order to prevent the boundary tree from being distinguished by a simple brute force algorithm, a small volcano needs to generate a string to make its quality W as large as possible. Now, given a string s, you can arrange the string arbitrarily. Can you calculate maximum mass that can be achieved by arranging strings?
Idea:
After summarizing and looking at the sample, it is found that the output times with the most characters in this string can be found. Traverse and record the number of character occurrences. Just find the maximum output. (note that the output cin and printf should not be used together. The output will explode.)
code:
#include<iostream> #include<cstdio> using namespace std; int num[30]; int main() { ios::sync_with_stdio(false); int t; cin>>t; for(int cnt=1;cnt<=t;cnt++) { string s; cin>>s; int x; int maxx=-1; for(int i=0;i<s.size();i++) { x=s[i]-'a'; num[x]+=1; } for(int i=0;i<26;i++) { maxx=max(maxx,num[i]); num[i]=0; } cout<<"Case #"<<cnt<<": "<<maxx<<endl; } }
G - 3x3 Convolution
Meaning:
There is an n x n matrix A and a 3 x 3 matrix K, both of which are nonnegative matrices, and K is a fractional matrix with a sum of 1. Define an operation. For matrix A, use matrix K to convolute each element in a ----- the upper left corner of K traverses each element in a, and each time covers the matrix within the range of 3 x 3 in a (if the range is not enough, add enough zeros at the edge of a) for weighted summation under the action of K. Now fix the matrix K, take the C doll infinite times, and find the final matrix.
Idea:
Judge whether K[1][1] of matrix K is 1; If yes, output the original matrix A, otherwise output the 0 matrix;
First, K is A decimal matrix, A is an integer matrix, and the answer is an integer matrix; So C11 must be an integer.
Equivalent to A 3*3 This matrix and K matrix are multiplied infinite times; If C11 is an integer, K11 must be an integer, because K matrix decimal matrix and sum is 1, then K11=1.
The band diagram is explained here
code:
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N=55; int a[55][55],k[4][4]; int main() { int t; scanf("%d",&t); while(t--) { int n; scanf("%d",&n); for(int i=1; i<=n; i++) for(int j=1; j<=n; j++) { scanf("%d",&a[i][j]); } int sum=0; for(int i=1; i<=3; i++) for(int j=1; j<=3; j++) { scanf("%d",&k[i][j]); sum+=k[i][j]; } int temp=1; if(sum/k[1][1]!=1) temp=0; for(int i=1; i<=n; i++) for(int j=1; j<=n; j++) printf("%d%c",a[i][j]*temp,j==n?'\n':' '); } }