C++ Introductory Algorithm Training - Operation Grid, Node Selection (with code)

Example - Manipulating the grid 

topic background

        There are n grids, placed in a row from left to right, numbered 1-n.

There are m operations in total, and there are 3 types of operations:

  1. Modify the weight of a grid,
  2. Find the weight sum of a continuous segment of grids,
  3. Find the maximum value of a continuous grid.

        For each 2, 3 operation, output the result you have obtained.

input and output format

input format

        The first line contains 2 integers n, m.

        The next line of n integers represents the initial weights of n grids.

        Next m lines, each line contains 3 integers p, x, y, p indicates the type of operation, when p=1, it means modifying the weight of the grid x to y, and when p=2, it means finding the weight of the grid in the interval [x, y] Value sum, p=3 means finding the maximum weight of the grid in the interval [x,y].

output format

        There are a number of rows equal to the total number of operations for p=2 or 3.

        1 integer per line, corresponding to the result of each p=2 or 3 operation.

Input and output samples

input sample

4 3
1 2 3 4
2 1 3
1 4 3
3 1 4

output sample

6
3

Instructions/Tips

  1. For 20% of the data n <= 100, m <= 200.
  2. For 50% of the data n <= 5000, m <= 5000.
  3. For 100% of the data 1 <= n <= 100000, m <= 100000, 0 <= grid weight <= 10000.

Reference Code

#include <stdio.h>  
#define left (2 * i)  
#define right (2 * i + 1)  
#define mid ((a[i].l + a[i].r) / 2)  
#define max(a, b) (a)>(b)?(a):(b)  
  
struct node{  
    int l, r, sum, max;  
}a[1000000];  
  
void init(int x, int y, int i)  
{  
    a[i].l = x;  
    a[i].r = y;  
    a[i].sum = 0;  
    a[i].max = 0;  
    if(x != y){  
        init(x, mid, left);  
        init(mid+1, y, right);  
    }     
}  
  
void input(int i, int x, int w)  
{  
    if(x == a[i].l && x == a[i].r){  
        a[i].sum = w;  
        a[i].max = w;  
    }  
    else{  
        if(x > mid){  
            input(right, x, w);  
        }else{  
            input(left, x, w);  
        }  
      
        a[i].sum = a[left].sum + a[right].sum;  
        a[i].max = max(a[left].max, a[right].max);    
          
    }  
}  
  
int get_sum(int x, int y, int i)  
{     
    if(a[i].l == x && a[i].r == y){  
        return a[i].sum;  
    }  
      
    if(x > mid){  
        return get_sum(x, y, right);  
    }  
    else if(y <= mid){  
        return get_sum(x, y, left);  
    }  
    else{  
        return get_sum(x, mid, left) + get_sum(mid+1, y, right);  
    }  
}  
  
int get_max(int x, int y, int i)  
{  
    if(a[i].l == x && a[i].r == y){  
        return a[i].max;  
    }   
    if(x > mid){  
        return get_max(x, y, right);  
    }  
    else if(y <= mid){  
        return get_max(x, y, left);  
    }  
    else{  
        return max(get_max(x, mid, left), get_max(mid+1, y, right));  
    }  
}  
  
int main()  
{  
    int i, n, m, w, p, x, y;  
      
    scanf("%d%d", &n, &m);  
      
    init(1, n, 1);  
      
    for(i = 1; i <= n; i ++){  
        scanf("%d", &w);  
              
        input(1, i, w);  
    }  
      
    for(i = 0; i < m; i ++){  
        scanf("%d%d%d", &p, &x, &y);  
        switch(p){  
            case 1:  
                input(1, x, y);  
                break;  
            case 2:  
                printf("%d\n", get_sum(x, y, 1));  
                break;  
            case 3:  
                printf("%d\n", get_max(x, y, 1));  
                break;  
        }  
    }  
      
    return 0;  
}  

Inference from one example to another - node selection

topic background

        There is a tree with n nodes, and each node in the tree has a positive integer weight. If a point is selected, none of its neighbors in the tree can be selected. What is the maximum weight sum of the selected points?

input and output format

input format

        The first line contains an integer n.

        The next line contains n positive integers, and the i-th positive integer represents the weight of point i.

        There are n-1 lines in total, and each line describes an edge on the tree.

output format

        Output an integer representing the maximum value of the weight sum of the selected points.

Input and output samples

input sample

5
1 2 3 4 5
1 2
1 3
2 4
2 5

output sample

12

Instructions/Tips

        Select points 3, 4, and 5, and the weight sum is 3+4+5 = 12.


Reference Code

#include<iostream>
#include<vector>
using namespace std;
vector<int>node[100001];
  int dp[100001][2];   
int visit[100001];
int value[100001];
void Dfs(int tn)
{
    int i;
    dp[tn][0]=0; 
    dp[tn][1]=value[tn];
    visit[tn]=1;
    for(i=0;i<node[tn].size();i++)
    {
        int son;
        son=node[tn][i];
        if(visit[son])
            continue;
        Dfs(son);
        dp[tn][1]=dp[tn][1]+dp[son][0];
        dp[tn][0]=dp[tn][0]+max(dp[son][0],dp[son][1]);
    }
}
int max(int a,int b)
{
    if(a>b)
        return a;
    else
        return b; 
}
int main()
{
    int n,x,y,Max;
    int i;
    cin>>n;
    for(i=1;i<n+1;i++)
    {
        cin>>value[i];
        visit[i]=0;
    }
    for(i=1;i<n;i++)
    {
        cin>>x>>y;
        node[x].push_back(y);
        node[y].push_back(x);
    }
    Dfs(1);
    Max=dp[1][0];
    Max=max(Max,dp[1][1]);
    cout<<Max;
    return 0;
}

Guess you like

Origin blog.csdn.net/leyang0910/article/details/130670718