current position:Home>I have encountered several array problems in C + +. I hope the big God can solve the puzzle. It's best to have comments

I have encountered several array problems in C + +. I hope the big God can solve the puzzle. It's best to have comments

2022-02-03 00:01:33 CSDN Q & A

Xiao Ming and queue up

describe :
Xiao Ming likes queuing now , That day Xiao Ming got a queue , Everyone in the queue has height and weight , Xiao Ming came up with such a question , For one j Position person , Find the first person on his left who is the same height as him , Suppose this person's position is i , that j The reward for the person in the position is i To j Between ( barring i and j ) Everyone's weight and , Xiao Ming wants to know the reward of the person who gets the most reward in this queue . But Xiao Ming can't do , Smart, help Xiao Ming solve this problem .
Input format
Enter one on the first line n, Indicates the number of people in the queue
Next n That's ok , Two numbers per line h and w, Indicates the height and weight of the person
Output format
Output a number , Indicates the maximum reward .
Data range
1<=n<=1000000
1<=h,w<=1000
sample input

5
10 20
20 10
20 30
10 10
20 20

sample output

Sample explanation
The first one , The second person and the third person get 0 Reward the fourth person to get 10+30=40 Reward
The fifth person gets 10 Reward




Refer to the answer 1:

Record the height and weight of the input person into the array , Each person entered , Then search for the first person with the same height from the existing data in the array , Then sum the weight of everyone between the two positions

#include <iostream>using namespace std;#define MAX 1000000int main(){    int n,i;    cin>>n;    int *sg = new int[n];    int *tz = new int[n];    for(i=0;i<n;i++)    {        cin>>sg[i]>>tz[i];        for(int j=i-1;j>=0;j--)        {            if(sg[j] == sg[i])            {                int sum = 0;                for(int k=j+1;k<i;k++)                    sum += tz[k];                cout<<sum<<endl;                break;            }        }    }        return 0;}



Refer to the answer 2:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.primo.wiki/2022/02/202202030001323270.html

Random recommended