Title Link: non zero segment division
Title Description
A1,A2,⋯,An It's a n An array of natural numbers (non negative integers). We call it Ai,⋯,Aj Is a non-zero segment if and only if the following conditions are met at the same time:
- 1≤i≤j≤n;
- For any integer k. If i ≤ K ≤ j, then Ak>0;
- i=1 or Ai−1=0;
- j=n or Aj+1=0.
Here are some simple examples:
- A=[3,1,2,0,0,2,0,4,5,0,2] Medium four The non-zero segments are [3,1,2],[2],[4,5] and [2];
- A=[2,3,1,4,5] have only one A non-zero segment;
- A=[0,0,0] Then there are no non-zero segments (i.e. the number of non-zero segments is) 0).
Now we can compare arrays A Do the following: select any positive integer p. Then A All less than p All the numbers become 0 Try to choose a suitable one p. Make array A The number of non-zero segments in reaches the maximum. If entered A The number of non-zero segments has reached the maximum, which can be taken p=1, i.e. No A Make any changes.
Input format
Read in data from standard input.
The first line of input contains a positive integer n.
The second line of input contains n A natural number separated by spaces A1,A2,⋯,An.
Output format
Output to standard output.
Only one integer is output to represent the array A The maximum number of non-zero segments after operation.
Example 1 input
11 3 1 2 0 0 2 0 4 5 0 2
Sample 1 output
5
Example 1 explanation
p=2 When, A=[3,0,2,0,0,2,0,4,5,0,2], 5 The non-zero segments are [3],[2],[2],[4,5] and [2]; At this time, the number of non-zero segments reaches the maximum.
Example 2 Input
14 5 1 20 10 10 10 10 15 10 20 1 5 10 15
Example 2 output
4
Data
Example 2 explanation
p=12 When, a = [0,0,20,0,0,0,15,0,20,0,0,0,0,15], 4 The non-zero segments are [20],[15],[20] and [15]; At this time, the number of non-zero segments reaches the maximum.
Example 3 input
3 1 0 0
Data
Example 3 output
1
Data
Example 3 explanation
p=1 When, A=[1,0,0], only one Non zero segments [1] , the number of non-zero segments reaches the maximum.
Example 4 input
3 0 0 0
Example 4 output
0
Example 4 explanation
whether p What is the value, A None of them contain non-zero segments, so the number of non-zero segments is at most 0
Subtask
70% The test data meet the requirements n≤1000;
All test data meet n≤5 × 10 ^ 5, and array A No more than 10^4.
The first version of no brain violence code... Has 70 points 2333
#include <iostream> #include <algorithm> using namespace std; int main() { int n; cin>>n; int* a=(int*)malloc(sizeof(int)*n); int* b=(int*)malloc(sizeof(int)*n); int* c=(int*)malloc(sizeof(int)*n); for(int i=0;i<n;i++) { cin>>a[i]; b[i]=a[i]; c[i]=a[i]; } sort(b,b+n); int p=0,num=0,max=-1; for(int i=0;i<n;i++) { p=b[i]; while(p==b[i]) { i++; } i--; for(int j=0;j<n;j++) { if(c[j]<p) { c[j]=0; } } num=0; for(int j=0;j<n;j++) { if(c[j]!=0) { num++; while(c[j]!=0) { j++; } j--; } } if(num>max) { max=num; } for(int j=0;j<n;j++) { c[j]=a[j]; } } if(max==-1) { max=0; } cout<<max; return 0; }
After you have changed everything you can find that can be optimized... Still timeout, or 70 points
#include <iostream> #include <cstdio> #include <set> using namespace std; int main() { int n; scanf("%d",&n); int* a=(int*)malloc(sizeof(int)*n); set<int> b; for(int i=0;i<n;i++) { scanf("%d",&a[i]); b.insert(a[i]); } int p=0,num=0,max=0; for(set<int>::iterator it=b.begin();it!=b.end();it++) { p=*it; if(p==0) { continue; } num=0; for(int j=0;j<n;j++) { if(a[j]<p) { a[j]=0; } if(a[j]>=p) { num++; while(a[j]>=p) { j++; } j--; } } if(num>max) { max=num; } } printf("%d",max); return 0; }
It is OK! As Xiaobai, I learned new knowledge! Take notes
A friendly introduction to Xiaobai:
(23 messages) basic introduction to STL set_ Zheng Dandan's blog - CSDN blog
(23 messages) detailed usage of set in STL_ Contentment blog - CSDN blog
About accessing the set element:
(23 messages) access to elements in the set container_ Running snail CSDN blog_ Access set element
//Access set element for(set<int>::iterator it=st.begin();it!=st.end();it++) { cout<<*it<<" "; }
Finally, I learned the solution of the boss!
(23 messages) csp2021-09-2 non-zero segment division_ Next door Li sou's blog - CSDN blog
This metaphor is really good! Make the meaning clear at once √
Although the reflection arc is long, I still stare at the code for a long time to reflect how it is implemented, and I may not write it next time
Notes notes
unique() function
#include <algorithm>
This is a more specific introduction:
(23 messages) unique function in C + +_ Simao CSDN blog
(23 messages) explanation of unique() function_ Bearded blog - CSDN blog_ unique
[finishing] unique function in C + + - nimphy - blog Garden (cnblogs.com)
(little voice, take a good look at the interface of this blog. After opening it, I played with the villain in the lower right corner for a long time before I thought of coming to see and explain XD