AtCoder Problem Solution-AtCoder Beginner Contest 185-D-Stamp-Simulation

Topic related

Topic link

AtCoder Beginner Contest 185 D 题,https://atcoder.jp/contests/abc185/tasks/abc185_d

Problem Statement

There are N squares arranged in a row from left to right. Let Square i be the i-th square from the left.
M of those squares, Square A1, A2, A3, …, AM, are blue; the others are white. (M may be 0, in which case there is no blue square.)
You will choose a positive integer k just once and make a stamp with width k. In one use of a stamp with width k, you can choose k consecutive squares from the N squares and repaint them red, as long as those k squares do not contain a blue square.
At least how many uses of the stamp are needed to have no white square, with the optimal choice of k and the usage of the stamp?

Input

Input is given from Standard Input in the following format:

N M
A1 A2 A3 … AM

Output

Print the minimum number of uses of the stamp needed to have no white square.

Samples1

Sample Input 1

5 2
1 3

Sample Output 1

3

Explaination

If we choose k=1 and repaint the three white squares one at a time, three uses are enough, which is optimal.
Choosing k=2 or greater would make it impossible to repaint Square 2, because of the restriction that does not allow the k squares to contain a blue square.

Samples2

Sample Input 2

13 3
13 3 9

Sample Output 2

6

Explaination

One optimal strategy is choosing k=2 and using the stamp as follows:

  • Repaint Squares 1,2 red.
  • Repaint Squares 4,5 red.
  • Repaint Squares 5,6 red.
  • Repaint Squares 7,8 red.
  • Repaint Squares 10,11 red.
  • Repaint Squares 11,12 red.

Note that, although the k consecutive squares chosen when using the stamp cannot contain blue squares, they can contain already red squares.

Samples3

Sample Input 3

5 5
5 2 1 4 3

Sample Output 3

0

Explaination

If there is no white square from the beginning, we do not have to use the stamp at all.

Samples4

Sample Input 4

1 0

Sample Output 4

1

Explaination

M may be 0.

Constraints

  • 1≤N≤10^9
  • 0≤M≤2×10^5
  • 1≤Ai≤N
  • Ai are pairwise distinct.
  • All values in input are integers.

Problem solving report

Topic translation

We have N squares, arranged from left to right. Among them, M squares have been painted in red, denoted as A1, A2, ..., AM. The rest is white. M can be zero.

We have to choose a positive integer k. We can paint k consecutive white squares red. Note that the ones that have been painted red can be colored repeatedly. Only white squares are allowed to be painted red. Please find out the number of times of coloring.

Topic analysis

It's another mock question. Question D this time is a bit watery. Let's analyze the data first, and then talk about pseudocode.

Sample data analysis

Sample data 1

According to the sample data, we can draw the following picture.

As shown in the picture above, there are a total of 5 squares, numbered 1 to 5. Among them, 1 and 3 are painted blue, and we need to paint the remaining white squares red. There are two continuous white areas in the above picture, the first area has a length of 1, and the second area has a length of 2. Since the blue square cannot be painted red, we can only choose the smallest length k=1. The coloring process is as follows:

the first time. Paint the number 2 square red.

the second time. Paint square number 4 in red.

the third time. Paint square number 5 in red.

Sample data 2

According to the sample data, we can draw the following picture.

As shown in the figure above, there are a total of 13 squares, numbered from 1 to 13, of which 3, 9 and 13 are painted blue, and we need to paint the remaining white squares red. There are three continuous white areas in the above picture. The first area has a length of 2, the second area has a length of 5, and the second area has a length of 3. Since the blue square cannot be painted red, we can only choose the smallest length k=2. The coloring process is as follows:

the first time. Paint squares 1 and 2 in red.

the second time. Paint squares 4 and 5 in red.

the third time. Paint squares 6 and 7 in red.

the fourth time. Paint squares 7 and 8 in red. Note that in accordance with the rules, repeat No. 7 color.

the fifth time. Paint squares 10 and 11 in red.

the sixth time. Paint squares 11 and 12 in red. Note that in accordance with the rules, repeat the coloring of No. 11.

Sample data 3

According to the sample data, we can draw the following picture.

As shown in the picture above, there are a total of 5 squares, numbered 1 to 5. All the squares are painted blue. So the answer is 0.

Sample data 4

According to the sample data, we can draw the following picture.

As shown in the figure above, there is a total of 1 square, numbered 1. None of the squares are painted blue, we need to paint the remaining white squares red. The picture above has a continuous white area, and the first area has a length of 1. We can only choose the smallest length k=1. The coloring process is as follows:

the first time. Paint square number 1 red.

algorithm design

From the above sample data analysis, we can see that the key is to find the smallest K.

detail

1. Since the sample data is not guaranteed to be sorted, referring to sample 2 and sample 3, we need to sort the input A.

2. By traversing the sorted A, we can get the gap array, which represents the length of each continuous white fast.

3. Find K. By traversing the gap array, find the non-zero minimum value of the gap array.

4. Count the number of paintings. We can traverse the gap array and calculate the relationship between gap[i] and k.

Pseudocode

读取n,m
读取数组 A
排序数组 A
遍历数据 A,计算间隔数组 gap
遍历数据 gap,找到最小非零 k
遍历数据 gap,计算 ans

AC reference code

//https://atcoder.jp/contests/abc185/tasks/abc185_d
//D - Stamp
#include <bits/stdc++.h>

using namespace std;

//如果提交到OJ,不要定义 __LOCAL
//#define __LOCAL

const int MAXN=1e9+4;
const int MAXM=2e5+4;
int a[MAXM];
int gap[MAXM];

int main() {
#ifndef __LOCAL
    //这部分代码需要提交到OJ,本地调试不使用
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
#endif
    int n, m;
    cin>>n>>m;
    for (int i=1; i<=m; i++) {
        cin>>a[i];
    }
    a[m+1]=n+1;
    sort(a+1, a+m+1);

    //差分
    int k=MAXN;
    int ans=0;
    for (int i=1; i<=m+1; i++) {
        gap[i]=a[i]-a[i-1]-1;
        if (gap[i]<k && gap[i]>0) {
            k=gap[i];
        }
    }
    if (MAXN!=k) {
        for (int i=1; i<=m+1; i++) {
            ans += ceil(1.0*gap[i]/k);
        }
    }
    cout<<ans<<"\n";

#ifdef __LOCAL
    //这部分代码不需要提交到OJ,本地调试使用
    system("pause");
#endif
    return 0;
}

The efficiency is not very high. Prepare for the interval between final exams and prevent Alzheimer's disease.

time complexity

O(MlogM). Note that we have a sort, this time complexity is the highest.

Space complexity

MAN).

Guess you like

Origin blog.csdn.net/justidle/article/details/111286992