0x03 basic algorithm - recursion

Posted by web_loone_08 on Fri, 18 Feb 2022 12:58:22 +0100

1.A [HNOI2003] laser bomb

Link: https://ac.nowcoder.com/acm/contest/999/A
Source: niuke.com

A new type of laser bomb can destroy all targets in a square with side length R.
Now there are n(N ≤ 10000) targets on the map. The integer Xi and Yi (whose value is [05000]) represent the position of the target on the map, and each target has a value.
The laser bomb is placed through satellite positioning, but it has one disadvantage: its blasting range, that is, the side of the square with side length R must be parallel to the x and y axes.
If the target is located on the edge of the blasting square, the target will not be destroyed.

Enter Description:

The first line of the input file is a positive integer n Sum positive integer R,Next n Each row has three positive integers, representing xi,yi ,vi . 

Output Description:

The output file has only a positive integer, which indicates the maximum number of targets with a total value on the map that can be blown up by a bomb (the result will not exceed 32767).

Example 1

input

2 1
0 0 1
1 1 1

output

1

remarks:

For 100%Data, guarantee 1≤n≤10^4,0≤xi,yi≤5×10^3,1≤m≤5×10^3,1≤vi<100. 

Idea: two dimensional difference

#include<stdio.h>
#include<iostream>
#include<math.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
const int maxn=5010;
int maps[maxn][maxn];
int val[maxn][maxn];//Calculate prefix and
int main()
{
    int T;
    int R;//Bomb border
    cin>>T;
    cin>>R;
    int xx=R,yy=R;//Confirm map boundaries
    int x,y,w;
    while (T--){
        cin>>x>>y>>w;
        x++;
        y++;//Change the initial coordinates to (1,1) to start
        maps[x][y]=w;
        xx=max(x,xx);
        yy=max(y,yy);//Initialize boundary
    }
    for(int i=1;i<=xx;i++)//Calculate prefix and
    for(int j=1;j<=yy;j++){
        val[i][j]=val[i-1][j]+val[i][j-1]-val[i-1][j-1]+maps[i][j];
    }
    int ans=0;//Calculate the maximum value that a bomb can cost
    for(int i=R;i<=xx;i++)
    for(int j=R;j<=yy;j++){
        ans=max(ans,val[i][j]-val[i-R][j]-val[i][j-R]+val[i-R][j-R]);
    }
    cout<<ans;
    return 0;
}

2.B IncDec Sequence

Link: https://ac.nowcoder.com/acm/contest/999/B
Source: niuke.com

Title Description

Given a sequence a1,a2,..., an with length n(n ≤ 10 ^ 5);
You can select an interval [l,r] every time, so that the number of subscripts in this interval increases or decreases by one.
And find out how many kinds of sequences may be obtained on the premise of ensuring the minimum number of times.

Enter Description:

The first line is a positive integer n. 
next n Line, one integer per line, second i+1 Integer representation of rows aia_iai. 

Output Description:

The first line outputs the minimum number of operations.
The second line outputs how many results can be obtained in the end.

Example 1

input

1
2
2

output

1
2

remarks:

For 100%Data, n=100000,0≤ai<21474836480 \leq ai \lt 21474836480≤ai<2147483648

thinking
Reference blog: https://shineeternal.blog.csdn.net/article/details/101078881

Why let d[2]~d[n] be 0, because if the difference fraction group is prefixed and summed, only d[2]~d[n] is 0, the calculation results are equal.
question: so how many times do we have to do it? Max (sum of positive numbers, sum of negative absolute values) why?

Because every time you operate on the original array, the interval [l,r] is reflected on the differential array, which is d [l] + 1, d [R + 1] - 1 (or D [l] - 1, d [R + 1] + 1)
Because each operation can only be applied to one positive number - 1 and one negative number + 1, min (sum of positive numbers and sum of negative absolute values) is required
If there are still positive or negative numbers remaining, we regard it as + 1 or - 1 of [l,l], that is, their difference ABS (sum of positive and negative absolute values)
Min (sum of positive numbers, sum of negative absolute values) + ABS (sum of positive numbers - sum of negative absolute values) = max (sum of positive numbers, sum of negative absolute values)

question: second, how to calculate?
That is, the first value of the changed element
Then a clue from the previous question, if there are still positive numbers left.
For example, the current difference sequence is d[1]:4 d[2]:0 d[3]:2 d[4]:0
Then we have two choices, either eliminate it ourselves or eliminate it with d[1].
I.e. self cancellation d[1]:4
And d[1] cancel once d[1]:5
And d[1] cancel twice d[1]:6
So it's ABS (sum of positive numbers - sum of negative absolute values) + 1

#include<stdio.h>
#include<iostream>
#include<math.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
const int maxn=1e5+5;
int Sequence[maxn];
int main()
{
    int T;
    cin>>T;
    long long pos;
    long long neg;
    pos=0;
    neg=0;
    for(int i=1;i<=T;i++)
        cin>>Sequence[i];
    for(int i=2;i<=T;i++){
        if(Sequence[i]-Sequence[i-1]>0)
            pos+=Sequence[i]-Sequence[i-1];
        else
            neg-=Sequence[i]-Sequence[i-1];
    }
    cout<<max(pos,neg)<<endl<<abs(pos-neg)+1<<endl;
}

3.C Tallest Cow

Link: https://ac.nowcoder.com/acm/contest/999/C
Source: niuke.com

Title Description

FJ's N (1 ≤ N ≤ 10,000) cows conveniently indexed 1...N are standing in a line. Each cow has a positive integer height (which is a bit of secret). You are told only the height H (1 ≤ H ≤ 1,000,000) of the tallest cow along with the index I of that cow.
FJ has made a list of R (0 ≤ R ≤ 10,000) lines of the form "cow 17 sees cow 34". This means that cow 34 is at least as tall as cow 17, and that every cow between 17 and 34 has a height that is strictly smaller than that of cow 17.
For each cow from 1...N, determine its maximum possible height, such that all of the information given is still correct. It is guaranteed that it is possible to satisfy all the constraints.

Enter Description:

Line 1: Four space-separated integers: N, I, H and R
Lines 2..R+1: Two distinct space-separated integers A and B (1 ≤ A, B ≤ N), indicating that cow A can see cow B.

Output Description:

Lines 1..N: Line i contains the maximum possible height of cow i.

Example 1

input

9 3 5 5
1 3
5 3
4 3
3 7
9 8

output

5
4
5
3
4
4
5
5
5

Topic Description:

Farmer John has n cows. They are arranged in a row. Farmer John only knows the serial number of the tallest cow and its height. The height of other cows is unknown. Now FarmerJohn has RRR information in his hand, and each information has the serial numbers of two cows (A and b). The height of cow B must be greater than or equal to that of cow a, and the height of all cows between a and B is smaller than that of cow a. Now farmer John wants you to find the maximum possible height of each cow based on this information. (data guaranteed)

Input format:
Line 1: four integers separated by spaces: n, i, h and R (the meaning of N and R is shown in the question surface; i and h mean that the height of the ith cow is h, and he is the tallest cow)

Next line R: two different integers a and b (1 ≤ a, b ≤ n)

Output format:
A total of n lines represent the maximum possible height of each cow

Idea:

The height of cattle between two visible cattle is strictly less than, so the height between two cattle should be reduced by 1. Prefix and maintenance are used
There may be duplicate data, which should be judged

#include<stdio.h>
#include<iostream>
#include<math.h>
#include<stdlib.h>
#include<algorithm>
#include<map>

using namespace std;
const int maxn=1e5+5;
map<pair<int,int>,bool> book;
int Sequence[maxn];
int main()
{
    int n,i,h,r;
    cin>>n>>i>>h>>r;
    int x,y;
    for(int j=1;j<=r;j++){
        cin>>x>>y;
        if(x>y) swap(x,y);
        if(book[make_pair(x,y)]) continue;
        book[make_pair(x,y)]=1;
        Sequence[x+1]--;
        Sequence[y]++;
    }
    for(int j=1;j<=n;j++){
        Sequence[j]=Sequence[j-1]+Sequence[j];
        cout<<Sequence[j]+h<<endl;
    }
}

Topics: Algorithm