The fifth day of labor union

I'm dead

I am a trash

I am a toilet cleaner

Waking up late in the morning, I almost locked my dormitory and couldn't get out. I didn't eat in the morning because of the meal card.
After watching yxc's video, I will start coding

Robber Afu

Title description

Ah Fu is an experienced robber. Taking advantage of the dark and high winds of the moon, Ah Fu plans to looting a street shop tonight.
There are NN stores on this street, and each store has some cash.
Afu's prior investigation revealed that only when he ransacked two adjacent shops at the same time, the street alarm system would be activated, and then the police would swarm in.
As a thief who has always been cautious in committing crimes, Ah Fu is unwilling to take the risk of being hunted down by the police to steal.
He wants to know how much cash he can get tonight without alarming the police?
Input format
The first line of input is an integer TT, which means there is a total of TT group data.
For each next set of data, the first line is an integer NN, indicating that there are NN stores in total.
The second line is NN positive integers separated by spaces, indicating the amount of cash in each store.
The amount of cash in each store does not exceed 1,000.

Output format

For each group of data, output one line.
This line contains an integer that represents the amount of cash that Afu can get without alerting the police.

Sample

Input sample

2
3
1 8 2
4
10 7 6 14

Sample output

8
24

Sample explanation

For the first set of examples, Ah Fu chose the second shop to steal and the amount of cash obtained was 8.
For the second set of examples, Ah Fu chooses the first and fourth shops to steal, and the amount of cash obtained is 10+14=24.

#include<bits/stdc++.h>
using namespace std;
int n,w[100861],t,f[100861];
int main()
{
    
    
	cin>>t;
	while(t--)
	{
    
    
		memset(f,0,sizeof(f));
		scanf("%d",&n);
		for(int i=1;i<=n;i++)
			scanf("%d",&w[i]);
		f[1]=w[1];
		for(int i=2;i<=n;i++)
		{
    
    
			f[i]=max(f[i-1],f[i-2]+w[i]); 
		}
		cout<<f[n]<<endl;
	}
	return 0;
} 

This question is more watery in dp, f[i] represents the maximum number of steals in the i-th shop, and the current state may be from... I don’t know how to say it, look at the picture
Insert picture description here
if f[i] inherits f [i-1] can’t steal w[i], so compare f[i-1] with f[i-2]+w[i], which is a Miao problem.

Stock Trading IV

Title description

Given an array of length NN, the iith number in the array represents the price of a given stock on the iith day.
Design an algorithm to calculate the maximum profit you can get, you can complete up to kk transactions.
Note: You cannot participate in multiple transactions at the same time (you must sell the previous stocks before buying again). One purchase and sale are combined into one transaction.

Input format

The first line contains the integers NN and kk, which represent the length of the array and the maximum number of transactions you can complete.
The second line contains NN positive integers not exceeding 1000010000, which represents a complete array.

Output format

Output an integer, representing the maximum profit.

Sample

Input example 1

3 2
2 4 1

Sample output 1

2

Input example 2

6 2
3 2 6 5 0 3

Sample output 2

7

Sample explanation

Example 1: Buy on the 1st day (stock price = 2), and sell on the 2nd day (stock price = 4). This exchange can make a profit = 4-2 = 2.
Example 2: Buy on the 2nd day (stock price = 2) and sell on the 3rd day (stock price = 6). The exchange can make a profit = 6-2 = 4. Then, buy on the 5th day (stock price = 0) and sell on the 6th day (stock price = 3). This exchange can make a profit = 3-0 = 3. The total profit is 4+3 = 7.
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+10,M=1e2+10;
int n,k,f[N][M][2],ans,a[N];
int main()
{
    
    
	cin>>n>>k;
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	memset(f,-10,sizeof(f));
	for(int i=0;i<=n;i++)
		f[i][0][0]=0;
	for(int i=1;i<=n;i++)
	{
    
    
		for(int j=1;j<=k;j++)
		{
    
    
			f[i][j][0]=max(f[i-1][j][0],f[i-1][j][1]+a[i]);
			f[i][j][1]=max(f[i-1][j][1],f[i-1][j-1][0]-a[i]);
		}
	}
	for(int i=1;i<=k;i++)
		ans=max(f[n][i][0],ans);
	cout<<ans;
	return 0;
}

Then began to write cultural class homework.
then

Energy Necklace

【Problem Description】

On the planet Mars, every Mars person wears an energy necklace with him. There are NN energy beads on the necklace. The energy bead is a bead with a head mark and a tail mark. These marks correspond to a positive integer. And, for two adjacent beads, the tail mark of the previous bead must be equal to the head mark of the next bead. Because only in this way, through the action of a sucker (a sucker is an organ for MarsMars people to absorb energy), the two beads can aggregate into a bead, and at the same time release the energy that can be absorbed by the sucker. If the head of the former energy bead is marked as mm, the tail is marked as rr, the head of the latter energy bead is marked as rr, and the tail is marked as nn, the energy released after polymerization is m×r×nm×r×n ( Mars unit), the head of the newly produced beads is marked as mm, and the tail is marked as nn.
When needed, the Mars would use a suction cup to clamp two adjacent beads, and obtain energy through aggregation until only one bead remained on the necklace. Obviously, the total energy obtained by different polymerization sequences is different. Please design a polymerization sequence to maximize the total energy released by a string of necklaces.
For example: Let N=4N=4, the head mark and tail mark of 44 beads are (2,3)(3,5)(5,10)(10,2)(2,3)(3,5) (5,10)(10,2). We use the notation ⊕⊕ to denote the polymerization operation of two beads, and (j⊕k)(j⊕k) denotes the energy released by the jth, kj, and k-th beads after they are polymerized. Then the energy released by the 4th, 14th and 1st beads after polymerization is:
(4⊕1)=10×2×3=60
(4⊕1)=10×2×3=60
This string of necklaces can get the most The total energy released by a polymerization sequence of the
figure of merit is (((4⊕1)⊕2)⊕3)=10×2×3+10×3×5+10×5×10=710
(((4⊕ 1)⊕2)⊕3)=10×2×3+10×3×5+10×5×10=710

【Input file】

The first line of the input file energy.in is a positive integer NN (4≤N≤1004≤N≤100), which represents the number of beads on the necklace. The second line is NN positive integers separated by spaces, and all the numbers do not exceed 10001000. The iith number is the head mark of the iith bead (1≤i≤N1≤i≤N). When i<Ni<N, the tail mark of the iith bead should be equal to that of the i+1i+1th bead Head mark. The tail mark of the NNth bead should be equal to the head mark of the 11th bead.
As for the order of the beads, you can determine it like this: put the necklace on the table without crosses, specify the first bead at will, and then determine the order of the other beads in a clockwise direction.

[Output file]

The output file energy.out has only one line, which is a positive integer EE (E≤2.1×109E≤2.1×109), which is the total energy released by an optimal aggregation sequence.

【Example 1】

ex_energy1.in

4
2 3 5 10

ex_energy1.ans

710

#include<bits/stdc++.h>
using namespace std;
const int N=520;
long long q,n,head[N],tail[N],f[N][N],da=-1;
int main()
{
    
    
	scanf("%lld",&n);
	for(int i=1;i<=n;i++)
	{
    
    
	    scanf("%lld",&head[i]);
	    tail[i-1]=head[i];
	    head[i+n]=head[i];
	    tail[i+n-1]=head[i];
	    tail[2*n]=head[1];
	}
	for(int i=1;i<=n;i++)
	{
    
    
		for(int j=1;j<=2*n;j++)
		{
    
    
			q=i+j-1;
			if(q>2*n) break;
			for(int k=j;k<q;k++)
				f[j][q]=max(f[j][q],f[j][k]+f[k+1][q]+head[j]*tail[k]*tail[q]);
		}
	}
	for(int i=1;i<=n;i++)
		da=max(da,f[i][i+n-1]);
	printf("%lld",da);
	return 0;
} 

Then the teacher called for a physical education teacher to give a physical education class, which was outrageous.
When I went to the playground to play "Together in the Same Boat", my knee hurts and my shoes are full of stones, although the process is very happy. After I came back, I finished the Chinese homework in
one go, and then the teacher hurriedly wrote the code when he came.

Ring pebbles merge

Title description

Arrange the nn piles of stones around the circular playground. Now the stones are to be combined into one pile in an orderly manner. It is stipulated that only two adjacent piles can be selected and merged into a new pile each time, and the number of stones in the new pile is recorded as the score of the merger.
Please write a program that reads the number of piles nn and the number of stones in each pile, and calculates as follows:
Choose a scheme of merging stones so that the total score of n−1n−1 merges is the largest.
Choose a scheme of merging stones so that the sum of scores for n−1n−1 merging is the smallest.

Input format

Enter an integer nn in the first line, which means there are nn piles of stones.
The nn integers in the second line represent the number of stones in each pile.

Output format

There are two lines of output: the
first line is the minimum sum of combined scores, and the
second line is the maximum sum of combined scores.

Sample

Input sample

4
4 5 9 4

Sample output

43
54

#include <bits/stdc++.h>
using namespace std;
const int N=410;
int n,a[N],f[N][N],g[N][N],s[N],r,maxx,minn;
int main()
{
    
    
	cin>>n;
	for(int i=1;i<=n;i++)
	{
    
    
		cin>>s[i];
		s[i+n]=s[i];
	}
	for(int i=1;i<=n*2;i++)	s[i]+=s[i-1];
	memset(f,10,sizeof f);memset(g,-10,sizeof g);
	maxx=-999999;minn=888888;
	for(int i=1;i<=n;i++)
	{
    
    
		for(int j=1;j+i-1<=2*n;j++)
		{
    
    
			r=i+j-1;
			if(i==1)	f[j][r]=g[j][r]=0;
			else
			{
    
    
				for(int k=j;k<r;k++)
				{
    
    
					f[j][r]=min(f[j][r],f[j][k]+f[k+1][r]+s[r]-s[j-1]);
					g[j][r]=max(g[j][r],g[j][k]+g[k+1][r]+s[r]-s[j-1]);
				}
			}
		}
	}
	for(int i=1;i<=n;i++)
	{
    
    
		maxx=max(maxx,g[i][i+n-1]);
		minn=min(minn,f[i][i+n-1]);
	}
	cout<<minn<<endl<<maxx;
	return 0;
}

Then I started writing physics homework again, which was great.
Insert picture description here

Guess you like

Origin blog.csdn.net/ydsrwex/article/details/113745542
Recommended