2022-03-06 swipe questions and punch in every day

Posted by mshen on Sun, 06 Mar 2022 13:37:26 +0100

2022-03-06 swipe questions and punch in every day

Source code - div2 daily question

Path count - topic - Daimayuan Online Judge

There is an n × N grid, some grids are passable, and some grids are obstacles.

At the beginning of your position in the upper left corner, you can go down or right every step and ask how many options there are to go to the lower right corner.

Because the answer is very big, output the result of modulo 10 ^ 9 + 7.

Input format

The first line is a positive integer n.

The next N lines are n positive integers in each line. 11 indicates that it is passable and 00 indicates that it is not passable.

Output format

An integer representing the answer.

sample input

3
1 1 1
1 0 1
1 1 1

sample output

2

Data scale

For 100% 100% data, ensure 2 ≤ n ≤ 100, and the upper left corner and lower right corner are accessible.

Very simple dp, because the way we reach a lattice can only come from the top or bottom (except the upper and left boundaries). We prepare dp array F, f[i] [j], which means that there are f[i] [j] walking methods for the lattice reaching the position of I and J. normally, the state transition equation is f[i] [j]=f[i] [j-1]+f[i-1] [j]. However, there are obstacles in this question, and the obstacles cannot be reached. Then we can directly turn f[i] [j] into 0. The answer is f[n-1] [n-1].

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<set>
#include<string>
#include<map>
#include<unordered_map>
#include<stack>

typedef long long ll;
typedef pair<ll, int>PII;
const int MOD = 1e9 + 7;
const int N = 1010;
ll f[N][N], h[N][N];

int main()
{
	int n;
	cin >> n;
	bool flag = true;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			cin >> h[i][j];
		}
	}
	for (int i = 0; i < n; i++)
	{
		if (h[0][i] == 0)flag = false;
		if (flag)f[0][i] = 1;
		else f[0][i] = 0;
	}
	flag = true;
	for (int i = 0; i < n; i++)
	{
		if (h[i][0] == 0)flag = false;
		if (flag)f[i][0] = 1;
		else f[i][0] = 0;
	}
	for (int i = 1; i < n; i++)
	{
		for (int j = 1; j < n; j++)
		{
			if (h[i][j] == 0)f[i][j] = 0;
			else f[i][j] = (f[i - 1][j] + f[i][j - 1]) % MOD;
		}
	}
	cout << f[n - 1][n - 1] << endl;
	return 0;
}

Force deduction - one question per day

2100. Suitable for bank robbery

You and a group of robbers are going to rob the bank. Give you an integer array security with subscript starting from 0, where security[i] is the number of guards on duty on day I. Days are numbered from 0. And give you an integer time.

If all of the following conditions are met on day i, we call it a day suitable for bank robbery:

There are at least time days before and after the i day.
The number of non consecutive days is increasing.
The number of guards is non decreasing for consecutive days after i day.
More formally, day I is a suitable day to rob banks if and only if: security [I - time] > = security [I - time + 1] > =... > = security [i] < =... < = security [i + time - 1] < = security [i + time]

Please return an array containing all the days suitable for bank robbery (subscript starts from 0). The returned days can be arranged in any order.

Example 1:

Input: security = [5,3,3,3,5,6,2], time = 2
Output: [2,3]
Explanation:
On the second day, we have security [0] > = security [1] > = security [2] < = security [3] < = security [4].
On day 3, we have security [1] > = security [2] > = security [3] < = security [4] < = security [5].
No other day meets this condition, so days 2 and 3 are suitable for robbing banks.

Prepare two arrays. One is to save the length of non incremental sequence from the front to the back, and the other is to save the length of non incremental sequence from the back to the front. Traverse security. As long as down [i] > = time and up [i] > = time, it can be considered that this is a suitable day for robbery. Record in the array, and finally return the array of records.

class Solution {
public:
    vector<int> goodDaysToRobBank(vector<int>& security, int time) {
       
        int n=security.size();
        vector<int>v;
        if(time==0)
        {
            for(int i=0;i<n;i++)v.push_back(i);
            return v;
        }
        vector<int>up(n,0),down(n,0);
        for(int i=1;i<n;i++)
        {
            if(security[i]<=security[i-1])down[i]=down[i-1]+1;
        }
        for(int i=n-2;i>=0;i--)
        {
            if(security[i]<=security[i+1])up[i]=up[i+1]+1;
        }
        for(int i=time;i<=n-time;i++)
        {
            if(i>0&&i<n-1&&up[i]>=time&&down[i]>=time)v.push_back(i);
        }
        return v;
    }
};

Topics: C++ Algorithm leetcode