Hướng dẫn giải của VM 09 Bài 02 - Non-decreasing sequence
Chỉ dùng lời giải này khi không có ý tưởng, và đừng copy-paste code từ lời giải này. Hãy tôn trọng người ra đề và người viết lời giải.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.
Lưu ý: Các code mẫu dưới đây chỉ mang tính tham khảo và có thể không AC được bài tập này
Code mẫu của flashmt
#include <iostream> #include <algorithm> using namespace std; int n,x,curMax,curMin,ans,last,low[100100],high[100100]; int main() { cin >> n >> curMax; curMin=curMax; for (int i=1;i<=n;i++) { if (i<n) scanf("%d",&x); else x=1000111222; if (x>curMax) { while (last && curMin<=low[last]) last--; low[++last]=curMin; high[last]=curMax; curMax=curMin=x; } else curMin=min(curMin,x); } for (int i=1;i<=last;i++) ans+=high[i]-max(low[i],high[i-1]); cout << ans << endl; }
Code mẫu của ladpro98
#include <bits/stdc++.h> using namespace std; const int N = 100005; int n; int a[N], MAX[N]; int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 1; i <= n; ++i) cin >> a[i]; for (int i = 1; i <= n; ++i) { MAX[i] = MAX[i - 1]; if (a[MAX[i]] < a[i]) MAX[i] = i; } int ans = 0, last = n, upperBound = 1e9 + 9; while (last > 0) { int maxV = min(a[MAX[last]], upperBound), maxPos = MAX[last]; int minV = maxV; for (int i = maxPos + 1; i <= last; ++i) minV = min(minV, a[i]); ans += maxV - minV; upperBound = min(upperBound, minV); last = maxPos - 1; } cout << ans << endl; return 0; }
Code mẫu của RR
//Written by RR {$R+,Q+} {$Mode objfpc} {$inline on} uses math; const FINP = ''; FOUT = ''; MAXN = 200111; var f1,f2 : text; n : longint; a,lowest : array[1..MAXN] of longint; c,typ : array[1..MAXN] of longint; procedure openF; begin assign(f1,FINP); reset(f1); assign(f2,FOUT); rewrite(f2); end; procedure closeF; begin close(f1); close(f2); end; procedure inp; var i:longint; begin read(f1,n); for i:=1 to n do read(f1,a[i]); end; procedure swap(var a,b:longint); inline; var tmp:longint; begin tmp:=a; a:=b; b:=tmp; end; procedure sort(l,r:longint); inline; var i,j,mid,tmp:longint; begin i:=l; j:=r; mid:=c[l+random(r-l+1)]; repeat while c[i]<mid do inc(i); while c[j]>mid do dec(j); if i<=j then begin swap(c[i],c[j]); swap(typ[i],typ[j]); inc(i); dec(j); end; until i>j; if i<r then sort(i,r); if l<j then sort(l,j); end; procedure solve; var i,sl,save,sum:longint; res:int64; begin lowest[1]:=a[1]; for i:=2 to n do lowest[i]:=max(lowest[i-1],a[i]); sl:=0; for i:=1 to n do if a[i]<lowest[i] then begin inc(sl); c[sl]:=a[i]; typ[sl]:=1; inc(sl); c[sl]:=lowest[i]; typ[sl]:=-1; end; sort(1,sl); save:=c[1]; typ[sl+1]:=1; res:=0; sum:=0; for i:=1 to sl do begin inc(sum,typ[i]); if (typ[i]=-1) and (sum=0) then begin inc(res,c[i]-save); save:=c[i+1]; end; end; writeln(f2,res); end; begin openF; inp; solve; closeF; end.
Code mẫu của hieult
#include <set> #include <map> #include <list> #include <cmath> #include <queue> #include <stack> #include <cstdio> #include <string> #include <vector> #include <cstdlib> #include <cstring> #include <sstream> #include <iomanip> #include <iostream> #include <algorithm> #include <ctime> #include <deque> #include <bitset> #include <cctype> #include <utility> using namespace std; typedef long long ll; typedef long double ld; typedef unsigned int ui; typedef unsigned long long ull; #define Rep(i,n) for(int i = 0; i < (n); ++i) #define Repd(i,n) for(int i = (n)-1; i >= 0; --i) #define For(i,a,b) for(int i = (a); i <= (b); ++i) #define Ford(i,a,b) for(int i = (a); i >= (b); --i) #define Fit(i,v) for(__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++i) #define Fitd(i,v) for(__typeof((v).rbegin()) i = (v).rbegin(); i != (v).rend(); ++i) #define mp make_pair #define pb push_back #define fi first #define se second #define sz(a) ((int)(a).size()) #define all(a) (a).begin(), (a).end() #define ms(a,x) memset(a, x, sizeof(a)) template<class F, class T> T convert(F a, int p = -1) { stringstream ss; if (p >= 0) ss << fixed << setprecision(p); ss << a; T r; ss >> r; return r; } template<class T> T gcd(T a, T b) { T r; while (b != 0) { r = a % b; a = b; b = r; } return a; } template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; } template<class T> T sqr(T x) { return x * x; } template<class T> T cube(T x) { return x * x * x; } template<class T> int getbit(T s, int i) { return (s >> i) & 1; } template<class T> T onbit(T s, int i) { return s | (T(1) << i); } template<class T> T offbit(T s, int i) { return s & (~(T(1) << i)); } template<class T> int cntbit(T s) { return s == 0 ? 0 : cntbit(s >> 1) + (s & 1); } typedef pair<int, int> II; const ld PI = acos(-1.0); const ld eps = 1e-9; const int dr[] = { -1, 0, +1, 0 }; const int dc[] = { 0, +1, 0, -1 }; const int inf = (int) 1e9 + 5; const ll linf = (ll) 1e16 + 5; const ll mod = (ll) 1e9 + 7; #define maxn 200005 int a[maxn], n; vector<pair<int, int> > V; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); // freopen("in.txt", "r", stdin); ll res = 0; cin >> n; For(i, 1, n) cin >> a[i]; int MAX = 0; For(i, 1, n){ MAX = max(MAX, a[i]); V.pb(mp(a[i], MAX)); } sort(all(V)); res = V[0].se - V[0].fi; MAX = V[0].se; For(i, 1, sz(V) - 1){ res += max(0, V[i].se - max(V[i].fi, MAX)); MAX = max(MAX, V[i].se); } cout << res << endl; return 0; }
Code mẫu của ll931110
{$MODE DELPHI} program TOINCSEQ; const input = ''; output = ''; maxn = 100000; type rec = record inf,sup: integer; end; var n: integer; a: array[1..maxn] of integer; stack: array[1..maxn] of rec; top,res: integer; procedure init; var f: text; i: integer; begin assign(f, input); reset(f); readln(f, n); for i := 1 to n do read(f, a[i]); close(f); end; procedure push(v: rec); begin inc(top); stack[top] := v; end; procedure pop; begin dec(top); end; procedure solve; var i: integer; v: rec; begin top := 0; res := 0; i := 1; v.inf := 0; v.sup := 0; while (i < n) and (a[i] <= a[i + 1]) do begin v.sup := a[i + 1]; inc(i); end; if i = n then exit; v.sup := a[i]; v.inf := a[i + 1]; repeat while (i <= n) and (v.sup >= a[i]) do begin if v.inf > a[i] then v.inf := a[i]; inc(i); end; while (top > 0) and (v.inf < stack[top].inf) do pop; if top = 0 then push(v) else if v.inf > stack[top].sup then push(v) else stack[top].sup := v.sup; if i > n then break; v.sup := a[i]; while (i < n) and (a[i] <= a[i + 1]) do begin v.sup := a[i + 1]; inc(i); end; if i > n then break; v.inf := a[i]; until i > n; while top > 0 do begin res := res + (stack[top].sup - stack[top].inf); pop; end; end; procedure printresult; var f: text; begin assign(f, output); rewrite(f); writeln(f, res); close(f); end; begin init; solve; printresult; end.
Code mẫu của khuc_tuan
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <cmath> #include <sstream> #include <cstdlib> #include <algorithm> #include <map> #include <set> #include <queue> using namespace std; #define Rep(i,n) for(int i=0,lll=(n);i<lll;++i) #define For(i,a,b) for(int i=(a),lll=(b);i<=lll;++i) #define Ford(i,a,b) for(int i=(a),lll=(b);i>=lll;--i) #define pb push_back #define MP make_pair #define fi first #define se second inline int nextint() { int x; scanf("%d", &x); return x; } int n; pair<int,int> a[100010]; int min_index[100010]; int main() { scanf("%d", &n); Rep(i,n) { scanf("%d", &a[i].fi); a[i].se = i; } sort( a, a + n); min_index[n-1] = a[n-1].se; Ford(i,n-2,0) min_index[i] = min( min_index[i+1], a[i].se); int max_index = -1, res = 0; Rep(i,n-1) { max_index = max( max_index, a[i].se); if(max_index > min_index[i+1]) res += a[i+1].fi - a[i].fi; } printf("%d\n", res); return 0; }
Bình luận