2022 Niuke winter vacation algorithm basic training camp 3

I went to visit relatives that day. Later, I vp got away with it. I can only say that I escaped from the real hell (Wuwuwuwu was killed by brother zhinai). 6 in the game and 8 after the game, it was estimated that rank was about 450, so I gave an estimated score

A: Successful, 0

B: Backpack, 6 (1)

C: dp (supplementary)

D: Check in, 2

E: Analog, 175 (6)

G: Data structure (supplementary)

1: Ruler, 107 (5)

50: ST L + simulation, 30

B: Wisdom is buying melons

The essence of this question is a group backpack. There are three operations for each item: choose, don't choose and choose half. In fact, there are three items in a group, 0, the whole and half. As for why I wa1, ha ha, forgot to take the mold. The first two games were all over because of this wa. Woo woo, just don't have a long memory.

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define mod (ll)(1e9+7)
int a[1010];
int dp[1010][1010];
signed main(){
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    for(int i=1;i<=n;i++){
        for(int j=0;j<=m;j++){
            dp[i][j]=dp[i-1][j];
            if(j>=a[i]){
                dp[i][j]+=dp[i-1][j-a[i]];
            }
            if(j>=a[i]/2){
                dp[i][j]+=dp[i-1][j-a[i]/2];
            }
            dp[i][j]%=mod;
        }
        dp[i][a[i]]++;
        dp[i][a[i]/2]++;
    }
    for(int i=1;i<=m;i++){
        cout<<dp[n][i]%mod<<" ";
    }   
}

E: Zhinai's digital building block (easy version)

This problem is a simulation, with a little thinking, which is a little similar to F in the previous field. First, the problem says that the number of any two continuous same color intervals can be exchanged. Then we can sort each continuous same color interval every time we traverse, but pay attention to copy a new one and can't modify it directly on the original array. Why am I wa6? Because I'm fw(, I thought of searching the set at the beginning, but this problem can be modified directly in a circular way, and it doesn't involve iterative modification. Woo woo.

#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0), cin.tie(0)
#define int long long
#define ll long long
#define mod (int)(1e9+7)

int x[100001];
int y[100001];
int z[100001];
int ou=0;
signed main(){
    fast;
    int n,m,k;
    cin>>n>>m>>k;
    for(int i=1;i<=n;i++){
        char a;
        cin>>a;
        x[i]=a-'0';
    }
    for(int i=1;i<=n;i++){
        cin>>y[i];
    }
    int flag=1;
    k++;
    while(k--){
        if(flag!=1){
            int a,b;
            cin>>a>>b;
            for(int i=0;i<=n;i++){
                if(y[i]==a){
                    y[i]=b;
                }
            }
        }
        flag=0;
        memcpy(z,x,sizeof(x));
        int l=1,r=2;
        for(int i=2;i<=n;i++){
            if(y[i]==y[i-1]){
                r++;
                continue;
            }else{
                sort(z+l,z+r,greater<int>());
                l=r;
                r++;
            }
        }
        if(l!=r){
            sort(z+l,z+r,greater<int>());
        }
        ou=0;
        for(int i=1;i<=n;i++){
            ou*=10;
            ou+=z[i];
            ou%=mod;
        }
        ou%=mod;
        cout<<ou<<endl;
    }
}

G: Easy version of zhinai's tree

 

Hey, the thief's problem is actually the balanced rotation of the tree. If you don't understand it, you can go back and learn the data structure. It's a must for acmer entry. Then why didn't you do it?, My comment on this is: I didn't get started fw, mainly because vp didn't want to play in the end. I saw that all the dads in the team got off the plane and had no power. Woo woo.

Idea: since the title says that there must be a solution of K < = 1, there are only the following two cases:

Brother zhinai is a cow (super loud). It's clear at a glance. If you can't understand it, go to station b to listen to brother zhinai's explanation. easy version is not difficult, but I won't make up for hard version. I'll make up for the 12 person question with my head.

#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0), cin.tie(0)
#define int long long
#define ll long long
map<int,int>ol;
map<int,int>ne;
signed main(){
    fast;
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
    	int a,b;
    	cin>>a>>b;
    	ol[a]=i;
    	ol[b]=i;
	}
	for(int i=1;i<=n;i++){
		int a,b;
		cin>>a>>b;
		ne[a]=i;
		ne[b]=i;
	}
    if(ne==ol){
        cout<<0<<endl;
        return 0;
    }
	int cnt=0;
	int ans;
	for(int i=1;i<=n;i++){
        if(ne[i]==0){
            continue;
        }
		if(ol[ne[i]]==i&&ol[i]!=ne[i]){
			ans=i;
			cnt++;
		}
	}
	if(cnt==1){
		cout<<1<<endl;
		cout<<ans<<endl;
	}else{
		if(cnt==0){
			cout<<0<<endl;
			return 0;
		}
		cout<<-1<<endl;
	}
}

1: Zhinai's password

This question is essentially a ruler taking (many people say it's called double pointer, I think it's more convenient to take a ruler?), The characteristic of ruler taking is that I maintain a certain section in a string of numbers, and the answer of this section changes regularly with the change of the left and right endpoints. For example, I don't have to trace the right endpoint back to the first one on the right of the left endpoint every time I move the left endpoint, but I can find a way to directly calculate the answer increment through the law. This question is very typical. Why wa5: tnnd, Read the wrong title. The title says at least three, and I think it must be three. Moreover, it is also right to understand the sample in this way. I don't care about the pot of the sample.....

First of all, the length of my interval must conform to the meaning of the question, so I don't move my left endpoint first and move my right endpoint to the right. Once I find an interval containing at least three symbols, is it still necessary for my right endpoint to move backward? It must not be necessary, because the following interval must meet the condition that it contains at least three symbols. It can be calculated directly. Should my left endpoint move one bit to the right and then pull the right endpoint back? No, every time we move the left endpoint, we check whether the condition of at least three symbols is true. If it is still true, we can still apply the formula to calculate directly. In this way, the right endpoint does not retreat and O(n) is solved.

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long

int fun(int a,int b,int c,int d){
    int cnt=0;
    if(a!=0){
        cnt++;
    }
    if(b!=0){
        cnt++;
    }
    if(c!=0){
        cnt++;
    }
    if(d!=0){
        cnt++;
    }
    return cnt;
}

signed main(){
    int n,l,r;
    cin>>n>>l>>r;
    string a;
    cin>>a;
    int cnt1=0,cnt2=0,cnt3=0,cnt4=0;
    int le=0,re=-1;
    int ans=0;
    for(int i=0;i<a.size();i++){
        if(a[i]>='A'&&a[i]<='Z'){
            cnt1++;
        }else if(a[i]>='a'&&a[i]<='z'){
            cnt2++;
        }else if(a[i]>='0'&&a[i]<='9'){
            cnt3++;
        }else{
            cnt4++;
        }
        re++;
        while(fun(cnt1,cnt2,cnt3,cnt4)>=3&&re-le+1>=l){
            if(fun(cnt1,cnt2,cnt3,cnt4)>=3&&re-le+1>=l&&re-le+1<=r){
                ans+=min(n-re,r-(re-le));
            }
            if(a[le]>='A'&&a[le]<='Z'){
                cnt1--;
            }else if(a[le]>='a'&&a[le]<='z'){
                cnt2--;
            }else if(a[le]>='0'&&a[le]<='9'){
                cnt3--;
            }else{
                cnt4--;
            }
            le++;
        }
    }
    while(re-le+1>=l){
        if(a[le]>='A'&&a[le]<='Z'){
            cnt1--;
        }else if(a[le]>='a'&&a[le]<='z'){
            cnt2--;
        }else if(a[le]>='0'&&a[le]<='9'){
            cnt3--;
        }else{
            cnt4--;
        }
        le++;
        if(fun(cnt1,cnt2,cnt3,cnt4)>=3&&re-le+1>=l&&re-le+1<=r){
            ans+=min(n-re,r-(re-le));
        }
    }
    cout<<ans<<endl;
}

50: Zhinai's database

This question is a long one. In fact, it is to introduce you to the group by statement. Those who have not studied the database may have to understand it for a while. In fact, group by is to spell the following fields together as a whole keyword for statistics, and calculate the number of records corresponding to each keyword. We know that this question is a water question. We can just follow it directly, Putting keywords together is actually to take a vector as the key value, and then use map to count the number. However, string processing may be a little difficult for Xiaobai (to be honest, I first thought of using regular expression...)

#include <bits/stdc++.h>
using namespace std;
#define visit _visit
#define pb push_back
#define int long long
#define ll long long

int x[1010][1010];
map<string,int>pos;
map<vector<int>,int>ans;
signed main(){
    int n,m;
    cin>>n>>m;
    for(int i=0;i<m;i++){
        string a;
        cin>>a;
        pos[a]=i;
    }
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
           cin>>x[i][j];
        }
    }
    getchar();
    string a;
    getline(cin,a);
    a=a.substr(36);
    string tem="";
    vector<int>p;
    for(int i=0;i<a.size();i++){
        if(a[i]==','||a[i]==';'){
            p.pb(pos[tem]);
            tem="";
        }else{
            tem+=a[i];
        }
    }
    vector<int>t;
    for(int i=0;i<n;i++){
        t.clear();
        for(int j=0;j<p.size();j++){
            t.pb(x[i][p[j]]);
        }
        ans[t]++;
    }
    cout<<ans.size()<<endl;
    for(auto i:ans){
        cout<<i.second<<" ";
    }
    cout<<endl;
}

Conclusion: the difficulty of this skill has been improved. It is still an old problem to test reading comprehension ability. There are too many wa skills. During the Spring Festival, we need to specially train the ability to pass it once. Sobbing, the next game will be after the new year. It's time to cf score points during this period, Wuhu.

Keywords: C++ Algorithm data structure Dynamic Programming

Added by evil turnip on Sun, 30 Jan 2022 02:13:31 +0200