2022 Meituan School Recruitment Technical Post Written Test All AC_Code Sharing

[Automatic Vehicle Algorithm Post] After 5 seconds, I still failed to AK. The third question only got 18% of the data right at the beginning. When there were 20 minutes left, I found that the question was wrong. When I code to cout<<ans<<endl; In the code box, the cursor has just been placed on the save code, and I found that I can't press it anymore, my dear, the time has expired! ! !

The written test questions are divided into two parts: 4 algorithm questions + 3 multiple-choice questions related to artificial intelligence. The time limit is two hours. After each subsection is submitted, you can go back and modify it. This is still very user-friendly. Unlike some written tests, you cannot return to modify the subsection after submitting it, which is not so friendly.

The questions are probably all about the difficulty of [mid~hard], so let’s get to the point! Since there is still a written test in the evening, the specific proof of ideas will be added later, please post the Code first.


Question 1_Sichuan Mahjong 

meaning of the title

The gameplay is to choose 3 cards of the same suit after the cards are drawn and exchange them for others in a certain order. In order to destroy the opponent's game experience as much as possible, each time, three cards that are not consecutive are selected to be exchanged out. For example, if you have {1 4 5 6 8} these 5 slivers, you may choose {1 5 8} these 3 slivers to exchange out.
Simplifying this problem, you are now given a repeatable set, and hopefully you choose a subset of it as large as possible so that no two numbers in it are "consecutive".

Description of input and output

enter

The first line is an integer n (1<=n<=100000), which represents the resettable size.
The second line is an integer separated by n spaces (the range is between 1 and 200000), which represents the resettable.

output

Output the size of the largest subset that satisfies the condition

sample data

enter

6

1 2 3 5 6 7

output

4

AC_Code

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+10, M = 2e5+10;
int n, a[N], dp[M];

int main() {
	cin>>n;
	for(int i=1; i<=n; i++)  cin>>a[i];
	for(int i=1; i<=n; i++)	 dp[a[i]] = 1 ;
	
	for(int i=1; i<M; i++) {
		if(dp[i])  dp[i] = max(dp[i],dp[i-2]+1);
		dp[i] = max(dp[i],dp[i-1]);
	}
	cout << dp[M-1] << endl;
	return 0;
}


Second question _ flip the largest subsection sum

meaning of the title

The maximum subsection sum is a classic problem of force buckle. Here it is adapted to allow flipping consecutive sections of the array before solving the problem, e.g. flipping the subarray of the 3rd to 5th elements of (1,2,3.45.6) yields (1,2,3.45.6) 2, 5, 4, 3, 6), find the maximum number of sub-segments of the array after flipping.

Description of input and output

enter

The first line - a positive integer n, (1<=n<=100000), representing the length of the array
The second line is an integer separated by n spaces (-1000<=ai<=1000), representing the size of the array element

output

Find the largest subsegment sum of the resulting array after flipping

sample data

enter

6

-1 3 -5 2 -1 3

output

7

AC_Code

#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+10, inf = 1e9+10;
int n, a[N], p[N], s[N];

void solve() {
	p[0] = -inf;
	int sum=0;
	for(int i=1; i<=n; i++) {
		sum = max(0, sum) + a[i];
		p[i] = max(p[i-1], sum);
	}
	s[n+1]=-inf;	sum=0;
	for(int i=n; i>=1; i--) {
		sum = max(0, sum) + a[i];
		s[i] = max(s[i+1], sum);
	}
}

int main() {
	cin>>n;
	for(int i=1; i<=n; i++) cin>>a[i];
	solve();
	int ma = *max_element(p+1, p+n+1);
	for(int i=1; i<n; i++) 
		ma = max(ma, p[i]+s[i+1]);
	cout << ma << endl;
	return 0;
}

Question 3_Cut tofu

meaning of the title

In order to cut out more even tofu, I want to know how big the largest piece of tofu is in the tofu pieces cut out after each cut.

Description of input and output

In the first line, there are two integers n, m (1<=n,m<=1000), which means that the length, width and height are n cm at the beginning, and m knives are cut in total.
The second line contains m lowercase letters, each of which is one of x,y,z. The i-th represents which coordinate axis the knife is perpendicular to.
The third row contains m positive integers greater than 0 and less than n. The i-th represents the distance from the plane where the i-th knife is located to the upper right corner of the tofu (or choose a corner and fix it, no matter which corner is selected, the answer is the same).
Ensure that the tofu will not be deformed and will not be displaced when cutting. Every time the knife is cut, the whole piece of tofu will be cut open, and there is no cut in half to close the knife.

sample data

enter

2 3

x y z

1 1 1

output

4

2

1

AC_Code

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+10, M = 2e5+10;
int n, m;

int main() 
{
	cin >> n >> m;
	vector<string> op(m+1);
	for(int i=1; i<=m; i++) cin>>op[i];
	vector<int> v(m+1);
	for(int i=1; i<=m; i++) cin>>v[i];
	
	multiset<int> x,y,z;
	x.insert(0), x.insert(n);
	y.insert(0), y.insert(n);
	z.insert(0), z.insert(n);
	
	for(int i=1; i<=m; i++) 
    {
		if(op[i][0]=='x')  x.insert(v[i]);
		else if(op[i][0]=='y')  y.insert(v[i]);
		else if(op[i][0]=='z')  z.insert(v[i]);
	}
	
	int mx=0, my=0, mz=0, pre=0;
	for(auto it:x)  mx = max(mx,it-pre), pre=it;
		pre=0;
	for(auto it:y)  my = max(mx,it-pre), pre=it;
		pre=0;
	for(auto it:z)  mz = max(mx,it-pre), pre=it;
	
	vector<int> ans(m+1);
	for(int i=m; i>=1; i--) 
	{
		ans[i] = mx*my*mz;
		if(op[i][0]=='x') 
		{
			auto it = x.find(v[i]);
			int suf = *(++it);
			it--;
			int pre = *(--it);
			mx = max(mx, suf-pre);
			x.erase(v[i]);
		}
		if(op[i][0]=='y') 
		{
			auto it = y.find(v[i]);
			int suf = *(++it);
			it--;
			int pre = *(--it);
			my = max(my, suf-pre);
			y.erase(v[i]);
		}
		if(op[i][0]=='z') 
		{
			auto it = z.find(v[i]);
			int suf = *(++it);
			it--;
			int pre = *(--it);
			mz = max(mz, suf-pre);
			z.erase(v[i]);
		}
	}
	
	for(int i=1; i<=m; i++) 
		cout << ans[i] << endl;
	return 0;
}

Question 4_Interval operation summation

meaning of the title

Given an array of length n, perform interval operations on m arrays, including adding a value to all numbers in the interval and querying the sum of all numbers in an interval. If the elements in the initial array are allowed to be rearranged and the operations performed in sequence, how large can the sum of the answers for the sum of all the query intervals in the operation be?

Description of input and output

enter 

The first line has two numbers n, m (1<=n=<5000, 1<=m<=500), which represent the length of the array and the number of operations. The second line has n numbers, representing the elements in the initial array, and the next m lines each have 1 lr or 2 lrk, respectively representing the sum of the elements whose subscripts belong to [l,r]. All elements belonging to [l,r] are added with constant value k. lrk satisfies 1<=l<=r<=n, 1<=k<=5000

output

Output If the initial array is allowed to be rearranged, the maximum value of the sum of all outputs generated after m operations

sample data

enter

5 5

3 4 2 1 5

1 1 3

2 3 4 2

1 2 4

2 2 3 2

1 3 5

output

42

AC_Code

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+10, M = 2e5+10;
int n, m;

int main() {
	cin>>n>>m;
	vector<ll> a(n+1);
	for(int i=1; i<=n; i++) cin>>a[i];
	ll ans_sum=0;
	vector<ll> cnt(n+1),add(n+1);
	
	for(int i=1; i<=m; i++) {
		int op; cin>>op;
		if(op==1) {
			int l,r;
			cin>>l>>r;
			for(int i=l; i<=r; i++) cnt[i]++, ans_sum += add[i];
		}
		else{
			int l,r,k;
			cin>>l>>r>>k;
			for(int i=l; i<=r; i++) add[i] += k;
		}
	}
	
    sort(a.begin()+1,a.end());
    sort(cnt.begin()+1,cnt.end());
    
    for(int i=1; i<=n; i++) 
    	ans_sum += 1LL * cnt[i] * a[i];
	cout << ans_sum << endl;
	return 0;
}

 OVER~ 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325242735&siteId=291194637