Time limit: 1.00 s Memory limit: 512 MB You are given an array that contains each number between 1…n exactly once. Your task is to collect the numbers from 1 to n in increasing order. On each round, you go through the array from left to right and collect as many numbers as possible. What will be the total number of rounds? Input The first line has an integer n: the array size. The next line has n integers x1,x2,…,xn: the numbers in the array. Output Print one integer: the number of rounds. Constraints 1≤n≤2⋅105 Example Input: 5 4 2 1 5 3 Output: 3
2 câu trả lời
full AC nha
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
int a[n+1],t=1,d=1;
for (int i=1;i<=n;i++)
{ cin>>d;a[d]=i; }
d=1;
for (int i=1;i<=n;i++) { if (d>a[i]) { t++; } d=a[i]; }
cout<<t;
}
var i,n,j:longint; a,l:array[0..1000000]of longint;
begin
readln(n);
for i:=1 to n do read(a[i]);
a[0]:=high(longint);
for i:=n downto 0 do
begin
l[i]:=1;
for j:=i+1 to n do if (a[i]>a[j])and(l[i]<l[j]+1) then
l[i]:=l[j]+1;
end;
writeln(l[0]-1);
end.
Câu hỏi trong lớp
Xem thêm