# CSP2021 J2/S2 reference analysis

## CSP2021 J2

Topic transmission

### P7909 [CSP-J 2021] candy

Analysis: this question can be read directly, but it should be noted that the output result of the question is the maximum value you can get at a single time.

Method 1: violent enumeration, directly enumerate [L, R], compare with the existing maximum value, and take the maximum value each time.
However, the data range required by the topic is 1e9, and the sequence that the computer can execute in one second is about 1e6~1e7, 1e8, and 1e9 will explode. Therefore, this method cannot get full marks.

Method 2: mathematical thinking, if each child can have at least two choices, then the maximum value is n-1;
Otherwise (that is, when the child has only 0 or 1 chance to choose), the maximum value is R%n.

Click to view the code
```#include<bits/stdc++.h>
using namespace std;

int main(){
freopen("candy.in", "r", stdin);
freopen("candy.out", "w", stdout);

int n,l,r; cin>>n>>l>>r;
int ans=0;
if(l/n < r/n) ans=n-1;
else ans=r%n;
cout<<ans;

fclose(stdin);
fclose(stdout);
return 0;
}

int main_70(){
freopen("candy.in", "r", stdin);
freopen("candy.out", "w", stdout);

int n,l,r; cin>>n>>l>>r;
int ans=0;
for(int k=l; k<=r; k++){
int num=k%n;
if(ans<num) ans=num;
}
cout<<ans;

fclose(stdin);
fclose(stdout);
return 0;
}
```

### P7910 [CSP-J 2021] insert sort

Analysis: the topic is long. After reading it patiently, it seems that it is not difficult to find the topic, but once you look at the data points, you can play it directly. However, you should calm down and get the highest score.

Method 1: pure simulation, without thinking about anything, simulate it according to the topic. Of course, this can only get 36 ~ 50 points.
Method 2: counting by buckets without simulated exchange. Count the number of elements l less than a[x] on the left of a[x] and the number of elements r greater than a[x] on the right of a[x]. It is found that the sorted position of a[x] is x+r-l, but this method will also timeout. After all, each number needs to be checked once, and about 70 ~ 80 points can be obtained.
Method 3:

36 ~ 50 points code

Click to view the code
```#include<bits/stdc++.h>
using namespace std;

const int N=1e4;
struct T{
int id, value;
}a[N], b[N];

void insertSort(T arr[], int n){
for(int i=1; i<=n; i++){
for(int j=i; j>=2; j--){
if(arr[j].value < arr[j-1].value){
T t = arr[j-1];
arr[j-1] = arr[j];
arr[j] = t;
}
}
}
}

int find(T arr[], int n, T x){
for(int i=1; i<=n; i++){
if(arr[i].value==x.value && arr[i].id==x.id)
return i;
}
return -1;
}

bool cmp(T a, T b){
return a.value<b.value;
}
int main_50(){
freopen("sort.in", "r", stdin);
freopen("sort.out", "w", stdout);

int n,q; scanf("%d%d", &n, &q);
for(int i=1; i<=n; i++){
scanf("%d", &a[i].value); a[i].id=i;
}
for(int i=1; i<=q; i++){
int f,x,v; scanf("%d%d", &f, &x);
if(f==1){
scanf("%d", &v); a[x].value=v;
}else if(f==2){
for(int i=1; i<=n; i++)    b[i]=a[i];
insertSort(b, n);
cout<<find(b, n, a[x])<<endl;
}
}

fclose(stdin);
fclose(stdout);
return 0;
}
```

70 ~ 80 points code

Click to view the code
```#include<bits/stdc++.h>
using namespace std;

const int N=1e4;
int a[N];
int main(){
freopen("sort.in", "r", stdin);
freopen("sort.out", "w", stdout);
int n,q; cin>>n>>q;
for(int i=1; i<=n; i++) cin>>a[i];
for(int i=1; i<=q; i++){
int f,x,v; cin>>f>>x;
if(f==1){
cin>>v; a[x]=v;
}else{ //query
int l=0,r=0;
for(int i=1; i<x; i++)    if(a[i]>a[x]) l++;
for(int i=x+1; i<=n; i++) if(a[i]<a[x]) r++;
cout<<x-l+r<<endl;
}
}

fclose(stdin);
fclose(stdout);
return 0;
}
```

### P7911 [CSP-J 2021] network connection

Analysis: for a large simulation, the problem is not difficult, but it tests the amount of code. This problem needs to be read carefully. Then you will find a very interesting thing. 50% of the data meet property 1. What is property 1? That is to say, ip is legal, so the problem comes. What is the most difficult problem? It's just to judge that the ip address is legal. I assume that the ip address is legal. If I start directly, I can get 50 points. Isn't it fragrant?

The 50 point code is as follows

Click to view the code
```#include<bits/stdc++.h>
using namespace std;

map<string, int> m;

//Check ip validity
bool check(string s){
return 1;
}

int main(){
freopen("network.in", "r", stdin);
freopen("network.out", "w", stdout);

int n; cin>>n;
for(int i=1; i<=n; i++){
string s1,s2; cin>>s1>>s2;
if(!check(s1)){
cout<<"ERR"<<endl;
}else{
if(s1=="Server"){
if(m[s2]==0){
cout<<"OK"<<endl;
m[s2]=i;
}else{
cout<<"FAIL"<<endl;
}
}else{
if(m[s2]==0) cout<<"FAIL"<<endl;
else cout<<m[s2]<<endl;
}
}
}
fclose(stdin);
fclose(stdout);
return 0;
}
```

## CSP2021 S2

Topic transmission

### P7916 [CSP-S 2021] traffic planning

Posted by dustinnoe on Wed, 03 Nov 2021 01:19:29 -0700